Add debug_artifact/tests/test_core.py

This commit is contained in:
Mika 2026-01-25 17:42:38 +00:00
parent 1045b453c0
commit ae6c835b1c

View file

@ -0,0 +1,60 @@
import json
import pytest
from pathlib import Path
# Da der Architekturplan ein Single-Script-Layout vorgibt, importieren wir direkt aus dem Core-Modul.
from src.debug_artifact import core
@pytest.fixture()
def sample_raw_events():
# Beispielhafte Rohdaten, die realistische Szenarien abbilden.
return [
{"corr_id": "runA", "mischfenster": 0.45},
{"corr_id": "runB", "mischfenster": 0.95},
{"corr_id": "runC", "mischfenster": 0.8},
]
def test_create_debug_artifact_basic(sample_raw_events):
result = core.create_debug_artifact(sample_raw_events)
# Überprüfe Struktur des Rückgabewerts
assert isinstance(result, dict), "Ergebnis muss ein Dictionary sein"
assert set(result.keys()) == {"worst_mischfenster", "corr_id"}, "Falsche Keys im Ergebnis"
# Validierung der Typen
assert isinstance(result["worst_mischfenster"], float), "worst_mischfenster muss float sein"
assert isinstance(result["corr_id"], str), "corr_id muss str sein"
def test_create_debug_artifact_reproducibility(sample_raw_events):
first = core.create_debug_artifact(sample_raw_events)
second = core.create_debug_artifact(sample_raw_events)
assert first == second, "Ergebnisse müssen bei gleichem Input identisch sein"
def test_create_debug_artifact_edge_cases():
# Leere Liste sollte ein definierter Fallback sein
result_empty = core.create_debug_artifact([])
assert isinstance(result_empty, dict)
assert result_empty["worst_mischfenster"] == 0.0
assert result_empty["corr_id"] == "none"
# Ungültige Struktur — Einträge ohne Felder
bad_input = [{"no_corr_id": 1}]
with pytest.raises((KeyError, AssertionError, ValueError)):
_ = core.create_debug_artifact(bad_input)
def test_json_io(tmp_path: Path, sample_raw_events):
# Simuliere JSON-Datei-Input und Output
input_file = tmp_path / "input.json"
output_file = tmp_path / "output.json"
input_file.write_text(json.dumps(sample_raw_events))
loaded = json.loads(input_file.read_text())
result = core.create_debug_artifact(loaded)
# Ergebnis schreiben und lesen, um Reproduzierbarkeit zu bestätigen
output_file.write_text(json.dumps(result))
reloaded = json.loads(output_file.read_text())
assert result == reloaded, "JSON-Schreib-/Lesevorgang muss reproduzierbar sein"