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,446 @@
|
||||
"""
|
||||
Unit tests for testset validator and quality checker
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from rag_evaluation.testset.validator import (
|
||||
TestsetValidator,
|
||||
TestsetValidationError,
|
||||
TestsetQualityChecker
|
||||
)
|
||||
|
||||
|
||||
class TestTestsetValidator:
|
||||
"""Test cases for TestsetValidator"""
|
||||
|
||||
@pytest.fixture
|
||||
def valid_testset(self):
|
||||
"""Create a valid testset for testing"""
|
||||
return {
|
||||
"id": "12345678901234567890123456789012",
|
||||
"biz_seq": "BIZ20240115120000ABC123",
|
||||
"sys_seq": "SYS20240115120000DEF456",
|
||||
"knowledge_base_id": "kb-001",
|
||||
"name": "Test Testset",
|
||||
"created_at": "2024-01-15 12:00:00",
|
||||
"metadata": {
|
||||
"test_size": 2,
|
||||
"question_distribution": {
|
||||
"simple": 0.5,
|
||||
"reasoning": 0.5,
|
||||
"multi_context": 0.0
|
||||
},
|
||||
"sampling_strategy": {
|
||||
"small_file_threshold_mb": 1.0,
|
||||
"medium_file_threshold_mb": 10.0,
|
||||
"small_file_sampling_rate": 1.0,
|
||||
"medium_file_sampling_rate": 0.5,
|
||||
"large_file_sampling_rate": 0.2
|
||||
},
|
||||
"document_count": 5
|
||||
},
|
||||
"questions": [
|
||||
{
|
||||
"index": 0,
|
||||
"question": "What is the capital of France?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "The capital of France is Paris.",
|
||||
"context": ["doc1_chunk1", "doc1_chunk2"]
|
||||
},
|
||||
"metadata": {
|
||||
"source_file": "doc1.txt"
|
||||
}
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"question": "How does the system handle authentication?",
|
||||
"type": "reasoning",
|
||||
"ground_truth": {
|
||||
"answer": "The system uses JWT tokens for authentication.",
|
||||
"context": ["doc2_chunk1"]
|
||||
},
|
||||
"metadata": {
|
||||
"source_file": "doc2.txt"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def validator(self):
|
||||
"""Create validator instance"""
|
||||
return TestsetValidator(strict_mode=False)
|
||||
|
||||
def test_validate_valid_testset(self, validator, valid_testset):
|
||||
"""Test validating a valid testset"""
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert is_valid
|
||||
assert len(errors) == 0
|
||||
|
||||
def test_validate_missing_top_level_field(self, validator, valid_testset):
|
||||
"""Test validation with missing top-level field"""
|
||||
del valid_testset['id']
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('id' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_id_format(self, validator, valid_testset):
|
||||
"""Test validation with invalid ID format"""
|
||||
valid_testset['id'] = "invalid_id"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('ID format' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_biz_seq(self, validator, valid_testset):
|
||||
"""Test validation with invalid biz_seq"""
|
||||
valid_testset['biz_seq'] = "short"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('biz_seq' in error for error in errors)
|
||||
|
||||
def test_validate_empty_knowledge_base_id(self, validator, valid_testset):
|
||||
"""Test validation with empty knowledge_base_id"""
|
||||
valid_testset['knowledge_base_id'] = ""
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('knowledge_base_id' in error for error in errors)
|
||||
|
||||
def test_validate_empty_name(self, validator, valid_testset):
|
||||
"""Test validation with empty name"""
|
||||
valid_testset['name'] = " "
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('name' in error for error in errors)
|
||||
|
||||
def test_validate_missing_metadata(self, validator, valid_testset):
|
||||
"""Test validation with missing metadata"""
|
||||
del valid_testset['metadata']
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('metadata' in error for error in errors)
|
||||
|
||||
def test_validate_metadata_not_dict(self, validator, valid_testset):
|
||||
"""Test validation with metadata as non-dict"""
|
||||
valid_testset['metadata'] = "invalid"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('Metadata must be a dictionary' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_test_size(self, validator, valid_testset):
|
||||
"""Test validation with invalid test_size"""
|
||||
valid_testset['metadata']['test_size'] = -5
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('test_size' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_question_distribution_sum(self, validator, valid_testset):
|
||||
"""Test validation with invalid question distribution sum"""
|
||||
valid_testset['metadata']['question_distribution'] = {
|
||||
"simple": 0.5,
|
||||
"reasoning": 0.3,
|
||||
"multi_context": 0.1 # Sum = 0.9
|
||||
}
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('question_distribution must sum to 1.0' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_question_type_in_distribution(self, validator, valid_testset):
|
||||
"""Test validation with invalid question type in distribution"""
|
||||
valid_testset['metadata']['question_distribution'] = {
|
||||
"simple": 0.5,
|
||||
"invalid_type": 0.3,
|
||||
"multi_context": 0.2
|
||||
}
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('Invalid question type in distribution' in error for error in errors)
|
||||
|
||||
def test_validate_empty_questions_list(self, validator, valid_testset):
|
||||
"""Test validation with empty questions list"""
|
||||
valid_testset['questions'] = []
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('cannot be empty' in error for error in errors)
|
||||
|
||||
def test_validate_questions_not_list(self, validator, valid_testset):
|
||||
"""Test validation with questions as non-list"""
|
||||
valid_testset['questions'] = "not a list"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('must be a list' in error for error in errors)
|
||||
|
||||
def test_validate_question_missing_field(self, validator, valid_testset):
|
||||
"""Test validation with question missing required field"""
|
||||
valid_testset['questions'][0].pop('question')
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('missing required field' in error for error in errors)
|
||||
|
||||
def test_validate_question_too_short(self, validator, valid_testset):
|
||||
"""Test validation with question text too short"""
|
||||
valid_testset['questions'][0]['question'] = "Hi"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('too short' in error for error in errors)
|
||||
|
||||
def test_validate_invalid_question_type(self, validator, valid_testset):
|
||||
"""Test validation with invalid question type"""
|
||||
valid_testset['questions'][0]['type'] = "invalid_type"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('invalid type' in error for error in errors)
|
||||
|
||||
def test_validate_ground_truth_not_dict(self, validator, valid_testset):
|
||||
"""Test validation with ground_truth as non-dict"""
|
||||
valid_testset['questions'][0]['ground_truth'] = "invalid"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('ground_truth must be a dictionary' in error for error in errors)
|
||||
|
||||
def test_validate_empty_answer(self, validator, valid_testset):
|
||||
"""Test validation with empty answer"""
|
||||
valid_testset['questions'][0]['ground_truth']['answer'] = " "
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('must be non-empty string' in error for error in errors)
|
||||
|
||||
def test_validate_context_not_list(self, validator, valid_testset):
|
||||
"""Test validation with context as non-list"""
|
||||
valid_testset['questions'][0]['ground_truth']['context'] = "not a list"
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('must be a list' in error for error in errors)
|
||||
|
||||
def test_validate_inconsistent_question_count(self, validator, valid_testset):
|
||||
"""Test validation with inconsistent question count"""
|
||||
valid_testset['metadata']['test_size'] = 10
|
||||
# Only 2 questions in the list
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('Inconsistent question count' in error for error in errors)
|
||||
|
||||
def test_validate_non_sequential_indices(self, validator, valid_testset):
|
||||
"""Test validation with non-sequential indices"""
|
||||
valid_testset['questions'][0]['index'] = 0
|
||||
valid_testset['questions'][1]['index'] = 5
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('not sequential' in error for error in errors)
|
||||
|
||||
def test_validate_duplicate_questions(self, validator, valid_testset):
|
||||
"""Test validation with duplicate questions"""
|
||||
valid_testset['questions'][1]['question'] = valid_testset['questions'][0]['question']
|
||||
is_valid, errors = validator.validate(valid_testset)
|
||||
assert not is_valid
|
||||
assert any('duplicate' in error.lower() for error in errors)
|
||||
|
||||
def test_validate_strict_mode_raises_exception(self, valid_testset):
|
||||
"""Test that strict mode raises exception"""
|
||||
validator = TestsetValidator(strict_mode=True)
|
||||
valid_testset['id'] = "invalid"
|
||||
|
||||
with pytest.raises(TestsetValidationError):
|
||||
validator.validate(valid_testset)
|
||||
|
||||
|
||||
class TestTestsetQualityChecker:
|
||||
"""Test cases for TestsetQualityChecker"""
|
||||
|
||||
@pytest.fixture
|
||||
def quality_checker(self):
|
||||
"""Create quality checker instance"""
|
||||
return TestsetQualityChecker()
|
||||
|
||||
@pytest.fixture
|
||||
def sample_testset(self):
|
||||
"""Create a sample testset for quality checking"""
|
||||
return {
|
||||
"id": "12345678901234567890123456789012",
|
||||
"name": "Sample Testset",
|
||||
"questions": [
|
||||
{
|
||||
"index": 0,
|
||||
"question": "What is the capital of France?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "Paris is the capital of France.",
|
||||
"context": ["doc1"]
|
||||
},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"question": "How does the authentication system work?",
|
||||
"type": "reasoning",
|
||||
"ground_truth": {
|
||||
"answer": "The system uses JWT tokens for authentication.",
|
||||
"context": ["doc2", "doc3"]
|
||||
},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"index": 2,
|
||||
"question": "What are the steps to reset a password?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "Go to settings, click reset password.",
|
||||
"context": ["doc1"]
|
||||
},
|
||||
"metadata": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def test_quality_check_success(self, quality_checker, sample_testset):
|
||||
"""Test successful quality check"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
assert report['status'] == 'success'
|
||||
assert 'metrics' in report
|
||||
assert 'overall_score' in report
|
||||
|
||||
def test_quality_check_no_questions(self, quality_checker):
|
||||
"""Test quality check with no questions"""
|
||||
testset = {"questions": []}
|
||||
report = quality_checker.check_quality(testset)
|
||||
assert report['status'] == 'error'
|
||||
|
||||
def test_quality_check_type_distribution(self, quality_checker, sample_testset):
|
||||
"""Test type distribution calculation"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
type_dist = report['metrics']['type_distribution']
|
||||
|
||||
assert 'counts' in type_dist
|
||||
assert 'percentages' in type_dist
|
||||
assert type_dist['total'] == 3
|
||||
assert type_dist['counts']['simple'] == 2
|
||||
assert type_dist['counts']['reasoning'] == 1
|
||||
|
||||
def test_quality_check_question_length(self, quality_checker, sample_testset):
|
||||
"""Test question length statistics"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
length_stats = report['metrics']['question_length']
|
||||
|
||||
assert 'min' in length_stats
|
||||
assert 'max' in length_stats
|
||||
assert 'avg' in length_stats
|
||||
assert 'median' in length_stats
|
||||
assert length_stats['min'] > 0
|
||||
|
||||
def test_quality_check_answer_completeness(self, quality_checker, sample_testset):
|
||||
"""Test answer completeness check"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
completeness = report['metrics']['answer_completeness']
|
||||
|
||||
assert 'total' in completeness
|
||||
assert 'complete' in completeness
|
||||
assert 'completeness_rate' in completeness
|
||||
assert completeness['total'] == 3
|
||||
assert completeness['completeness_rate'] > 0
|
||||
|
||||
def test_quality_check_context_coverage(self, quality_checker, sample_testset):
|
||||
"""Test context coverage check"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
coverage = report['metrics']['context_coverage']
|
||||
|
||||
assert 'total' in coverage
|
||||
assert 'with_context' in coverage
|
||||
assert 'coverage_rate' in coverage
|
||||
assert coverage['total'] == 3
|
||||
assert coverage['coverage_rate'] == 1.0 # All have context
|
||||
|
||||
def test_quality_check_diversity_score(self, quality_checker, sample_testset):
|
||||
"""Test diversity score calculation"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
diversity = report['metrics']['diversity_score']
|
||||
|
||||
assert 'total_words' in diversity
|
||||
assert 'unique_words' in diversity
|
||||
assert 'diversity_ratio' in diversity
|
||||
assert diversity['diversity_ratio'] > 0
|
||||
|
||||
def test_quality_check_overall_score(self, quality_checker, sample_testset):
|
||||
"""Test overall score calculation"""
|
||||
report = quality_checker.check_quality(sample_testset)
|
||||
score = report['overall_score']
|
||||
|
||||
assert isinstance(score, float)
|
||||
assert 0 <= score <= 100
|
||||
|
||||
def test_quality_check_generates_recommendations(self, quality_checker, sample_testset):
|
||||
"""Test that recommendations are generated when needed"""
|
||||
# Create testset with issues
|
||||
testset = {
|
||||
"questions": [
|
||||
{
|
||||
"index": i,
|
||||
"question": f"Question {i}",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "Short",
|
||||
"context": []
|
||||
},
|
||||
"metadata": {}
|
||||
}
|
||||
for i in range(10)
|
||||
]
|
||||
}
|
||||
|
||||
report = quality_checker.check_quality(testset)
|
||||
|
||||
# Should have issues and recommendations
|
||||
assert len(report['issues']) > 0
|
||||
assert len(report['recommendations']) > 0
|
||||
|
||||
def test_quality_check_low_completeness(self, quality_checker):
|
||||
"""Test quality check with low answer completeness"""
|
||||
testset = {
|
||||
"questions": [
|
||||
{
|
||||
"index": 0,
|
||||
"question": "What is X?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "X", # Too short
|
||||
"context": []
|
||||
},
|
||||
"metadata": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
report = quality_checker.check_quality(testset)
|
||||
completeness = report['metrics']['answer_completeness']
|
||||
assert completeness['completeness_rate'] < 1.0
|
||||
|
||||
def test_quality_check_low_context_coverage(self, quality_checker):
|
||||
"""Test quality check with low context coverage"""
|
||||
testset = {
|
||||
"questions": [
|
||||
{
|
||||
"index": 0,
|
||||
"question": "What is X?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "X is something.",
|
||||
"context": [] # No context
|
||||
},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"question": "What is Y?",
|
||||
"type": "simple",
|
||||
"ground_truth": {
|
||||
"answer": "Y is something.",
|
||||
"context": ["doc1"]
|
||||
},
|
||||
"metadata": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
report = quality_checker.check_quality(testset)
|
||||
coverage = report['metrics']['context_coverage']
|
||||
assert coverage['coverage_rate'] < 1.0
|
||||
assert coverage['without_context'] > 0
|
||||
Reference in new issue
Block a user