479 lines
16 KiB
Python
479 lines
16 KiB
Python
"""
|
||||
|
|
Integration tests for testset generation pipeline
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
import json
|
|||
|
|
import tempfile
|
|||
|
|
import shutil
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
from rag_evaluation.testset.generator import TestsetGenerator
|
|||
|
|
from rag_evaluation.testset.document_loader import Document, load_documents
|
|||
|
|
from rag_evaluation.core.config import Config
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def temp_dir():
|
|||
|
|
"""创建临时目录用于测试"""
|
|||
|
|
temp_dir = tempfile.mkdtemp()
|
|||
|
|
yield temp_dir
|
|||
|
|
# 清理临时目录
|
|||
|
|
shutil.rmtree(temp_dir, ignore_errors=True)
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def sample_files(temp_dir):
|
|||
|
|
"""创建示例文件用于测试"""
|
|||
|
|
# 创建不同大小的文件
|
|||
|
|
files = {}
|
|||
|
|
|
|||
|
|
# 小文件 (< 1MB)
|
|||
|
|
small_file = Path(temp_dir) / "small.txt"
|
|||
|
|
small_file.write_text("这是一个小型测试文件。它包含了一些基础的内容。" * 10, encoding='utf-8')
|
|||
|
|
files['small'] = str(small_file)
|
|||
|
|
|
|||
|
|
# 中等文件 (1-10MB 模拟)
|
|||
|
|
medium_file = Path(temp_dir) / "medium.txt"
|
|||
|
|
medium_content = "这是一个中等大小的文件。" * 5000 # 模拟较大内容
|
|||
|
|
medium_file.write_text(medium_content, encoding='utf-8')
|
|||
|
|
files['medium'] = str(medium_file)
|
|||
|
|
|
|||
|
|
# 大型文件 (> 10MB 模拟)
|
|||
|
|
large_file = Path(temp_dir) / "large.txt"
|
|||
|
|
large_content = "这是一个大型文件的示例内容。" * 20000 # 模拟很大内容
|
|||
|
|
large_file.write_text(large_content, encoding='utf-8')
|
|||
|
|
files['large'] = str(large_file)
|
|||
|
|
|
|||
|
|
# Markdown 文件
|
|||
|
|
md_file = Path(temp_dir) / "doc.md"
|
|||
|
|
md_content = """
|
|||
|
|
# 测试文档
|
|||
|
|
|
|||
|
|
## 简介
|
|||
|
|
这是一个测试文档,用于验证文档加载功能。
|
|||
|
|
|
|||
|
|
## 主要内容
|
|||
|
|
- 第一项
|
|||
|
|
- 第二项
|
|||
|
|
- 第三项
|
|||
|
|
|
|||
|
|
## 详细说明
|
|||
|
|
这里包含了一些详细的说明内容,用于测试文档处理功能。
|
|||
|
|
""" * 20
|
|||
|
|
md_file.write_text(md_content, encoding='utf-8')
|
|||
|
|
files['markdown'] = str(md_file)
|
|||
|
|
|
|||
|
|
return files
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def test_config(temp_dir):
|
|||
|
|
"""创建测试配置"""
|
|||
|
|
config = Config()
|
|||
|
|
# 更新存储路径为临时目录
|
|||
|
|
config.storage.testset_dir = str(Path(temp_dir) / "testsets")
|
|||
|
|
config.storage.report_dir = str(Path(temp_dir) / "reports")
|
|||
|
|
config.storage.cache_dir = str(Path(temp_dir) / "cache")
|
|||
|
|
|
|||
|
|
# 设置较小的测试集大小
|
|||
|
|
config.testset.default_size = 10
|
|||
|
|
|
|||
|
|
return config
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestDocumentLoading:
|
|||
|
|
"""测试文档加载流程"""
|
|||
|
|
|
|||
|
|
def test_load_single_text_file(self, sample_files):
|
|||
|
|
"""测试加载单个文本文件"""
|
|||
|
|
documents = load_documents([sample_files['small']])
|
|||
|
|
|
|||
|
|
assert len(documents) == 1
|
|||
|
|
assert documents[0].page_content
|
|||
|
|
assert 'source' in documents[0].metadata
|
|||
|
|
assert 'file_size' in documents[0].metadata
|
|||
|
|
|
|||
|
|
def test_load_multiple_files(self, sample_files):
|
|||
|
|
"""测试加载多个文件"""
|
|||
|
|
file_paths = [sample_files['small'], sample_files['medium'], sample_files['markdown']]
|
|||
|
|
documents = load_documents(file_paths)
|
|||
|
|
|
|||
|
|
assert len(documents) == 3
|
|||
|
|
for doc in documents:
|
|||
|
|
assert doc.page_content
|
|||
|
|
assert 'source' in doc.metadata
|
|||
|
|
|
|||
|
|
def test_document_metadata(self, sample_files):
|
|||
|
|
"""测试文档元数据"""
|
|||
|
|
documents = load_documents([sample_files['small']])
|
|||
|
|
|
|||
|
|
doc = documents[0]
|
|||
|
|
assert 'source' in doc.metadata
|
|||
|
|
assert 'file_size' in doc.metadata
|
|||
|
|
assert 'file_type' in doc.metadata
|
|||
|
|
assert doc.metadata['file_type'] == 'text'
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestSamplingStrategy:
|
|||
|
|
"""测试采样策略"""
|
|||
|
|
|
|||
|
|
def test_small_file_full_sampling(self, test_config, sample_files):
|
|||
|
|
"""测试小文件完全采样"""
|
|||
|
|
from rag_evaluation.testset.sampling import SamplingStrategy
|
|||
|
|
|
|||
|
|
strategy = SamplingStrategy(test_config.testset)
|
|||
|
|
documents = load_documents([sample_files['small']])
|
|||
|
|
|
|||
|
|
sampled = strategy.sample_documents(documents)
|
|||
|
|
|
|||
|
|
# 小文件应该完全包含
|
|||
|
|
assert len(sampled) == 1
|
|||
|
|
|
|||
|
|
def test_mixed_size_sampling(self, test_config, sample_files):
|
|||
|
|
"""测试混合大小文件采样"""
|
|||
|
|
from rag_evaluation.testset.sampling import SamplingStrategy
|
|||
|
|
|
|||
|
|
strategy = SamplingStrategy(test_config.testset)
|
|||
|
|
file_paths = [sample_files['small'], sample_files['medium'], sample_files['large']]
|
|||
|
|
documents = load_documents(file_paths)
|
|||
|
|
|
|||
|
|
sampled = strategy.sample_documents(documents)
|
|||
|
|
|
|||
|
|
# 应该至少包含小文件
|
|||
|
|
assert len(sampled) >= 1
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestDocumentChunking:
|
|||
|
|
"""测试文档分块"""
|
|||
|
|
|
|||
|
|
def test_chunk_small_document(self, test_config):
|
|||
|
|
"""测试分块小文档"""
|
|||
|
|
from rag_evaluation.testset.sampling import DocumentChunker
|
|||
|
|
|
|||
|
|
chunker = DocumentChunker(chunk_size=100, chunk_overlap=20)
|
|||
|
|
doc = Document(content="这是一个短文档。", metadata={'source': 'test.txt'})
|
|||
|
|
|
|||
|
|
chunks = chunker.chunk_document(doc)
|
|||
|
|
|
|||
|
|
# 短文档应该只有一个块
|
|||
|
|
assert len(chunks) >= 1
|
|||
|
|
|
|||
|
|
def test_chunk_large_document(self, test_config):
|
|||
|
|
"""测试分块大文档"""
|
|||
|
|
from rag_evaluation.testset.sampling import DocumentChunker
|
|||
|
|
|
|||
|
|
chunker = DocumentChunker(chunk_size=100, chunk_overlap=20)
|
|||
|
|
large_content = "测试内容。" * 100
|
|||
|
|
doc = Document(content=large_content, metadata={'source': 'test.txt'})
|
|||
|
|
|
|||
|
|
chunks = chunker.chunk_document(doc)
|
|||
|
|
|
|||
|
|
# 大文档应该分成多个块
|
|||
|
|
assert len(chunks) >= 2
|
|||
|
|
|
|||
|
|
def test_chunk_preserves_metadata(self, test_config):
|
|||
|
|
"""测试分块保留元数据"""
|
|||
|
|
from rag_evaluation.testset.sampling import DocumentChunker
|
|||
|
|
|
|||
|
|
chunker = DocumentChunker(chunk_size=100, chunk_overlap=20)
|
|||
|
|
doc = Document(
|
|||
|
|
content="测试内容。" * 50,
|
|||
|
|
metadata={'source': 'test.txt', 'author': 'Test Author'}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
chunks = chunker.chunk_document(doc)
|
|||
|
|
|
|||
|
|
for chunk in chunks:
|
|||
|
|
assert 'source' in chunk.metadata
|
|||
|
|
assert chunk.metadata['source'] == 'test.txt'
|
|||
|
|
assert 'author' in chunk.metadata
|
|||
|
|
assert 'chunk_index' in chunk.metadata
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestTestsetGeneration:
|
|||
|
|
"""测试测评集生成流程"""
|
|||
|
|
|
|||
|
|
def test_generator_initialization(self, test_config):
|
|||
|
|
"""测试生成器初始化"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
assert generator.config is not None
|
|||
|
|
assert generator.sampling_strategy is not None
|
|||
|
|
assert generator.chunker is not None
|
|||
|
|
|
|||
|
|
def test_generate_testset_structure(self, test_config, sample_files):
|
|||
|
|
"""测试生成测评集结构"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small'], sample_files['markdown']],
|
|||
|
|
test_size=5,
|
|||
|
|
name="测试集"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 验证测评集结构
|
|||
|
|
assert 'id' in testset
|
|||
|
|
assert 'knowledge_base_id' in testset
|
|||
|
|
assert testset['knowledge_base_id'] == "kb-test-001"
|
|||
|
|
assert 'name' in testset
|
|||
|
|
assert testset['name'] == "测试集"
|
|||
|
|
assert 'created_at' in testset
|
|||
|
|
assert 'questions' in testset
|
|||
|
|
|
|||
|
|
def test_testset_questions_count(self, test_config, sample_files):
|
|||
|
|
"""测试测评集问题数量"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
test_size = 10
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=test_size
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 验证问题数量
|
|||
|
|
assert len(testset['questions']) == test_size
|
|||
|
|
|
|||
|
|
def test_testset_question_structure(self, test_config, sample_files):
|
|||
|
|
"""测试问题结构"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=3
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 验证每个问题的结构
|
|||
|
|
for i, question in enumerate(testset['questions']):
|
|||
|
|
assert 'index' in question
|
|||
|
|
assert question['index'] == i
|
|||
|
|
assert 'question' in question
|
|||
|
|
assert 'type' in question
|
|||
|
|
assert question['type'] in ['simple', 'reasoning', 'multi_context']
|
|||
|
|
assert 'ground_truth' in question
|
|||
|
|
assert 'answer' in question['ground_truth']
|
|||
|
|
assert 'context' in question['ground_truth']
|
|||
|
|
assert 'metadata' in question
|
|||
|
|
|
|||
|
|
def test_testset_metadata(self, test_config, sample_files):
|
|||
|
|
"""测试测评集元数据"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=5
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 验证元数据
|
|||
|
|
metadata = testset['metadata']
|
|||
|
|
assert 'test_size' in metadata
|
|||
|
|
assert 'question_distribution' in metadata
|
|||
|
|
assert 'sampling_strategy' in metadata
|
|||
|
|
assert 'document_count' in metadata
|
|||
|
|
|
|||
|
|
def test_question_type_distribution(self, test_config, sample_files):
|
|||
|
|
"""测试问题类型分布"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=10
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 统计问题类型
|
|||
|
|
type_counts = {}
|
|||
|
|
for question in testset['questions']:
|
|||
|
|
qtype = question['type']
|
|||
|
|
type_counts[qtype] = type_counts.get(qtype, 0) + 1
|
|||
|
|
|
|||
|
|
# 验证分布符合配置
|
|||
|
|
distribution = test_config.testset.question_distribution
|
|||
|
|
expected_simple = int(10 * distribution['simple'])
|
|||
|
|
expected_reasoning = int(10 * distribution['reasoning'])
|
|||
|
|
|
|||
|
|
assert type_counts.get('simple', 0) == expected_simple
|
|||
|
|
assert type_counts.get('reasoning', 0) == expected_reasoning
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestTestsetPersistence:
|
|||
|
|
"""测试测评集持久化"""
|
|||
|
|
|
|||
|
|
def test_save_testset(self, test_config, sample_files, temp_dir):
|
|||
|
|
"""测试保存测评集"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=5,
|
|||
|
|
name="持久化测试集"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 保存测评集
|
|||
|
|
output_dir = Path(temp_dir) / "testsets_output"
|
|||
|
|
file_path = generator.save_testset(testset, str(output_dir))
|
|||
|
|
|
|||
|
|
# 验证文件存在
|
|||
|
|
assert Path(file_path).exists()
|
|||
|
|
|
|||
|
|
# 验证文件内容
|
|||
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|||
|
|
saved_data = json.load(f)
|
|||
|
|
|
|||
|
|
assert saved_data['id'] == testset['id']
|
|||
|
|
assert saved_data['name'] == "持久化测试集"
|
|||
|
|
assert len(saved_data['questions']) == 5
|
|||
|
|
|
|||
|
|
def test_load_testset(self, test_config, sample_files, temp_dir):
|
|||
|
|
"""测试加载测评集"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
# 生成并保存测评集
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=5
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
output_dir = Path(temp_dir) / "testsets_output"
|
|||
|
|
file_path = generator.save_testset(testset, str(output_dir))
|
|||
|
|
|
|||
|
|
# 加载测评集
|
|||
|
|
loaded_testset = generator.load_testset(testset['id'], str(output_dir))
|
|||
|
|
|
|||
|
|
# 验证加载的数据
|
|||
|
|
assert loaded_testset['id'] == testset['id']
|
|||
|
|
assert loaded_testset['knowledge_base_id'] == testset['knowledge_base_id']
|
|||
|
|
assert len(loaded_testset['questions']) == len(testset['questions'])
|
|||
|
|
|
|||
|
|
def test_save_creates_directory(self, test_config, sample_files, temp_dir):
|
|||
|
|
"""测试保存时创建目录"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-test-001",
|
|||
|
|
file_paths=[sample_files['small']],
|
|||
|
|
test_size=3
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 使用不存在的目录
|
|||
|
|
output_dir = Path(temp_dir) / "new_dir" / "testsets"
|
|||
|
|
file_path = generator.save_testset(testset, str(output_dir))
|
|||
|
|
|
|||
|
|
# 验证目录被创建
|
|||
|
|
assert Path(file_path).parent.exists()
|
|||
|
|
assert Path(file_path).exists()
|
|||
|
|
|
|||
|
|
def test_load_nonexistent_testset(self, test_config, temp_dir):
|
|||
|
|
"""测试加载不存在的测评集"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
with pytest.raises(FileNotFoundError):
|
|||
|
|
generator.load_testset("nonexistent-id", str(Path(temp_dir) / "testsets"))
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestEndToEndGeneration:
|
|||
|
|
"""端到端测试:完整的测评集生成流程"""
|
|||
|
|
|
|||
|
|
def test_full_pipeline(self, test_config, sample_files):
|
|||
|
|
"""测试完整的生成流程"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
# 1. 生成测评集
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-e2e-test",
|
|||
|
|
file_paths=[sample_files['small'], sample_files['medium'], sample_files['markdown']],
|
|||
|
|
test_size=10,
|
|||
|
|
name="端到端测试集"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 2. 验证测评集
|
|||
|
|
assert testset['id']
|
|||
|
|
assert testset['knowledge_base_id'] == "kb-e2e-test"
|
|||
|
|
assert len(testset['questions']) == 10
|
|||
|
|
|
|||
|
|
# 3. 保存测评集
|
|||
|
|
file_path = generator.save_testset(testset)
|
|||
|
|
|
|||
|
|
# 4. 验证文件
|
|||
|
|
assert Path(file_path).exists()
|
|||
|
|
|
|||
|
|
# 5. 加载测评集
|
|||
|
|
loaded_testset = generator.load_testset(testset['id'])
|
|||
|
|
|
|||
|
|
# 6. 验证加载的数据完整性
|
|||
|
|
assert loaded_testset['id'] == testset['id']
|
|||
|
|
assert len(loaded_testset['questions']) == len(testset['questions'])
|
|||
|
|
|
|||
|
|
# 7. 验证所有问题都有正确的结构
|
|||
|
|
for question in loaded_testset['questions']:
|
|||
|
|
assert 'question' in question
|
|||
|
|
assert 'type' in question
|
|||
|
|
assert 'ground_truth' in question
|
|||
|
|
assert 'metadata' in question
|
|||
|
|
|
|||
|
|
def test_pipeline_with_large_fileset(self, test_config, sample_files):
|
|||
|
|
"""测试处理大文件集的流程"""
|
|||
|
|
generator = TestsetGenerator(test_config)
|
|||
|
|
|
|||
|
|
# 包含所有测试文件
|
|||
|
|
file_paths = list(sample_files.values())
|
|||
|
|
|
|||
|
|
testset = generator.generate(
|
|||
|
|
knowledge_base_id="kb-large-test",
|
|||
|
|
file_paths=file_paths,
|
|||
|
|
test_size=15
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 验证生成成功
|
|||
|
|
assert testset['id']
|
|||
|
|
assert len(testset['questions']) == 15
|
|||
|
|
|
|||
|
|
# 验证问题类型多样性
|
|||
|
|
types = set(q['type'] for q in testset['questions'])
|
|||
|
|
assert len(types) > 1 # 至少有2种类型
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestConfigValidation:
|
|||
|
|
"""测试配置验证"""
|
|||
|
|
|
|||
|
|
def test_config_question_distribution_validation(self):
|
|||
|
|
"""测试问题分布配置验证"""
|
|||
|
|
config = Config()
|
|||
|
|
|
|||
|
|
# 有效的分布
|
|||
|
|
config.testset.question_distribution = {
|
|||
|
|
'simple': 0.5,
|
|||
|
|
'reasoning': 0.3,
|
|||
|
|
'multi_context': 0.2
|
|||
|
|
}
|
|||
|
|
config.testset.validate() # 应该不抛出异常
|
|||
|
|
|
|||
|
|
# 无效的分布(和不等于1)
|
|||
|
|
config.testset.question_distribution = {
|
|||
|
|
'simple': 0.5,
|
|||
|
|
'reasoning': 0.3,
|
|||
|
|
'multi_context': 0.1 # 和为0.9
|
|||
|
|
}
|
|||
|
|
with pytest.raises(ValueError):
|
|||
|
|
config.testset.validate()
|
|||
|
|
|
|||
|
|
def test_config_sampling_rate_validation(self):
|
|||
|
|
"""测试采样率配置验证"""
|
|||
|
|
config = Config()
|
|||
|
|
|
|||
|
|
# 有效的采样率
|
|||
|
|
config.testset.sampling_strategy['small_file_sampling_rate'] = 1.0
|
|||
|
|
config.testset.sampling_strategy['medium_file_sampling_rate'] = 0.5
|
|||
|
|
config.testset.sampling_strategy['large_file_sampling_rate'] = 0.2
|
|||
|
|
config.testset.validate() # 应该不抛出异常
|
|||
|
|
|
|||
|
|
# 无效的采样率(>1)
|
|||
|
|
config.testset.sampling_strategy['small_file_sampling_rate'] = 1.5
|
|||
|
|
with pytest.raises(ValueError):
|
|||
|
|
config.testset.validate()
|