Skip to content

Commit

Permalink
Modify tests in tests/test_logs.py
Browse files Browse the repository at this point in the history
- Remove `tests` logger in `pyzzy.logs.vars.DEFAULT_CONFIG`
- Remove `tests/_test_logger.py`
  • Loading branch information
krakozaure committed Aug 16, 2018
1 parent d141b25 commit 1bfab77
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 81 deletions.
2 changes: 1 addition & 1 deletion pyzzy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.4"
__version__ = "0.0.5"
__author__ = "krakozaure"
__license__ = "MIT"
5 changes: 0 additions & 5 deletions pyzzy/logs/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ def _fg_colorize(text, color):
"level": "DEBUG",
"handlers": ["console_production", "tr_file_warnings"],
},
"tests": {
"propagate": False,
"level": "DEBUG",
"handlers": ["console_development", "file"],
},
},
"root": {
"level": "DEBUG",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyzzy",
version="0.0.4",
version="0.0.5",
author="krakozaure",
license="MIT",
author_email="",
Expand Down
40 changes: 0 additions & 40 deletions tests/_test_logger.py

This file was deleted.

101 changes: 67 additions & 34 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,84 @@
import contextlib
import io
import logging
import time

import _pytest.logging
import pytest

import pyzzy as pz


def test_init_logging():
pz.init_logging(
config=pz.logs.vars.DEFAULT_CONFIG,
capture_warnings=True,
simple_warnings=True,
raise_exceptions=True,
)
@contextlib.contextmanager
def capture_handler_stream(logger, handler_name):
handler = get_handler_by_name(logger.handlers, handler_name)
old_stream = handler.stream
new_stream = io.StringIO()
handler.stream = new_stream
try:
yield new_stream
finally:
new_stream.close()
handler.stream = old_stream


def test_handler_console_production(caplog):
caplog.clear()
def get_handler_by_name(handlers, name):
for handler in handlers:
if handler.get_name() == name:
return handler

logger = logging.getLogger('production')
logger.propagate = True

handler = get_handler_by_name(logger.handlers, 'console_production')
formatter = handler.formatter
@pytest.fixture()
def logging_config():
logging.config.dictConfig(pz.logs.DEFAULT_CONFIG)
return pz.logs.DEFAULT_CONFIG

log_fmt = formatter._fmt
log_msg = "Log message"
log_tags = pz.logs.vars._colored_tags

all_levels = logging._levelToName.keys()
test_levels = [lvl for lvl in all_levels if lvl >= handler.level]
expected_levels = logging.CRITICAL, logging.ERROR, logging.WARNING
def test_root_handlers_names(logging_config):
logger = logging.getLogger()
root_handlers_names = [
h.get_name() for h in logger.handlers
if not isinstance(h, _pytest.logging.LogCaptureHandler)
]
assert root_handlers_names == ['console_production', 'tr_file']

for level_number in sorted(test_levels):
logger.log(level_number, log_msg)

result = [
formatter.format(record)
for record in caplog.records
]
def test_root_console_handler_output(logging_config):
logger = logging.getLogger()
message = "Log message"

expected = [
log_fmt % {'message': log_msg, 'levelname': log_tags[level]}
for level in sorted(expected_levels)
]
with capture_handler_stream(logger, "console_production") as stream:
logger.error(message)
logger.debug(message)
captured_output = stream.getvalue()

assert result == expected
assert message in captured_output
assert pz.logs.vars._colored_tags[logging.ERROR] in captured_output
assert pz.logs.vars._colored_tags[logging.DEBUG] not in captured_output


def get_handler_by_name(handlers, name):
for handler in handlers:
if handler.get_name() == name:
return handler
def test_root_trfile_handler_output(logging_config):
logger = logging.getLogger()
message = "Log message"

with capture_handler_stream(logger, "tr_file") as stream:
logger.error(message)
logger.debug(message)
captured_output = stream.getvalue()

assert message in captured_output
assert logging._levelToName[logging.ERROR] in captured_output
assert logging._levelToName[logging.DEBUG] in captured_output
assert time.strftime('%Y-%m-%d %H:%M') in captured_output
assert logger.name in captured_output


def main():
logging_config_ = logging_config()
test_root_handlers_names(logging_config_)
test_root_console_handler_output(logging_config_)
test_root_trfile_handler_output(logging_config_)


if __name__ == "__main__":
main()

0 comments on commit 1bfab77

Please sign in to comment.