Add unknown_analysis/tests/test_core.py

This commit is contained in:
Mika 2026-02-12 11:16:21 +00:00
parent 1ad66a1c73
commit 616458e8d9

View file

@ -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)