test(classifier): ✅ add test on n2 patterns
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import re
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from logwatcher.classifier import N2_PATTERNS
|
||||
from logwatcher.config import FIXTURE_PATH
|
||||
|
||||
ORIGINAL_LOGS_DIR = FIXTURE_PATH / "original_logs"
|
||||
|
||||
|
||||
def _pattern_name_to_filename(name: str) -> str:
|
||||
"""STORE_CODE_NOT_FOUND → CR_store_code_not_found.txt"""
|
||||
return f"CR_{name.lower()}.txt"
|
||||
|
||||
def _read_fixture(path: Path) -> str:
|
||||
"""Lit un fichier de fixture avec fallback Windows-1252."""
|
||||
try:
|
||||
return path.read_text(encoding="utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return path.read_text(encoding="windows-1252")
|
||||
|
||||
@pytest.mark.parametrize("name, pattern", N2_PATTERNS.items())
|
||||
def test_pattern_matches_fixture(name, pattern):
|
||||
"""Chaque pattern N2 matche au moins une ligne de son fichier de fixture."""
|
||||
|
||||
fixture = ORIGINAL_LOGS_DIR / _pattern_name_to_filename(name)
|
||||
if not fixture.exists():
|
||||
msg = f"No fixture named '{fixture}'"
|
||||
warnings.warn(msg)
|
||||
pytest.skip(msg)
|
||||
|
||||
lignes = [
|
||||
l for l in _read_fixture(fixture).splitlines()
|
||||
if l.strip()
|
||||
]
|
||||
|
||||
assert lignes, f"empty fixture file : {fixture}"
|
||||
|
||||
match_count = 0
|
||||
|
||||
for ligne in lignes:
|
||||
match = re.search(pattern, ligne)
|
||||
if match:
|
||||
match_count += 1
|
||||
|
||||
assert match_count > 0, (
|
||||
f"{name} : no match in file {fixture.name}\n"
|
||||
f"pattern: {pattern}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user