Add data_analysis/tests/test_core.py
This commit is contained in:
parent
be443a0586
commit
fa509cf2cf
1 changed files with 58 additions and 0 deletions
58
data_analysis/tests/test_core.py
Normal file
58
data_analysis/tests/test_core.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import pytest
|
||||||
|
import math
|
||||||
|
from data_analysis import core
|
||||||
|
|
||||||
|
|
||||||
|
class RunData:
|
||||||
|
def __init__(self, policy_hash: str, warn_rate: float, unknown_rate: float, delta_time: float):
|
||||||
|
self.policy_hash = policy_hash
|
||||||
|
self.warn_rate = warn_rate
|
||||||
|
self.unknown_rate = unknown_rate
|
||||||
|
self.delta_time = delta_time
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_run_data():
|
||||||
|
return [
|
||||||
|
RunData(policy_hash="abc123", warn_rate=0.1, unknown_rate=0.05, delta_time=1.2),
|
||||||
|
RunData(policy_hash="def456", warn_rate=0.3, unknown_rate=0.10, delta_time=1.8),
|
||||||
|
RunData(policy_hash="ghi789", warn_rate=0.2, unknown_rate=0.02, delta_time=2.0),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_calculate_warn_rate_valid(sample_run_data):
|
||||||
|
result = core.calculate_warn_rate(sample_run_data)
|
||||||
|
expected = (0.1 + 0.3 + 0.2) / 3
|
||||||
|
assert math.isclose(result, expected, rel_tol=1e-9)
|
||||||
|
|
||||||
|
|
||||||
|
def test_calculate_warn_rate_empty():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
core.calculate_warn_rate([])
|
||||||
|
|
||||||
|
|
||||||
|
def test_calculate_warn_rate_malformed():
|
||||||
|
data = [object()]
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
core.calculate_warn_rate(data)
|
||||||
|
|
||||||
|
|
||||||
|
def test_delta_time_distribution_valid(sample_run_data):
|
||||||
|
result = core.delta_time_distribution(sample_run_data)
|
||||||
|
assert isinstance(result, dict)
|
||||||
|
assert set(result.keys()) >= {"mean", "variance"}
|
||||||
|
mean_expected = sum(rd.delta_time for rd in sample_run_data) / 3
|
||||||
|
var_expected = sum((rd.delta_time - mean_expected) ** 2 for rd in sample_run_data) / 3
|
||||||
|
assert math.isclose(result["mean"], mean_expected, rel_tol=1e-9)
|
||||||
|
assert math.isclose(result["variance"], var_expected, rel_tol=1e-9)
|
||||||
|
|
||||||
|
|
||||||
|
def test_delta_time_distribution_empty():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
core.delta_time_distribution([])
|
||||||
|
|
||||||
|
|
||||||
|
def test_delta_time_distribution_malformed():
|
||||||
|
data = [object()]
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
core.delta_time_distribution(data)
|
||||||
Loading…
Reference in a new issue