summaryrefslogtreecommitdiff
path: root/crocoite/test_logger.py
blob: 8a34aab3e02e6d9e9dda898a1bb3e98f12b66d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pytest
from .logger import Logger, Consumer, NullConsumer

@pytest.fixture
def logger ():
    return Logger (consumer=[NullConsumer ()])

class QueueConsumer (Consumer):
    def __init__ (self):
        self.data = []

    def __call__ (self, **kwargs):
        self.data.append (kwargs)
        return kwargs

def test_bind (logger):
    # simple bind
    logger = logger.bind (foo='bar')
    ret = logger.debug ()
    assert ret['foo'] == 'bar'

    # additional
    ret = logger.debug (bar='baz')
    assert ret['foo'] == 'bar'
    assert ret['bar'] == 'baz'
    
    # override
    ret = logger.debug (foo='baz')
    assert ret['foo'] == 'baz'

    # unbind
    logger = logger.unbind (foo=None)
    ret = logger.debug ()
    assert 'foo' not in ret

def test_consumer (logger):
    c = QueueConsumer ()
    logger.connect (c)
    ret = logger.debug (foo='bar')
    assert len (c.data) == 1
    assert c.data[0] == ret
    assert ret['foo'] == 'bar'
    c.data = []

    # inheritance
    logger = logger.bind (inherit=1)
    ret = logger.debug (foo='bar')
    assert len (c.data) == 1
    assert c.data[0] == ret
    assert ret['foo'] == 'bar'
    assert ret['inherit'] == 1
    c.data = []

    # removal
    logger.disconnect (c)
    ret = logger.debug (foo='bar')
    assert len (c.data) == 0
    assert ret['foo'] == 'bar'
    assert ret['inherit'] == 1