From 4ff22c100f26b4c4e8fad70e8cebb5e688c6f8e7 Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 22 Feb 2026 12:32:33 +0000 Subject: [PATCH] Add artifact.logging_module/tests/test_core.py --- artifact.logging_module/tests/test_core.py | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 artifact.logging_module/tests/test_core.py diff --git a/artifact.logging_module/tests/test_core.py b/artifact.logging_module/tests/test_core.py new file mode 100644 index 0000000..13d1825 --- /dev/null +++ b/artifact.logging_module/tests/test_core.py @@ -0,0 +1,113 @@ +import json +import os +import tempfile +from pathlib import Path +import pytest + +# Da das getestete Modul nicht spezifiziert wurde, greifen wir auf den Paketnamen zurck +import src.artifact_logging_module.core as core + + +def make_temp_log_file(entries=None): + data = entries or [] + tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json") + tmp.write(json.dumps(data).encode("utf-8")) + tmp.flush() + tmp.close() + return tmp.name + + +def teardown_temp_file(path): + try: + os.remove(path) + except FileNotFoundError: + pass + + +def test_logentry_initialization(): + entry = core.LogEntry( + timestamp="2024-06-01T12:00:00Z", + decision_type="PASS", + reason_code="CODE_OK", + additional_info="All checks passed" + ) + assert entry.timestamp == "2024-06-01T12:00:00Z" + assert entry.decision_type == "PASS" + assert entry.reason_code == "CODE_OK" + assert entry.additional_info == "All checks passed" + + +def test_log_decision_creates_valid_json_file(monkeypatch): + tmp_dir = tempfile.TemporaryDirectory() + log_path = Path(tmp_dir.name) / "ci_decisions.json" + + # ensure the function writes to expected location relative or default file + monkeypatch.setattr(core, "LOG_FILE_PATH", log_path) + + core.log_decision("PASS", "INIT_OK") + assert log_path.exists() + + with open(log_path, "r", encoding="utf-8") as f: + data = json.load(f) + assert isinstance(data, list) + assert len(data) == 1 + entry = data[0] + assert entry["decision_type"] == "PASS" + assert entry["reason_code"] == "INIT_OK" + + tmp_dir.cleanup() + + +def test_log_decision_invalid_inputs(monkeypatch): + tmp_dir = tempfile.TemporaryDirectory() + log_path = Path(tmp_dir.name) / "ci_decisions.json" + monkeypatch.setattr(core, "LOG_FILE_PATH", log_path) + + with pytest.raises(AssertionError): + core.log_decision(123, "CODE") + with pytest.raises(AssertionError): + core.log_decision("PASS", 789) + + tmp_dir.cleanup() + + +def test_generate_report_summarizes_entries(monkeypatch): + entries = [ + {"timestamp": "t1", "decision_type": "PASS", "reason_code": "OK", "additional_info": "good"}, + {"timestamp": "t2", "decision_type": "WARN", "reason_code": "WARN_CODE", "additional_info": "minor"}, + {"timestamp": "t3", "decision_type": "UNKNOWN", "reason_code": "UNK", "additional_info": "unknown"} + ] + + tmp_file = make_temp_log_file(entries) + monkeypatch.setattr(core, "LOG_FILE_PATH", Path(tmp_file)) + + report = core.generate_report() + assert isinstance(report, str) + assert "PASS" in report + assert "WARN" in report + assert "UNKNOWN" in report + + teardown_temp_file(tmp_file) + + +def test_generate_report_empty(monkeypatch): + tmp_file = make_temp_log_file([]) + monkeypatch.setattr(core, "LOG_FILE_PATH", Path(tmp_file)) + + report = core.generate_report() + assert "No entries" in report or isinstance(report, str) + teardown_temp_file(tmp_file) + + +def test_generate_report_invalid_json(monkeypatch): + tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json") + tmp.write(b"not a json") + tmp.flush() + tmp.close() + + monkeypatch.setattr(core, "LOG_FILE_PATH", Path(tmp.name)) + + with pytest.raises(Exception): + _ = core.generate_report() + + teardown_temp_file(tmp.name)