You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.2 KiB
207 lines
5.2 KiB
5 years ago
|
""" tests for the sartorius_logger.datalogger module """
|
||
|
|
||
|
import pytest
|
||
|
|
||
|
from unittest.mock import call
|
||
|
|
||
|
|
||
|
@pytest.mark.parametrize(
|
||
|
"value,expected",
|
||
|
[
|
||
|
([], True),
|
||
|
((), True),
|
||
|
({}, True),
|
||
|
(set(), True),
|
||
|
(iter("A"), True),
|
||
|
("string", False),
|
||
|
(b"bytes", False),
|
||
|
],
|
||
|
)
|
||
|
def test_is_container(value, expected):
|
||
|
from sartorius_logger.datalogger import is_container
|
||
|
|
||
|
result = is_container(value)
|
||
|
|
||
|
assert result == expected
|
||
|
|
||
|
|
||
|
def test_datalogger_init():
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
from pathlib import Path
|
||
|
|
||
|
logger = DataLogger("/logs", none_rep="<none>", sep=";", end="<br>")
|
||
|
|
||
|
assert isinstance(logger.path, Path)
|
||
|
assert logger.path == Path("/logs")
|
||
|
assert logger.none_rep == "<none>"
|
||
|
assert logger.sep == ";"
|
||
|
assert logger.end == "<br>"
|
||
|
assert logger.handle is None
|
||
|
|
||
|
|
||
|
def test_datalogger_open(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
from pathlib import Path
|
||
|
from tempfile import TemporaryDirectory
|
||
|
|
||
|
with TemporaryDirectory() as tmp_dir:
|
||
|
tmp_dir_path = Path(tmp_dir)
|
||
|
log_folder = tmp_dir_path / "new_folder"
|
||
|
log_path = log_folder / "test.log"
|
||
|
|
||
|
logger = DataLogger(log_path)
|
||
|
logger.open()
|
||
|
|
||
|
assert log_folder.exists()
|
||
|
assert log_path.exists()
|
||
|
|
||
|
logger.close()
|
||
|
|
||
|
|
||
|
def test_datalogger_open_on_already_opened_connection(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path")
|
||
|
logger.handle = "not None"
|
||
|
logger.path = mocker.Mock()
|
||
|
|
||
|
logger.open()
|
||
|
|
||
|
assert logger.path.open.call_count == 0
|
||
|
|
||
|
|
||
|
def test_datalogger_close(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path")
|
||
|
logger.handle = mocker.Mock()
|
||
|
mocked_handle = logger.handle
|
||
|
|
||
|
logger.close()
|
||
|
|
||
|
assert mocked_handle.close.call_count == 1
|
||
|
assert logger.handle is None
|
||
|
|
||
|
|
||
|
@pytest.mark.parametrize(
|
||
|
"value,add_count,list_count", [(["a"], 0, 1), ("b", 1, 0)]
|
||
|
)
|
||
|
def test_datalogger_call(mocker, value, add_count, list_count):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path")
|
||
|
mocker.patch.object(logger, "add")
|
||
|
mocker.patch.object(logger, "add_list")
|
||
|
|
||
|
logger(value)
|
||
|
|
||
|
assert logger.add.call_count == add_count
|
||
|
assert logger.add_list.call_count == list_count
|
||
|
|
||
|
|
||
|
def test_datalogger_add(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path", end="<br>")
|
||
|
logger.handle = mocker.Mock()
|
||
|
|
||
|
logger.add("some \n text \r lines")
|
||
|
|
||
|
assert logger.handle.write.call_count == 1
|
||
|
assert logger.handle.write.call_args == call(
|
||
|
"some <br> text <br> lines<br>"
|
||
|
)
|
||
|
|
||
|
|
||
|
def test_datalogger_add_raises_error_if_logfile_not_opened():
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path")
|
||
|
|
||
|
with pytest.raises(IOError):
|
||
|
logger.add("whoops")
|
||
|
|
||
|
|
||
|
def test_datalogger_add_list(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path", none_rep="REDACTED", sep="; ", end="<br>")
|
||
|
logger.add = mocker.Mock()
|
||
|
|
||
|
logger.add_list(["foo", 1, None, "", "bar"])
|
||
|
|
||
|
assert logger.add.call_count == 1
|
||
|
assert logger.add.call_args == call("foo; 1; REDACTED; ; bar")
|
||
|
|
||
|
|
||
|
@pytest.mark.parametrize("add_empty_line", [True, False])
|
||
|
def test_datalogger_add_section(mocker, add_empty_line):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path", none_rep="REDACTED", sep="; ", end="<br>")
|
||
|
logger.add = mocker.Mock()
|
||
|
|
||
|
logger.add_section(
|
||
|
"A really cool title\n", [[1, 2], [3, 4, 5]], add_empty_line
|
||
|
)
|
||
|
|
||
|
assert logger.add.call_count == 4 if add_empty_line else 3
|
||
|
assert logger.add.call_args_list[:3] == [
|
||
|
call("[A really cool title]"),
|
||
|
call("1; 2"),
|
||
|
call("3; 4; 5"),
|
||
|
]
|
||
|
if add_empty_line:
|
||
|
logger.add.call_args = call("")
|
||
|
|
||
|
|
||
|
def test_datalogger_as_context_manager(mocker):
|
||
|
from sartorius_logger.datalogger import DataLogger
|
||
|
|
||
|
logger = DataLogger("log_path")
|
||
|
logger.open = mocker.Mock()
|
||
|
logger.close = mocker.Mock()
|
||
|
|
||
|
with logger as context:
|
||
|
assert logger is context
|
||
|
|
||
|
assert logger.open.call_count == 1
|
||
|
assert logger.close.call_count == 1
|
||
|
|
||
|
|
||
|
def test_datalogger_null_logger_init_should_not_raise_error(mocker):
|
||
|
from sartorius_logger.datalogger import NullLogger
|
||
|
|
||
|
NullLogger("log_path", end="<br>", unknown="nothing")
|
||
|
|
||
|
|
||
|
def test_datalogger_null_logger_is_callable(mocker):
|
||
|
from sartorius_logger.datalogger import NullLogger
|
||
|
|
||
|
nl = NullLogger()
|
||
|
|
||
|
assert nl("whatever") is None
|
||
|
|
||
|
|
||
|
def test_datalogger_null_logger_attribute_access_returns_instance(mocker):
|
||
|
from sartorius_logger.datalogger import NullLogger
|
||
|
|
||
|
nl = NullLogger()
|
||
|
|
||
|
assert nl.some_really_weird_attribute is nl
|
||
|
|
||
|
|
||
|
def test_datalogger_null_logger_allows_method_calls(mocker):
|
||
|
from sartorius_logger.datalogger import NullLogger
|
||
|
|
||
|
nl = NullLogger()
|
||
|
|
||
|
assert nl.calling_any_method(1, b=2) is None
|
||
|
|
||
|
|
||
|
def test_datalogger_null_as_context_manager(mocker):
|
||
|
from sartorius_logger.datalogger import NullLogger
|
||
|
|
||
|
with NullLogger() as nl:
|
||
|
assert isinstance(nl, NullLogger)
|