Add artifact-1/tests/test_main.py
This commit is contained in:
parent
0ce3032a87
commit
34e3841c4b
1 changed files with 67 additions and 0 deletions
67
artifact-1/tests/test_main.py
Normal file
67
artifact-1/tests/test_main.py
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue