From 616458e8d9bd0fcc84a8ccd5611b184059c0ef53 Mon Sep 17 00:00:00 2001 From: Mika Date: Thu, 12 Feb 2026 11:16:21 +0000 Subject: [PATCH] Add unknown_analysis/tests/test_core.py --- unknown_analysis/tests/test_core.py | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 unknown_analysis/tests/test_core.py diff --git a/unknown_analysis/tests/test_core.py b/unknown_analysis/tests/test_core.py new file mode 100644 index 0000000..7ef351b --- /dev/null +++ b/unknown_analysis/tests/test_core.py @@ -0,0 +1,61 @@ +import pytest +from unknown_analysis import core + +@pytest.fixture +def sample_log_data(): + return [ + {"artifact_key": "a1", "status": "PASS", "cause": "", "path": "p1", "error": ""}, + {"artifact_key": "a2", "status": "UNKNOWN", "cause": "missing", "path": "p2", "error": "FileNotFoundError"}, + {"artifact_key": "a3", "status": "UNKNOWN", "cause": "schema", "path": "p3", "error": "SchemaMismatch"}, + {"artifact_key": "a4", "status": "PASS", "cause": "", "path": "p4", "error": ""}, + {"artifact_key": "a5", "status": "UNKNOWN", "cause": "missing", "path": "p5", "error": "FileNotFoundError"} + ] + +@pytest.fixture +def empty_log_data(): + return [] + +def test_calculate_unknown_rates_basic(sample_log_data): + result = core.calculate_unknown_rates(sample_log_data) + assert isinstance(result, dict) + assert set(result.keys()) == {"unknown_artifact_missing_rate", "unknown_schema_rate"} + total_unknowns = 3 + missing_count = 2 + schema_count = 1 + assert pytest.approx(result['unknown_artifact_missing_rate'], 0.1) == missing_count / total_unknowns + assert pytest.approx(result['unknown_schema_rate'], 0.1) == schema_count / total_unknowns + +def test_calculate_unknown_rates_empty(empty_log_data): + result = core.calculate_unknown_rates(empty_log_data) + assert result['unknown_artifact_missing_rate'] == 0.0 + assert result['unknown_schema_rate'] == 0.0 + +def test_calculate_unknown_rates_invalid_input(): + with pytest.raises((TypeError, KeyError)): + core.calculate_unknown_rates([{"invalid": True}]) + +def test_get_top_pass_unknown_switches_basic(sample_log_data): + result = core.get_top_pass_unknown_switches(sample_log_data) + assert isinstance(result, list) + assert all(isinstance(item, dict) for item in result) + if result: + for entry in result: + assert set(entry.keys()) == {"cause", "path", "error"} + +def test_get_top_pass_unknown_switches_empty(empty_log_data): + result = core.get_top_pass_unknown_switches(empty_log_data) + assert result == [] + +def test_get_top_pass_unknown_switches_structure(sample_log_data): + top_switches = core.get_top_pass_unknown_switches(sample_log_data) + # Ensure all required fields exist and are strings + for s in top_switches: + assert isinstance(s['cause'], str) + assert isinstance(s['path'], str) + assert isinstance(s['error'], str) + +def test_integration_rates_and_switches(sample_log_data): + rates = core.calculate_unknown_rates(sample_log_data) + switches = core.get_top_pass_unknown_switches(sample_log_data) + assert 'unknown_artifact_missing_rate' in rates + assert isinstance(switches, list) \ No newline at end of file