Add trace_agg/tests/test_core.py
This commit is contained in:
parent
4d325f19d8
commit
bcfe946ef2
1 changed files with 63 additions and 0 deletions
63
trace_agg/tests/test_core.py
Normal file
63
trace_agg/tests/test_core.py
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import json
|
||||||
|
import pytest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import src.trace_agg.core as core
|
||||||
|
|
||||||
|
# --- Fixtures ---
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_trace_data(tmp_path):
|
||||||
|
data = {
|
||||||
|
"correlation_id": "corr01",
|
||||||
|
"events": [
|
||||||
|
{"had_sched_switch": 1, "had_irq": 0, "had_softirq": 0, "cpu_ids_seen": [0]},
|
||||||
|
{"had_sched_switch": 0, "had_irq": 1, "had_softirq": 0, "cpu_ids_seen": [1]},
|
||||||
|
{"had_sched_switch": 0, "had_irq": 0, "had_softirq": 1, "cpu_ids_seen": [2]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
sample_path = tmp_path / "sample_trace.json"
|
||||||
|
with open(sample_path, "w", encoding="utf-8") as fh:
|
||||||
|
json.dump(data, fh)
|
||||||
|
return sample_path
|
||||||
|
|
||||||
|
# --- Tests ---
|
||||||
|
def test_aggregate_trace_data_nominal(sample_trace_data, monkeypatch):
|
||||||
|
def mock_open_file(path: str):
|
||||||
|
with open(path, "r", encoding="utf-8") as fh:
|
||||||
|
return json.load(fh)
|
||||||
|
|
||||||
|
monkeypatch.setattr(core, "open", open)
|
||||||
|
result = core.aggregate_trace_data("corr01")
|
||||||
|
assert isinstance(result, dict)
|
||||||
|
assert "had_sched_switch" in result
|
||||||
|
assert "had_irq" in result
|
||||||
|
assert "had_softirq" in result
|
||||||
|
assert "cpu_ids_seen" in result
|
||||||
|
# Ensure aggregation logic makes sense
|
||||||
|
assert result["had_sched_switch"] in (0, 1)
|
||||||
|
assert result["had_irq"] in (0, 1)
|
||||||
|
assert result["had_softirq"] in (0, 1)
|
||||||
|
assert all(isinstance(cpu, int) for cpu in result["cpu_ids_seen"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_aggregate_trace_data_edge_case_empty(monkeypatch):
|
||||||
|
def mock_open_file(path: str):
|
||||||
|
return {"correlation_id": "empty_case", "events": []}
|
||||||
|
|
||||||
|
monkeypatch.setattr(core, "open", lambda path, *_args, **_kwargs: open(__file__, "r")) # Dummy but unused
|
||||||
|
|
||||||
|
# Monkeypatch json.load since we're not actually reading
|
||||||
|
monkeypatch.setattr(json, "load", lambda fh: {"correlation_id": "empty_case", "events": []})
|
||||||
|
|
||||||
|
result = core.aggregate_trace_data("empty_case")
|
||||||
|
assert result["had_sched_switch"] == 0
|
||||||
|
assert result["had_irq"] == 0
|
||||||
|
assert result["had_softirq"] == 0
|
||||||
|
assert result["cpu_ids_seen"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_aggregate_trace_data_invalid_input(monkeypatch):
|
||||||
|
monkeypatch.setattr(json, "load", lambda fh: {"correlation_id": "bad", "events": [{"had_sched_switch": "wrong"}]})
|
||||||
|
|
||||||
|
with pytest.raises((ValueError, TypeError)):
|
||||||
|
core.aggregate_trace_data("bad")
|
||||||
Loading…
Reference in a new issue