feat(classifier): ✨ add classifier feature on log entries

add classifier on log entries, clean test functions and uniform fixtures in windows-1252 since all logs are encoded in windows-1252
This commit is contained in:
2026-08-26 11:34:54 +02:00
parent 51adcfd8ad
commit 23afe3f185
26 changed files with 3146 additions and 505 deletions
+8 -13
View File
@@ -2,40 +2,37 @@ from pathlib import Path
import pytest
from logwatcher.config import FIXTURE_PATH
from logwatcher.models import LogEntry
from logwatcher.parser import parse_log_file, parse_log_line
INVALID_LOGS_DIR = FIXTURE_PATH / "invalid_logs"
SPECIFIC_LOGS_DIR = FIXTURE_PATH / "valid_logs"
@pytest.fixture(name="empty_log_file")
def empty_log_file_fixture() -> Path:
def empty_log_file_fixture(invalid_log_dir: Path) -> Path:
"""
Path of an empty log file.
Ensures the parser does not raise an exception
and returns an empty list.
"""
return Path(INVALID_LOGS_DIR / "empty_file.txt")
return invalid_log_dir / "empty_file.txt"
@pytest.fixture(name="bad_log_file")
def bad_log_file_fixture() -> Path:
def bad_log_file_fixture(invalid_log_dir: Path) -> Path:
"""
Path of a file containing only incorrectly formatted lines.
Each line fails at a different point in LOG_PATTERN
(invalid IP, missing timestamp, missing DOSSIER EN COURS, etc.).
No LogEntry should be produced.
"""
return Path(INVALID_LOGS_DIR / "bad_log_file.txt")
return invalid_log_dir / "bad_log_file.txt"
@pytest.fixture(name="specific_logs")
def specific_logs_fixture() -> list[str]:
def specific_logs_fixture(valid_log_dir: Path) -> list[str]:
"""
Single log lines chosen to cover specific cases.
Each line is structurally valid and must be parsed
successfully. Used for unit tests of parse_log_line().
"""
log_file = Path(SPECIFIC_LOGS_DIR / "specific_logs.txt")
log_file = valid_log_dir / "specific_logs.txt"
with open(log_file, 'r') as file:
return file.readlines()
@@ -57,8 +54,6 @@ def test_parse_log_line(specific_logs: list[str]):
assert log_entry.error_message is not None
assert log_entry.raw_line != ''
def test_parse_empty_log_line():
"""
An empty or structurally invalid line returns None.
@@ -72,14 +67,14 @@ def test_parse_empty_log_line():
log_entry = parse_log_line("Répertoire scanné : \\\\192.168.60.48\\e$\\MDC_9110\\Logs")
assert not log_entry
def test_parse_valid_log_file(original_log_files: tuple[Path, ...]):
def test_parse_valid_log_file(original_log_dir: Path):
"""
Each real log file produces at least one valid LogEntry.
Integration test: files provided by technicians contain
a mix of valid lines and lines to be ignored.
Ensures the parser extracts at least one entry per file.
"""
for log_file in original_log_files:
for log_file in original_log_dir.glob("CR_*"):
log_list = parse_log_file(log_file)
assert len(log_list) > 0