Beispiel #1
0
    def test_pickle(self, proto):
        """
        Can be pickled and unpickled.
        """
        bllp = BoundLoggerLazyProxy(None)

        assert repr(bllp) == repr(pickle.loads(pickle.dumps(bllp, proto)))
Beispiel #2
0
    def test_honors_wrapper_class(self):
        """
        Passed wrapper_class is used.
        """
        p = BoundLoggerLazyProxy(None, wrapper_class=Wrapper)
        b = p.bind()

        assert isinstance(b, Wrapper)
Beispiel #3
0
    def test_unbind_unbinds_from_initial_values(self):
        """
        It's possible to unbind a value that came from initial_values.
        """
        p = BoundLoggerLazyProxy(None, initial_values={"a": 1, "b": 2})
        b = p.unbind("a")

        assert {"b": 2} == b._context
Beispiel #4
0
 def test_new_binds_only_initial_values_explicit_ctx_class(self, proxy):
     proxy = BoundLoggerLazyProxy(None,
                                  initial_values={
                                      "a": 1,
                                      "b": 2
                                  },
                                  context_class=dict)
     b = proxy.new(foo=42)
     assert {"a": 1, "b": 2, "foo": 42} == b._context
Beispiel #5
0
    def test_new_binds_only_initial_values_implicit_ctx_class(self, proxy):
        """
        new() doesn't clear initial_values if context_class comes from config.
        """
        proxy = BoundLoggerLazyProxy(None, initial_values={"a": 1, "b": 2})

        b = proxy.new(foo=42)

        assert {"a": 1, "b": 2, "foo": 42} == b._context
Beispiel #6
0
    def test_bind_honors_initial_values(self):
        """
        Passed initia_values are merged on binds.
        """
        p = BoundLoggerLazyProxy(None, initial_values={"a": 1, "b": 2})
        b = p.bind()

        assert {"a": 1, "b": 2} == b._context

        b = p.bind(c=3)

        assert {"a": 1, "b": 2, "c": 3} == b._context
Beispiel #7
0
    def test_emphemeral(self):
        """
        Calling an unknown method proxy creates a new wrapped bound logger
        first.
        """
        class Foo(BoundLoggerBase):
            def foo(self):
                return 42

        proxy = BoundLoggerLazyProxy(None,
                                     wrapper_class=Foo,
                                     cache_logger_on_first_use=False)
        assert 42 == proxy.foo()
Beispiel #8
0
 def test_repr(self):
     p = BoundLoggerLazyProxy(
         None, processors=[1, 2, 3], context_class=dict,
         initial_values={'foo': 42}, logger_factory_args=(4, 5),
     )
     assert (
         "<BoundLoggerLazyProxy(logger=None, wrapper_class=None, "
         "processors=[1, 2, 3], "
         "context_class=<%s 'dict'>, "
         "initial_values={'foo': 42}, "
         "logger_factory_args=(4, 5))>"
         % ('class' if PY3 else 'type',)
     ) == repr(p)
Beispiel #9
0
 def test_new_binds_only_initial_values_explicit_ctx_class(self, proxy):
     """
     new() doesn't clear initial_values if context_class is passed
     explicitly..
     """
     proxy = BoundLoggerLazyProxy(None,
                                  initial_values={
                                      "a": 1,
                                      "b": 2
                                  },
                                  context_class=dict)
     b = proxy.new(foo=42)
     assert {"a": 1, "b": 2, "foo": 42} == b._context
Beispiel #10
0
    def test_argument_takes_precedence_over_configuration(self, cache):
        """
        Passing cache_logger_on_first_use as an argument overrides config.
        """
        configure(cache_logger_on_first_use=cache)

        proxy = BoundLoggerLazyProxy(None, cache_logger_on_first_use=not cache)
        bind = proxy.bind
        proxy.bind()

        if cache:
            assert bind == proxy.bind
        else:
            assert bind != proxy.bind
