clog/testing/test_printfmt.py
theonepath e4446c90ec
Added pytest testing module
Added files which hold unit tests for each package component
respectfully. Unit testing makes use of the pytest library.

Majority of tests are self-explanatory, with wordy function headers to
help understand the purpose of the test. Note, test modules DO NOT
provide best practices and are separate from the standards set for the
remaining code base. For this reason, mypy and flake8 are told to ignore
testing modules.

pytest-cov is used to gain an idea of how much of the code base is being
reached by the unit tests. Coverage does not have to be 100%, and 100%
coverage does not indicate "bug-free" code. If any bugs are identified,
or further unit tests are possible to increase code base coverage,
create a new Issue, or PR request with the appropriate changes.
2022-05-12 13:49:32 +01:00

95 lines
2.6 KiB
Python

# flake8: noqa
import re
import pytest
from utils.common import LogLevel
from clog.utils.printfmt import (
Colours,
gen_log_header,
log_as_col,
loglevel_as_str,
wrap,
)
class TestPrintFMT:
@pytest.mark.parametrize(
"seq", [
eval(f"Colours.{_}") for _ in dir(Colours) if _.isupper()
]
)
def test_colour_ansi_format(self, seq):
assert re.match('\\033\\[[0-9]+m', seq)
@pytest.mark.parametrize(
"value, expected", [
(Colours.NORMAL, '\033[0m'),
(Colours.RED, '\033[91m'),
(Colours.GREEN, '\033[92m'),
(Colours.YELLOW, '\033[93m'),
(Colours.BLUE, '\033[94m'),
]
)
def test_colour_ansi_match_codes(self, value, expected):
assert value == expected
def test_log_header_time_format_iso_8601(self):
assert re.match(
'^\\[\\d{4}(-\\d{2}){2}T\\d{2}(:\\d{2}){2}\\+\\d{4}\\].*$',
gen_log_header('')
)
def test_log_header_component_length(self):
assert len(gen_log_header('TEST').split(' ')) >= 4
def test_log_header_is_formattable(self):
assert len(re.findall(r'{(.*?)}', gen_log_header(LogLevel.DEBUG))) > 0
@pytest.mark.parametrize(
"value, expected", [
(LogLevel.NORMAL, Colours.NORMAL),
(LogLevel.PASS, Colours.GREEN),
(LogLevel.DEBUG, Colours.BLUE),
(LogLevel.WARN, Colours.YELLOW),
(LogLevel.ERROR, Colours.RED),
(-1, Colours.NORMAL),
(1000, Colours.NORMAL),
(2, '\033[94m'),
(LogLevel.WARN, '\033[93m'),
("a pesky string", Colours.NORMAL),
]
)
def test_log_as_colour(self, value, expected):
assert log_as_col(value) == expected
@pytest.mark.parametrize(
"value, expected", [
*zip([eval(f"LogLevel.{_}") for _ in dir(LogLevel) if _.isupper()],
["{0: <5}".format(_) for _ in dir(LogLevel) if _.isupper()])
]
)
def test_log_level_in_string_form(self, value, expected):
assert loglevel_as_str(value) == expected
@pytest.mark.parametrize(
"value, expected", [
("This won't be too long. So no wrap", False),
("This will be long enough" * 10, True),
(gen_log_header(LogLevel.DEBUG).format(
"Not long enough in file", CALLER=0), False),
(gen_log_header(LogLevel.DEBUG).format(
"Long enough to break many times" * 20, CALLER=0), True)
]
)
def test_wrap_has_newlines(self, value, expected):
assert (wrap(value).find('\n') != -1) == expected