67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
import json
|
|
import os
|
|
from pathlib import Path
|
|
import pytest
|
|
|
|
from src.report_generation import main
|
|
|
|
# Fixtures
|
|
def sample_analysis_results():
|
|
return [
|
|
{"run_id": 21, "metric_name": "cluster_variance", "value": 0.12, "cluster_score": 0.94},
|
|
{"run_id": 22, "metric_name": "cluster_variance", "value": 0.10, "cluster_score": 0.97},
|
|
{"run_id": 23, "metric_name": "cluster_variance", "value": 0.11, "cluster_score": 0.95}
|
|
]
|
|
|
|
@pytest.fixture
|
|
def analysis_results_file(tmp_path):
|
|
data = sample_analysis_results()
|
|
input_path = tmp_path / "analysis_results.json"
|
|
with open(input_path, "w", encoding="utf-8") as f:
|
|
json.dump(data, f)
|
|
return input_path
|
|
|
|
@pytest.fixture
|
|
def output_file_path(tmp_path):
|
|
return tmp_path / "report_summary.json"
|
|
|
|
# Tests
|
|
def test_generate_report_creates_file(tmp_path, monkeypatch):
|
|
results = sample_analysis_results()
|
|
report_path = main.generate_report(results)
|
|
assert isinstance(report_path, str)
|
|
path_obj = Path(report_path)
|
|
assert path_obj.exists()
|
|
with open(path_obj, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
assert 'summary' in data
|
|
assert 'average_cluster_score' in data['summary']
|
|
assert data['summary']['total_runs'] == 3
|
|
|
|
|
|
def test_generate_report_invalid_input():
|
|
with pytest.raises((TypeError, AssertionError)):
|
|
main.generate_report(None)
|
|
|
|
|
|
def test_generate_report_correct_json_structure(tmp_path):
|
|
results = sample_analysis_results()
|
|
report_path = main.generate_report(results)
|
|
with open(report_path, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
assert isinstance(data, dict)
|
|
summary = data.get('summary', {})
|
|
assert isinstance(summary.get('average_cluster_score'), (float, int))
|
|
assert summary.get('total_runs') == len(results)
|
|
|
|
|
|
def test_generate_report_ci_validation(monkeypatch):
|
|
# CI check: ensure deterministic filename generation and structure
|
|
results = sample_analysis_results()
|
|
report_path = main.generate_report(results)
|
|
second_path = main.generate_report(results)
|
|
assert Path(report_path).exists()
|
|
assert Path(second_path).exists()
|
|
assert report_path != ""
|
|
# The output should be a JSON file with summary content
|
|
assert report_path.endswith('.json')
|