Beispiel #11
0
    def test_bind_doesnt_cache_logger(self):
        """
        Calling configure() changes BoundLoggerLazyProxys immediately.
        Previous uses of the BoundLoggerLazyProxy don't interfere.
        """
        class F(object):
            "New logger factory with a new attribute"
            def a(self, *args):
                return 5

        proxy = BoundLoggerLazyProxy(None)
        proxy.bind()
        configure(logger_factory=F)
        new_b = proxy.bind()
        assert new_b.a() == 5
Beispiel #12
0
 def test_repr(self):
     """
     repr reflects all attributes.
     """
     p = BoundLoggerLazyProxy(
         None,
         processors=[1, 2, 3],
         context_class=dict,
         initial_values={"foo": 42},
         logger_factory_args=(4, 5),
     )
     assert ("<BoundLoggerLazyProxy(logger=None, wrapper_class=None, "
             "processors=[1, 2, 3], "
             "context_class=<class 'dict'>, "
             "initial_values={'foo': 42}, "
             "logger_factory_args=(4, 5))>") == repr(p)
Beispiel #13
0
    def test_prefers_args_over_config(self):
        p = BoundLoggerLazyProxy(None, processors=[1, 2, 3],
                                 context_class=dict)
        b = p.bind()
        assert isinstance(b._context, dict)
        assert [1, 2, 3] == b._processors

        class Class(object):
            def __init__(self, *args, **kw):
                pass

            def update(self, *args, **kw):
                pass
        configure(processors=[4, 5, 6], context_class=Class)
        b = p.bind()
        assert not isinstance(b._context, Class)
        assert [1, 2, 3] == b._processors
Beispiel #14
0
    def test_prefers_args_over_config(self):
        """
        Configuration can be overridden by passing arguments.
        """
        p = BoundLoggerLazyProxy(None,
                                 processors=[1, 2, 3],
                                 context_class=dict)
        b = p.bind()
        assert isinstance(b._context, dict)
        assert [1, 2, 3] == b._processors

        class Class:
            def __init__(self, *args, **kw):
                pass

            def update(self, *args, **kw):
                pass

        configure(processors=[4, 5, 6], context_class=Class)
        b = p.bind()

        assert not isinstance(b._context, Class)
        assert [1, 2, 3] == b._processors
Beispiel #15
0
def proxy():
    """
    Returns a BoundLoggerLazyProxy constructed w/o paramaters & None as logger.
    """
    return BoundLoggerLazyProxy(None)
Beispiel #16
0
 def test_argument_takes_precedence_over_configuration2(self):
     configure(cache_logger_on_first_use=False)
     proxy = BoundLoggerLazyProxy(None, cache_logger_on_first_use=True)
     bind = proxy.bind
     proxy.bind()
     assert bind != proxy.bind
Beispiel #17
0
 def test_unbind_unbinds_from_initial_values(self):
     p = BoundLoggerLazyProxy(None, initial_values={"a": 1, "b": 2})
     b = p.unbind("a")
     assert {"b": 2} == b._context
Beispiel #18
0
 def test_bind_honors_initial_values(self):
     p = BoundLoggerLazyProxy(None, initial_values={"a": 1, "b": 2})
     b = p.bind()
     assert {"a": 1, "b": 2} == b._context
     b = p.bind(c=3)
     assert {"a": 1, "b": 2, "c": 3} == b._context
Beispiel #19
0
 def test_new_binds_only_initial_values_impolicit_ctx_class(self, proxy):
     proxy = BoundLoggerLazyProxy(None, initial_values={'a': 1, 'b': 2})
     b = proxy.new(foo=42)
     assert {'a': 1, 'b': 2, 'foo': 42} == b._context
Beispiel #20
0
 def test_unbind_unbinds_from_initial_values(self):
     p = BoundLoggerLazyProxy(None, initial_values={'a': 1, 'b': 2})
     b = p.unbind('a')
     assert {'b': 2} == b._context
Beispiel #21
0
 def test_bind_honors_initial_values(self):
     p = BoundLoggerLazyProxy(None, initial_values={'a': 1, 'b': 2})
     b = p.bind()
     assert {'a': 1, 'b': 2} == b._context
     b = p.bind(c=3)
     assert {'a': 1, 'b': 2, 'c': 3} == b._context