Initial commit
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
4ecf7b351d
143 files changed
+24156
No files matched your search
@@ -0,0 +1,236 @@
|
||||
"""
|
||||
Tests for report visualizer
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
from rag_evaluation.report.aggregator import EvaluationSummary, MetricStats
|
||||
from rag_evaluation.report.visualizer import ReportVisualizer
|
||||
|
||||
|
||||
class TestReportVisualizer(unittest.TestCase):
|
||||
"""测试报告可视化器"""
|
||||
|
||||
def setUp(self):
|
||||
"""设置测试环境"""
|
||||
try:
|
||||
self.visualizer = ReportVisualizer()
|
||||
self.plotly_available = True
|
||||
except ImportError:
|
||||
self.plotly_available = False
|
||||
self.skipTest("Plotly not installed")
|
||||
|
||||
# 创建测试数据
|
||||
self.summary = EvaluationSummary(
|
||||
evaluation_id="eval-001",
|
||||
knowledge_base_id="kb-001",
|
||||
testset_id="ts-001",
|
||||
total_questions=10,
|
||||
successful_questions=8,
|
||||
failed_questions=2,
|
||||
overall_score=75.5,
|
||||
metric_stats={
|
||||
'context_recall': MetricStats(
|
||||
name='context_recall',
|
||||
mean=0.85,
|
||||
median=0.87,
|
||||
std_dev=0.12,
|
||||
min=0.60,
|
||||
max=0.98,
|
||||
count=8,
|
||||
pass_rate=0.75
|
||||
),
|
||||
'faithfulness': MetricStats(
|
||||
name='faithfulness',
|
||||
mean=0.78,
|
||||
median=0.80,
|
||||
std_dev=0.15,
|
||||
min=0.50,
|
||||
max=0.95,
|
||||
count=8,
|
||||
pass_rate=0.625
|
||||
)
|
||||
},
|
||||
avg_retrieval_time_ms=150.5,
|
||||
avg_generation_time_ms=800.3,
|
||||
created_at=datetime.now()
|
||||
)
|
||||
|
||||
self.results = [
|
||||
{
|
||||
'question_index': i,
|
||||
'question_text': f'测试问题 {i}',
|
||||
'metrics': {
|
||||
'context_recall': 0.85,
|
||||
'faithfulness': 0.78
|
||||
},
|
||||
'retrieval_time_ms': 150,
|
||||
'generation_time_ms': 800
|
||||
}
|
||||
for i in range(8)
|
||||
] + [
|
||||
{
|
||||
'question_index': 8,
|
||||
'question_text': '失败问题 1',
|
||||
'metrics': None,
|
||||
'error': 'Evaluation failed'
|
||||
},
|
||||
{
|
||||
'question_index': 9,
|
||||
'question_text': '失败问题 2',
|
||||
'metrics': {
|
||||
'context_recall': 0.3,
|
||||
'faithfulness': 0.4
|
||||
},
|
||||
'retrieval_time_ms': 200,
|
||||
'generation_time_ms': 900
|
||||
}
|
||||
]
|
||||
|
||||
def test_generate_html_report(self):
|
||||
"""测试生成HTML报告"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
output_path = os.path.join(tmpdir, 'report.html')
|
||||
|
||||
self.visualizer.generate_html_report(
|
||||
self.summary,
|
||||
self.results,
|
||||
output_path,
|
||||
include_charts=True
|
||||
)
|
||||
|
||||
# 验证文件已创建
|
||||
self.assertTrue(os.path.exists(output_path))
|
||||
|
||||
# 验证文件内容
|
||||
with open(output_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 检查关键内容
|
||||
self.assertIn('RAG评估报告', content)
|
||||
self.assertIn('eval-001', content)
|
||||
self.assertIn('整体评分', content)
|
||||
self.assertIn('75.5', content)
|
||||
self.assertIn('指标雷达图', content)
|
||||
self.assertIn('Plotly', content)
|
||||
|
||||
def test_generate_html_report_without_charts(self):
|
||||
"""测试生成不含图表的HTML报告"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
output_path = os.path.join(tmpdir, 'report_no_charts.html')
|
||||
|
||||
self.visualizer.generate_html_report(
|
||||
self.summary,
|
||||
self.results,
|
||||
output_path,
|
||||
include_charts=False
|
||||
)
|
||||
|
||||
# 验证文件已创建
|
||||
self.assertTrue(os.path.exists(output_path))
|
||||
|
||||
# 验证文件内容
|
||||
with open(output_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 应该包含摘要但不包含图表
|
||||
self.assertIn('RAG评估报告', content)
|
||||
self.assertNotIn('radar-chart', content)
|
||||
|
||||
def test_create_trend_chart(self):
|
||||
"""测试创建趋势图"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
history_data = [
|
||||
{
|
||||
'timestamp': '2024-01-01',
|
||||
'metrics': {
|
||||
'context_recall': 0.75,
|
||||
'faithfulness': 0.70
|
||||
}
|
||||
},
|
||||
{
|
||||
'timestamp': '2024-01-02',
|
||||
'metrics': {
|
||||
'context_recall': 0.80,
|
||||
'faithfulness': 0.75
|
||||
}
|
||||
},
|
||||
{
|
||||
'timestamp': '2024-01-03',
|
||||
'metrics': {
|
||||
'context_recall': 0.85,
|
||||
'faithfulness': 0.78
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
output_path = os.path.join(tmpdir, 'trend.html')
|
||||
|
||||
self.visualizer.create_trend_chart(history_data, output_path)
|
||||
|
||||
# 验证文件已创建
|
||||
self.assertTrue(os.path.exists(output_path))
|
||||
|
||||
# 验证文件内容
|
||||
with open(output_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
self.assertIn('评估指标历史趋势', content)
|
||||
self.assertIn('Plotly', content)
|
||||
|
||||
def test_create_trend_chart_empty_data(self):
|
||||
"""测试空数据创建趋势图"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
output_path = os.path.join(tmpdir, 'trend_empty.html')
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
self.visualizer.create_trend_chart([], output_path)
|
||||
|
||||
def test_translate_metric_name(self):
|
||||
"""测试指标名称翻译"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
self.assertEqual(
|
||||
self.visualizer._translate_metric_name('context_recall'),
|
||||
'上下文召回率'
|
||||
)
|
||||
self.assertEqual(
|
||||
self.visualizer._translate_metric_name('faithfulness'),
|
||||
'忠实度'
|
||||
)
|
||||
self.assertEqual(
|
||||
self.visualizer._translate_metric_name('unknown_metric'),
|
||||
'unknown_metric'
|
||||
)
|
||||
|
||||
def test_score_to_grade(self):
|
||||
"""测试分数转等级"""
|
||||
if not self.plotly_available:
|
||||
return
|
||||
|
||||
self.assertEqual(self.visualizer._score_to_grade(95), 'A (优秀)')
|
||||
self.assertEqual(self.visualizer._score_to_grade(85), 'B (良好)')
|
||||
self.assertEqual(self.visualizer._score_to_grade(75), 'C (中等)')
|
||||
self.assertEqual(self.visualizer._score_to_grade(65), 'D (及格)')
|
||||
self.assertEqual(self.visualizer._score_to_grade(55), 'F (不及格)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user