test_logger.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import pytest
  2. from .logger import Logger, Consumer, NullConsumer, Level, DatetimeConsumer
  3. @pytest.fixture
  4. def logger ():
  5. return Logger (consumer=[NullConsumer (), DatetimeConsumer ()])
  6. class QueueConsumer (Consumer):
  7. def __init__ (self):
  8. self.data = []
  9. def __call__ (self, **kwargs):
  10. self.data.append (kwargs)
  11. return kwargs
  12. def test_bind (logger):
  13. # simple bind
  14. logger = logger.bind (foo='bar')
  15. ret = logger.debug ()
  16. assert ret['foo'] == 'bar'
  17. # additional
  18. ret = logger.debug (bar='baz')
  19. assert ret['foo'] == 'bar'
  20. assert ret['bar'] == 'baz'
  21. # override
  22. ret = logger.debug (foo='baz')
  23. assert ret['foo'] == 'baz'
  24. # unbind
  25. logger = logger.unbind (foo=None)
  26. ret = logger.debug ()
  27. assert 'foo' not in ret
  28. def test_consumer (logger):
  29. c = QueueConsumer ()
  30. logger.connect (c)
  31. ret = logger.debug (foo='bar')
  32. assert len (c.data) == 1
  33. assert c.data[0] == ret
  34. assert ret['foo'] == 'bar'
  35. c.data = []
  36. # inheritance
  37. logger = logger.bind (inherit=1)
  38. ret = logger.debug (foo='bar')
  39. assert len (c.data) == 1
  40. assert c.data[0] == ret
  41. assert ret['foo'] == 'bar'
  42. assert ret['inherit'] == 1
  43. c.data = []
  44. # removal
  45. logger.disconnect (c)
  46. ret = logger.debug (foo='bar')
  47. assert len (c.data) == 0
  48. assert ret['foo'] == 'bar'
  49. assert ret['inherit'] == 1
  50. def test_multiarg (logger):
  51. # single argument
  52. ret = logger.debug('maybe', foo='bar')
  53. assert ret['msg'] == 'maybe'
  54. assert ret['foo'] == 'bar'
  55. # multi arguments
  56. ret = logger.debug('may', 'be', foo='bar')
  57. assert ret['msg'] == ('may', 'be')
  58. assert ret['foo'] == 'bar'
  59. def test_call (logger):
  60. for level in ('debug', Level.DEBUG):
  61. ret = logger(level, 'arg1', 'arg2', foo='bar')
  62. assert ret['level'] == Level.DEBUG
  63. assert ret['msg'] == ('arg1', 'arg2')
  64. assert ret['foo'] == 'bar'
  65. def test_datetime (logger):
  66. ret = logger.debug()
  67. assert 'date' in ret
  68. def test_independence ():
  69. """ Make sure two instances are completely independent """
  70. l1 = Logger ()
  71. c = QueueConsumer ()
  72. l1.connect (c)
  73. l2 = Logger ()
  74. l2.info (nothing='nothing')
  75. assert not c.data