Add report_generation/tests/test_core.py
This commit is contained in:
parent
b5678d9932
commit
6dfe3af5a7
1 changed files with 75 additions and 0 deletions
75
report_generation/tests/test_core.py
Normal file
75
report_generation/tests/test_core.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
import src.report_generation.core as core
|
||||
|
||||
@pytest.fixture
|
||||
def sample_analysis_results():
|
||||
data = [
|
||||
{
|
||||
"run_id": "run_34",
|
||||
"step_id": 1,
|
||||
"epoch_ms": 1700000000000,
|
||||
"monotonic_ns": 100000000,
|
||||
"tz_offset_minutes": 0,
|
||||
"retry_tail_p99": 12.5,
|
||||
"band_width": 100.0
|
||||
},
|
||||
{
|
||||
"run_id": "run_35",
|
||||
"step_id": 1,
|
||||
"epoch_ms": 1700000001000,
|
||||
"monotonic_ns": 200000000,
|
||||
"tz_offset_minutes": 0,
|
||||
"retry_tail_p99": 10.0,
|
||||
"band_width": 110.0
|
||||
},
|
||||
{
|
||||
"run_id": "run_36",
|
||||
"step_id": 1,
|
||||
"epoch_ms": 1700000002000,
|
||||
"monotonic_ns": 300000000,
|
||||
"tz_offset_minutes": 0,
|
||||
"retry_tail_p99": 11.0,
|
||||
"band_width": 95.0
|
||||
}
|
||||
]
|
||||
return data
|
||||
|
||||
|
||||
def test_generate_report_creates_json_file(tmp_path, sample_analysis_results):
|
||||
output_file = tmp_path / "stability_report.json"
|
||||
|
||||
df = pd.DataFrame(sample_analysis_results)
|
||||
result_path = core.generate_report(df)
|
||||
|
||||
assert isinstance(result_path, str), "Return value must be a string path."
|
||||
assert os.path.exists(result_path), "Output file should be created."
|
||||
with open(result_path, 'r', encoding='utf-8') as f:
|
||||
report = json.load(f)
|
||||
|
||||
assert isinstance(report, dict), "Report must be a dictionary."
|
||||
assert set(["config_summary", "findings", "metric_comparison", "timestamp"]).issubset(report.keys())
|
||||
assert isinstance(report["metric_comparison"], dict)
|
||||
assert isinstance(report["findings"], str)
|
||||
|
||||
|
||||
def test_generate_report_handles_dict_input(tmp_path, sample_analysis_results):
|
||||
data_dict = {"results": sample_analysis_results}
|
||||
result_path = core.generate_report(data_dict)
|
||||
|
||||
assert isinstance(result_path, str)
|
||||
assert Path(result_path).exists()
|
||||
|
||||
with open(result_path, 'r', encoding='utf-8') as f:
|
||||
parsed = json.load(f)
|
||||
|
||||
assert "metric_comparison" in parsed
|
||||
|
||||
|
||||
def test_generate_report_invalid_input_type():
|
||||
with pytest.raises((TypeError, ValueError)):
|
||||
core.generate_report("invalid_input")
|
||||
Loading…
Reference in a new issue