♻️refactor(reporter): factorize reporter.py for future notifier

let cli call get_period instead of functions in `reporter.py`, add notifier.py
This commit is contained in:
2026-09-14 18:36:20 +02:00
parent 91e76e6079
commit b1409dfea5
4 changed files with 146 additions and 36 deletions
+45 -20
View File
@@ -4,16 +4,16 @@ from pathlib import Path
import pytest
from logwatcher.classifier import classify_log_entries
from logwatcher.config import DATETIME_FORMAT, FIXTURE_PATH, SourceType
from logwatcher.config import DATETIME_FORMAT, FIXTURE_PATH, FRENCH_TIMEZONE, SourceType
from logwatcher.models import LogEntry
from logwatcher.parser import parse_file
from logwatcher.reporter import (
N2_SUPPORT_TEMPLATE,
OTHER_TEMPLATE,
_get_period,
_render_entries,
_render_target_report,
build_reports,
get_period,
write_log_report,
)
@@ -61,14 +61,14 @@ def get_only_irrelevant_log_entries_fixture(valid_log_dir: Path):
def test_get_period_empty_list():
"""An empty entry list yields an empty (start, end) period."""
assert _get_period([]) == ("", "")
assert get_period([]) == ("", "")
def test_get_period_format(
get_all_log_entries: tuple[list[LogEntry], list[LogEntry]],
):
"""Period bounds are formatted according to the configured datetime format."""
period = _get_period(get_all_log_entries[0] + get_all_log_entries[1])
period = get_period(get_all_log_entries[0] + get_all_log_entries[1])
for period_bound in period:
try:
assert datetime.strptime(period_bound, DATETIME_FORMAT) # noqa: DTZ007
@@ -158,11 +158,12 @@ def test_build_reports(
get_all_log_entries: tuple[list[LogEntry], list[LogEntry]], nb_files
):
"""The three reports are built with correct headers, counts, and section separation."""
start_date, end_date = _get_period(get_all_log_entries[0] + get_all_log_entries[1])
start_date, end_date = get_period(get_all_log_entries[0] + get_all_log_entries[1])
reports = build_reports(
SourceType.FILE,
get_all_log_entries[0],
get_all_log_entries[1],
datetime.now(FRENCH_TIMEZONE).strftime(DATETIME_FORMAT),
start_date,
end_date,
nb_files,
@@ -206,7 +207,18 @@ def _test_write_log_report(
"""Write the reports, then verify files exist and their content matches build_reports."""
results_path = output_dir / "results"
relevant, irrelevant = log_entries_tuple
write_log_report(SourceType.FILE, relevant, irrelevant, nb_files, results_path)
today = datetime.now(FRENCH_TIMEZONE).strftime(DATETIME_FORMAT)
start_date, end_date = get_period(relevant + irrelevant)
write_log_report(
SourceType.FILE,
relevant,
irrelevant,
nb_files,
today,
start_date,
end_date,
results_path,
)
# check if files were created
all_file, n2_file, other_file = (
(results_path / "all.log"),
@@ -218,9 +230,15 @@ def _test_write_log_report(
assert other_file.exists()
# check content
start_date, end_date = _get_period(relevant + irrelevant)
start_date, end_date = get_period(relevant + irrelevant)
reports = build_reports(
SourceType.FILE, relevant, irrelevant, start_date, end_date, nb_files
SourceType.FILE,
relevant,
irrelevant,
datetime.now(FRENCH_TIMEZONE).strftime(DATETIME_FORMAT),
start_date,
end_date,
nb_files,
)
nb_errors = {
"all": len(relevant + irrelevant),
@@ -277,12 +295,18 @@ def test_write_log_report_all_logs(
):
"""The full fixture writes three files whose content matches build_reports exactly."""
results_path = tmp_path / "results"
today = datetime.now(FRENCH_TIMEZONE).strftime(DATETIME_FORMAT)
start_date, end_date = get_period(get_all_log_entries[0] + get_all_log_entries[1])
write_log_report(
SourceType.FILE,
get_all_log_entries[0],
get_all_log_entries[1],
nb_files,
results_path,
source_type=SourceType.FILE,
relevant=get_all_log_entries[0],
irrelevant=get_all_log_entries[1],
nb_files=nb_files,
today=today,
start_date=start_date,
end_date=end_date,
output_dir=results_path,
)
# check if files were created
@@ -296,14 +320,15 @@ def test_write_log_report_all_logs(
assert other_file.exists()
# check content
start_date, end_date = _get_period(get_all_log_entries[0] + get_all_log_entries[1])
start_date, end_date = get_period(get_all_log_entries[0] + get_all_log_entries[1])
reports = build_reports(
SourceType.FILE,
get_all_log_entries[0],
get_all_log_entries[1],
start_date,
end_date,
nb_files,
source_type=SourceType.FILE,
relevant=get_all_log_entries[0],
irrelevant=get_all_log_entries[1],
today=today,
start_date=start_date,
end_date=end_date,
nb_files=nb_files,
)
assert reports["all"] == all_file.read_text(encoding="windows-1252")
assert reports["n2"] == n2_file.read_text(encoding="windows-1252")