237 lines
9.1 KiB
Python
237 lines
9.1 KiB
Python
"""
|
|||
|
|
Unit tests for document sampling strategy
|
||
|
|
"""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from rag_evaluation.testset.sampling import SamplingStrategy, DocumentChunker
|
||
|
|
from rag_evaluation.testset.document_loader import Document
|
||
|
|
from rag_evaluation.core.config import TestsetConfig
|
||
|
|
|
||
|
|
|
||
|
|
class TestSamplingStrategy:
|
||
|
|
"""Test cases for SamplingStrategy"""
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def config(self):
|
||
|
|
"""Create test configuration"""
|
||
|
|
return TestsetConfig(
|
||
|
|
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
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def strategy(self, config):
|
||
|
|
"""Create sampling strategy instance"""
|
||
|
|
return SamplingStrategy(config)
|
||
|
|
|
||
|
|
def test_get_sampling_rate_small_file(self, strategy):
|
||
|
|
"""Test sampling rate for small files"""
|
||
|
|
# 0.5 MB file
|
||
|
|
file_size = 0.5 * 1024 * 1024
|
||
|
|
rate = strategy.get_sampling_rate(int(file_size))
|
||
|
|
assert rate == 1.0
|
||
|
|
|
||
|
|
def test_get_sampling_rate_medium_file(self, strategy):
|
||
|
|
"""Test sampling rate for medium files"""
|
||
|
|
# 5 MB file
|
||
|
|
file_size = 5 * 1024 * 1024
|
||
|
|
rate = strategy.get_sampling_rate(int(file_size))
|
||
|
|
assert rate == 0.5
|
||
|
|
|
||
|
|
def test_get_sampling_rate_large_file(self, strategy):
|
||
|
|
"""Test sampling rate for large files"""
|
||
|
|
# 15 MB file
|
||
|
|
file_size = 15 * 1024 * 1024
|
||
|
|
rate = strategy.get_sampling_rate(int(file_size))
|
||
|
|
assert rate == 0.2
|
||
|
|
|
||
|
|
def test_get_sampling_rate_boundary_small_medium(self, strategy):
|
||
|
|
"""Test sampling rate at small-medium boundary"""
|
||
|
|
# Exactly 1 MB file (boundary)
|
||
|
|
file_size = 1.0 * 1024 * 1024
|
||
|
|
rate = strategy.get_sampling_rate(int(file_size))
|
||
|
|
assert rate == 0.5 # Should be medium
|
||
|
|
|
||
|
|
def test_get_sampling_rate_boundary_medium_large(self, strategy):
|
||
|
|
"""Test sampling rate at medium-large boundary"""
|
||
|
|
# Exactly 10 MB file (boundary)
|
||
|
|
file_size = 10.0 * 1024 * 1024
|
||
|
|
rate = strategy.get_sampling_rate(int(file_size))
|
||
|
|
assert rate == 0.2 # Should be large
|
||
|
|
|
||
|
|
def test_sample_documents_all_small(self, strategy):
|
||
|
|
"""Test sampling with all small documents"""
|
||
|
|
docs = [
|
||
|
|
Document(content="Small doc 1", metadata={'file_size': 500 * 1024}),
|
||
|
|
Document(content="Small doc 2", metadata={'file_size': 800 * 1024}),
|
||
|
|
Document(content="Small doc 3", metadata={'file_size': 900 * 1024})
|
||
|
|
]
|
||
|
|
sampled = strategy.sample_documents(docs)
|
||
|
|
assert len(sampled) == 3 # All should be included
|
||
|
|
|
||
|
|
def test_sample_documents_mixed_sizes(self, strategy):
|
||
|
|
"""Test sampling with mixed document sizes"""
|
||
|
|
docs = [
|
||
|
|
Document(content="Small doc", metadata={'file_size': 500 * 1024}),
|
||
|
|
Document(content="Medium doc", metadata={'file_size': 5 * 1024 * 1024}),
|
||
|
|
Document(content="Large doc", metadata={'file_size': 15 * 1024 * 1024})
|
||
|
|
]
|
||
|
|
sampled = strategy.sample_documents(docs)
|
||
|
|
# Current implementation includes all documents with sampling_rate > 0
|
||
|
|
assert len(sampled) == 3
|
||
|
|
|
||
|
|
def test_sample_documents_empty_list(self, strategy):
|
||
|
|
"""Test sampling with empty document list"""
|
||
|
|
docs = []
|
||
|
|
sampled = strategy.sample_documents(docs)
|
||
|
|
assert len(sampled) == 0
|
||
|
|
|
||
|
|
def test_sample_documents_missing_file_size(self, strategy):
|
||
|
|
"""Test sampling with documents missing file_size metadata"""
|
||
|
|
docs = [
|
||
|
|
Document(content="Doc without size", metadata={})
|
||
|
|
]
|
||
|
|
sampled = strategy.sample_documents(docs)
|
||
|
|
# Should handle missing file_size gracefully (defaults to 0)
|
||
|
|
assert len(sampled) == 1
|
||
|
|
|
||
|
|
|
||
|
|
class TestDocumentChunker:
|
||
|
|
"""Test cases for DocumentChunker"""
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def chunker(self):
|
||
|
|
"""Create document chunker instance"""
|
||
|
|
return DocumentChunker(chunk_size=100, chunk_overlap=20)
|
||
|
|
|
||
|
|
def test_chunk_document_single_chunk(self, chunker):
|
||
|
|
"""Test chunking a document that fits in one chunk"""
|
||
|
|
doc = Document(
|
||
|
|
content="This is a short document.",
|
||
|
|
metadata={'source': 'test.txt'}
|
||
|
|
)
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
assert len(chunks) == 1
|
||
|
|
assert chunks[0].page_content == "This is a short document."
|
||
|
|
assert chunks[0].metadata['chunk_index'] == 0
|
||
|
|
|
||
|
|
def test_chunk_document_multiple_chunks(self, chunker):
|
||
|
|
"""Test chunking a document into multiple chunks"""
|
||
|
|
# Create content longer than chunk_size
|
||
|
|
content = "A" * 250
|
||
|
|
doc = Document(content=content, metadata={'source': 'test.txt'})
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
|
||
|
|
# Should create 3 chunks: 0-100, 80-180, 160-250
|
||
|
|
assert len(chunks) >= 2
|
||
|
|
assert chunks[0].metadata['chunk_index'] == 0
|
||
|
|
assert chunks[1].metadata['chunk_index'] == 1
|
||
|
|
|
||
|
|
def test_chunk_document_overlap(self, chunker):
|
||
|
|
"""Test that chunks have proper overlap"""
|
||
|
|
content = "A" * 250
|
||
|
|
doc = Document(content=content, metadata={'source': 'test.txt'})
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
|
||
|
|
# Check overlap between first two chunks
|
||
|
|
if len(chunks) >= 2:
|
||
|
|
chunk1_end = chunks[0].metadata['chunk_end']
|
||
|
|
chunk2_start = chunks[1].metadata['chunk_start']
|
||
|
|
overlap = chunk1_end - chunk2_start
|
||
|
|
assert overlap == chunker.chunk_overlap
|
||
|
|
|
||
|
|
def test_chunk_document_metadata_preservation(self, chunker):
|
||
|
|
"""Test that original metadata is preserved in chunks"""
|
||
|
|
doc = Document(
|
||
|
|
content="A" * 250,
|
||
|
|
metadata={'source': 'test.txt', 'author': 'Test Author'}
|
||
|
|
)
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
|
||
|
|
for chunk in chunks:
|
||
|
|
assert chunk.metadata['source'] == 'test.txt'
|
||
|
|
assert chunk.metadata['author'] == 'Test Author'
|
||
|
|
assert 'chunk_index' in chunk.metadata
|
||
|
|
assert 'chunk_start' in chunk.metadata
|
||
|
|
assert 'chunk_end' in chunk.metadata
|
||
|
|
|
||
|
|
def test_chunk_document_empty_content(self, chunker):
|
||
|
|
"""Test chunking an empty document"""
|
||
|
|
doc = Document(content="", metadata={'source': 'empty.txt'})
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
# Empty content results in no chunks
|
||
|
|
assert len(chunks) == 0
|
||
|
|
|
||
|
|
def test_chunk_documents_multiple(self, chunker):
|
||
|
|
"""Test chunking multiple documents"""
|
||
|
|
docs = [
|
||
|
|
Document(content="A" * 150, metadata={'source': 'doc1.txt'}),
|
||
|
|
Document(content="B" * 150, metadata={'source': 'doc2.txt'})
|
||
|
|
]
|
||
|
|
all_chunks = chunker.chunk_documents(docs)
|
||
|
|
|
||
|
|
# Should have chunks from both documents
|
||
|
|
assert len(all_chunks) >= 2
|
||
|
|
|
||
|
|
# Verify chunks from different documents
|
||
|
|
sources = set(chunk.metadata['source'] for chunk in all_chunks)
|
||
|
|
assert 'doc1.txt' in sources
|
||
|
|
assert 'doc2.txt' in sources
|
||
|
|
|
||
|
|
def test_chunk_documents_empty_list(self, chunker):
|
||
|
|
"""Test chunking empty document list"""
|
||
|
|
docs = []
|
||
|
|
all_chunks = chunker.chunk_documents(docs)
|
||
|
|
assert len(all_chunks) == 0
|
||
|
|
|
||
|
|
def test_chunk_size_configuration(self):
|
||
|
|
"""Test different chunk size configurations"""
|
||
|
|
chunker_small = DocumentChunker(chunk_size=50, chunk_overlap=10)
|
||
|
|
chunker_large = DocumentChunker(chunk_size=200, chunk_overlap=40)
|
||
|
|
|
||
|
|
content = "A" * 250
|
||
|
|
doc = Document(content=content, metadata={'source': 'test.txt'})
|
||
|
|
|
||
|
|
chunks_small = chunker_small.chunk_document(doc)
|
||
|
|
chunks_large = chunker_large.chunk_document(doc)
|
||
|
|
|
||
|
|
# Smaller chunk size should produce more chunks
|
||
|
|
assert len(chunks_small) > len(chunks_large)
|
||
|
|
|
||
|
|
def test_chunk_overlap_configuration(self):
|
||
|
|
"""Test different overlap configurations"""
|
||
|
|
chunker_no_overlap = DocumentChunker(chunk_size=100, chunk_overlap=0)
|
||
|
|
chunker_with_overlap = DocumentChunker(chunk_size=100, chunk_overlap=20)
|
||
|
|
|
||
|
|
content = "A" * 250
|
||
|
|
doc = Document(content=content, metadata={'source': 'test.txt'})
|
||
|
|
|
||
|
|
chunks_no_overlap = chunker_no_overlap.chunk_document(doc)
|
||
|
|
chunks_with_overlap = chunker_with_overlap.chunk_document(doc)
|
||
|
|
|
||
|
|
# With overlap should produce more chunks
|
||
|
|
assert len(chunks_with_overlap) >= len(chunks_no_overlap)
|
||
|
|
|
||
|
|
def test_chunk_content_integrity(self, chunker):
|
||
|
|
"""Test that all content is preserved across chunks"""
|
||
|
|
content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 10 # 260 characters
|
||
|
|
doc = Document(content=content, metadata={'source': 'test.txt'})
|
||
|
|
chunks = chunker.chunk_document(doc)
|
||
|
|
|
||
|
|
# Reconstruct content from first characters of each chunk
|
||
|
|
# (accounting for overlap)
|
||
|
|
reconstructed = ""
|
||
|
|
for i, chunk in enumerate(chunks):
|
||
|
|
if i == 0:
|
||
|
|
reconstructed += chunk.page_content
|
||
|
|
else:
|
||
|
|
# Skip overlap
|
||
|
|
reconstructed += chunk.page_content[chunker.chunk_overlap:]
|
||
|
|
|
||
|
|
# Should be able to reconstruct original content
|
||
|
|
assert len(reconstructed) >= len(content) - chunker.chunk_overlap
|