From 0269cf3831a4dfa78b417ab3136313fc5913a493 Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 1 Mar 2026 17:26:32 +0000 Subject: [PATCH] Add run_analysis/tests/test_core.py --- run_analysis/tests/test_core.py | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 run_analysis/tests/test_core.py diff --git a/run_analysis/tests/test_core.py b/run_analysis/tests/test_core.py new file mode 100644 index 0000000..90d3dbf --- /dev/null +++ b/run_analysis/tests/test_core.py @@ -0,0 +1,65 @@ +import json +import pytest +from pathlib import Path + +import run_analysis.core as core + +@pytest.fixture() +def sample_run_data(): + # Representative dataset including negative and non-negative visibility_lag + return [ + {"corr_id": "a1", "expires_at_dist_hours": 0.5, "visibility_lag": -2.3}, + {"corr_id": "b2", "expires_at_dist_hours": 1.0, "visibility_lag": 0.0}, + {"corr_id": "c3", "expires_at_dist_hours": 2.0, "visibility_lag": -0.5}, + {"corr_id": "d4", "expires_at_dist_hours": 3.5, "visibility_lag": 1.2} + ] + + +def test_analyze_run_data_basic(sample_run_data): + result = core.analyze_run_data(sample_run_data) + + assert isinstance(result, dict), "Result should be a dictionary" + assert "total_count" in result + assert "negative_dt_count" in result + assert "negative_corr_ids" in result + + assert result["total_count"] == len(sample_run_data) + assert result["negative_dt_count"] == 2, "Should detect exactly two negative cases" + assert set(result["negative_corr_ids"]) == {"a1", "c3"} + + # optional mean values should be numbers or None + for key in ["mean_expires_at_dist_hours_neg", "mean_visibility_lag_neg"]: + val = result.get(key) + assert val is None or isinstance(val, (int, float)) + + +def test_analyze_run_data_no_negatives(): + data = [ + {"corr_id": "p1", "expires_at_dist_hours": 0.1, "visibility_lag": 0.2}, + {"corr_id": "p2", "expires_at_dist_hours": 5.0, "visibility_lag": 1.0} + ] + result = core.analyze_run_data(data) + assert result["negative_dt_count"] == 0 + assert result["negative_corr_ids"] == [] + + +def test_analyze_run_data_invalid_input(): + # Missing field should trigger validation error + invalid_data = [{"corr_id": "z1", "expires_at_dist_hours": 1.0}] + with pytest.raises((KeyError, AssertionError, TypeError)): + core.analyze_run_data(invalid_data) + + +def test_analyze_run_data_from_json_file(tmp_path): + data_path = tmp_path / "test_run_data.json" + input_data = [ + {"corr_id": "x1", "expires_at_dist_hours": 2.0, "visibility_lag": -1.0}, + {"corr_id": "x2", "expires_at_dist_hours": 3.0, "visibility_lag": 2.0} + ] + data_path.write_text(json.dumps(input_data)) + + run_data = json.loads(data_path.read_text()) + result = core.analyze_run_data(run_data) + + assert result["negative_dt_count"] == 1 + assert result["negative_corr_ids"] == ["x1"] \ No newline at end of file