From 34e3841c4b745cc173aee1402aea3ed0a5aaacac Mon Sep 17 00:00:00 2001 From: Mika Date: Mon, 2 Mar 2026 14:26:31 +0000 Subject: [PATCH] Add artifact-1/tests/test_main.py --- artifact-1/tests/test_main.py | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 artifact-1/tests/test_main.py diff --git a/artifact-1/tests/test_main.py b/artifact-1/tests/test_main.py new file mode 100644 index 0000000..65359fc --- /dev/null +++ b/artifact-1/tests/test_main.py @@ -0,0 +1,67 @@ +import json +import pytest +from pathlib import Path +import pandas as pd + +import src.artifact_1.main as main + + +def load_test_data(): + test_data_path = Path(__file__).parent / 'data' / 'test_timing_data.json' + if not test_data_path.exists(): + # fallback synthetic data + return [ + {"expires_at_dist_hours": 48.0, "delta_t": 1.2, "pinned": True, "unpinned": False}, + {"expires_at_dist_hours": 12.0, "delta_t": -0.5, "pinned": False, "unpinned": True}, + {"expires_at_dist_hours": 5.0, "delta_t": 0.3, "pinned": False, "unpinned": True} + ] + with open(test_data_path, encoding='utf-8') as f: + return json.load(f) + + +@pytest.fixture(scope='module') +def sample_data(): + return load_test_data() + + +def test_analyze_timing_data_basic(sample_data): + result = main.analyze_timing_data(sample_data) + assert isinstance(result, dict) + assert 'negative_dt_count' in result + assert 'total_count' in result + assert result['total_count'] == len(sample_data) + assert result['negative_dt_count'] <= result['total_count'] + + +def test_analyze_timing_data_near_expiry_threshold(sample_data): + result = main.analyze_timing_data(sample_data) + threshold = result.get('recommended_near_expiry_threshold') + assert isinstance(threshold, (int, float)) + assert 0 <= threshold <= 72 + + +def test_analyze_timing_data_invalid_input(): + # wrong type + with pytest.raises((ValueError, TypeError)): + main.analyze_timing_data('not a list') + # missing keys + bad_data = [{"delta_t": 1.0}] + with pytest.raises((KeyError, ValueError)): + main.analyze_timing_data(bad_data) + + +def test_analyze_timing_data_pandas_consistency(sample_data): + # Ensure that conversion to pandas does not break the structure assumptions + df = pd.DataFrame(sample_data) + assert all(col in df.columns for col in ['expires_at_dist_hours', 'delta_t']) + result = main.analyze_timing_data(sample_data) + assert isinstance(result.get('mean_delta_t'), (float, int)) + + +def test_analyze_timing_data_negative_dt_flag(sample_data): + has_negative = any(d['delta_t'] < 0 for d in sample_data) + result = main.analyze_timing_data(sample_data) + if has_negative: + assert result['negative_dt_count'] > 0 + else: + assert result['negative_dt_count'] == 0 \ No newline at end of file