diff --git a/artifact.1/tests/test_core.py b/artifact.1/tests/test_core.py new file mode 100644 index 0000000..d598450 --- /dev/null +++ b/artifact.1/tests/test_core.py @@ -0,0 +1,78 @@ +import pytest +import pandas as pd +from scipy.stats import mannwhitneyu + +from src.artifact_1 import core + +@pytest.fixture +def sample_run_data_valid(): + return { + "timestamp": "2024-06-10T10:00:00Z", + "pinned_flag": True, + "runtime": 120.5, + "seqcount_retry_count": 2, + "mischfenster_dauer": 3.4 + } + +@pytest.fixture +def sample_run_data_unpinned(): + return { + "timestamp": "2024-06-10T10:02:00Z", + "pinned_flag": False, + "runtime": 122.0, + "seqcount_retry_count": 5, + "mischfenster_dauer": 7.2 + } + +def test_calculate_metrics_basic(sample_run_data_valid): + result = core.calculate_metrics(sample_run_data_valid) + assert isinstance(result, dict) + assert "retry_free_rate" in result + assert "mischfenster_stats" in result + assert "correlations" in result + # retry free rate sanity + assert 0.0 <= result["retry_free_rate"] <= 1.0 + # mischfenster_stats contains expected keys + stats = result["mischfenster_stats"] + for key in ("p50", "p95", "max"): + assert key in stats + # correlations sanity check + assert isinstance(result["correlations"], dict) + +def test_calculate_metrics_invalid_input(): + invalid_data = { + "timestamp": "2024-06-10T10:00:00Z", + # missing required fields + } + with pytest.raises((KeyError, ValueError, TypeError)): + core.calculate_metrics(invalid_data) + +def test_run_mann_whitney_test_basic(): + data1 = [1.2, 1.4, 1.6, 1.8, 2.0] + data2 = [2.1, 2.3, 2.5, 2.7, 2.9] + p_value = core.run_mann_whitney_test(data1, data2) + assert isinstance(p_value, float) + assert 0.0 <= p_value <= 1.0 + +def test_run_mann_whitney_test_identical_data(): + data = [1.0, 1.1, 1.2, 1.3] + p_value = core.run_mann_whitney_test(data, data.copy()) + assert pytest.approx(p_value, rel=1e-6) == 1.0 + +@pytest.mark.parametrize( + "data1,data2", + [ + ([1, 2, 3], [4, 5, 6]), + ([10.0, 10.5, 10.8], [9.7, 9.8, 9.9]), + ] +) +def test_run_mann_whitney_test_symmetry(data1, data2): + p1 = core.run_mann_whitney_test(data1, data2) + p2 = core.run_mann_whitney_test(data2, data1) + assert pytest.approx(p1, rel=1e-6) == p2 + +def test_run_mann_whitney_test_invalid_input(): + with pytest.raises((ValueError, TypeError)): + core.run_mann_whitney_test([], [1, 2, 3]) + with pytest.raises((ValueError, TypeError)): + core.run_mann_whitney_test([1, 2, 3], [None, 5]) \ No newline at end of file