Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
353 lines
12 KiB
Python
353 lines
12 KiB
Python
"""
|
|
Unit tests for document loader
|
|
"""
|
|
|
|
import pytest
|
|
import tempfile
|
|
import os
|
|
from pathlib import Path
|
|
from rag_evaluation.testset.document_loader import (
|
|
Document,
|
|
DocumentLoader,
|
|
TextLoader,
|
|
MarkdownLoader,
|
|
load_documents
|
|
)
|
|
|
|
|
|
class TestDocument:
|
|
"""Test cases for Document class"""
|
|
|
|
def test_document_creation(self):
|
|
"""Test creating a Document instance"""
|
|
content = "Test content"
|
|
metadata = {'source': 'test.txt', 'author': 'Test'}
|
|
doc = Document(content=content, metadata=metadata)
|
|
|
|
assert doc.page_content == content
|
|
assert doc.metadata == metadata
|
|
|
|
def test_document_empty_content(self):
|
|
"""Test creating a Document with empty content"""
|
|
doc = Document(content="", metadata={})
|
|
assert doc.page_content == ""
|
|
assert doc.metadata == {}
|
|
|
|
|
|
class TestDocumentLoader:
|
|
"""Test cases for DocumentLoader base class"""
|
|
|
|
def test_file_not_found(self):
|
|
"""Test that FileNotFoundError is raised for non-existent file"""
|
|
with pytest.raises(FileNotFoundError):
|
|
DocumentLoader("nonexistent_file.txt")
|
|
|
|
def test_load_not_implemented(self):
|
|
"""Test that load() raises NotImplementedError"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
|
|
f.write("test")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = DocumentLoader(temp_path)
|
|
with pytest.raises(NotImplementedError):
|
|
loader.load()
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_get_file_size(self):
|
|
"""Test getting file size"""
|
|
content = "Test content for size calculation"
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = DocumentLoader(temp_path)
|
|
size = loader.get_file_size()
|
|
assert size > 0
|
|
# Note: NamedTemporaryFile may create sparse files, so we just check it's positive
|
|
assert size >= len(content.encode('utf-8'))
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_get_file_extension(self):
|
|
"""Test getting file extension"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
|
|
f.write("test")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = DocumentLoader(temp_path)
|
|
ext = loader.get_file_extension()
|
|
assert ext == '.txt'
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_get_file_extension_case_insensitive(self):
|
|
"""Test that file extension is lowercase"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.TXT') as f:
|
|
f.write("test")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = DocumentLoader(temp_path)
|
|
ext = loader.get_file_extension()
|
|
assert ext == '.txt'
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
class TestTextLoader:
|
|
"""Test cases for TextLoader"""
|
|
|
|
def test_load_text_file(self):
|
|
"""Test loading a text file"""
|
|
content = "This is a test text file.\nWith multiple lines."
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = TextLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == content
|
|
assert docs[0].metadata['file_type'] == 'text'
|
|
assert docs[0].metadata['source'] == temp_path
|
|
assert docs[0].metadata['file_size'] > 0
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_empty_text_file(self):
|
|
"""Test loading an empty text file"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = TextLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == ""
|
|
assert docs[0].metadata['file_size'] == 0
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_text_file_with_unicode(self):
|
|
"""Test loading a text file with Unicode characters"""
|
|
content = "测试中文内容\nTest Unicode: 你好世界 🌍"
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = TextLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == content
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_large_text_file(self):
|
|
"""Test loading a large text file"""
|
|
content = "Line {}\n" * 10000 # 10000 lines
|
|
content = content.format(*range(10000))
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = TextLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert len(docs[0].page_content) > 50000 # Should be large
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
class TestMarkdownLoader:
|
|
"""Test cases for MarkdownLoader"""
|
|
|
|
def test_load_markdown_file(self):
|
|
"""Test loading a markdown file"""
|
|
content = "# Test Markdown\n\nThis is a **test** markdown file."
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = MarkdownLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == content
|
|
assert docs[0].metadata['file_type'] == 'markdown'
|
|
assert docs[0].metadata['source'] == temp_path
|
|
assert docs[0].metadata['file_size'] > 0
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_empty_markdown_file(self):
|
|
"""Test loading an empty markdown file"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md', encoding='utf-8') as f:
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = MarkdownLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == ""
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_markdown_with_code_blocks(self):
|
|
"""Test loading markdown with code blocks"""
|
|
content = """# Code Example
|
|
|
|
```python
|
|
def hello():
|
|
print("Hello, World!")
|
|
```
|
|
|
|
Some text after code.
|
|
"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
loader = MarkdownLoader(temp_path)
|
|
docs = loader.load()
|
|
|
|
assert len(docs) == 1
|
|
assert "```python" in docs[0].page_content
|
|
assert "def hello():" in docs[0].page_content
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
class TestLoadDocuments:
|
|
"""Test cases for load_documents function"""
|
|
|
|
def test_load_single_text_file(self):
|
|
"""Test loading a single text file"""
|
|
content = "Test content"
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
docs = load_documents([temp_path])
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == content
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_multiple_files(self):
|
|
"""Test loading multiple files"""
|
|
files = []
|
|
try:
|
|
# Create text file
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write("Text content")
|
|
files.append(f.name)
|
|
|
|
# Create markdown file
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md', encoding='utf-8') as f:
|
|
f.write("# Markdown content")
|
|
files.append(f.name)
|
|
|
|
docs = load_documents(files)
|
|
assert len(docs) == 2
|
|
|
|
# Check that both file types were loaded
|
|
file_types = [doc.metadata['file_type'] for doc in docs]
|
|
assert 'text' in file_types
|
|
assert 'markdown' in file_types
|
|
finally:
|
|
for file_path in files:
|
|
os.unlink(file_path)
|
|
|
|
def test_load_empty_file_list(self):
|
|
"""Test loading empty file list"""
|
|
docs = load_documents([])
|
|
assert len(docs) == 0
|
|
|
|
def test_load_mixed_extensions(self):
|
|
"""Test loading files with different extensions"""
|
|
files = []
|
|
try:
|
|
# Create .txt file
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write("Text file")
|
|
files.append(f.name)
|
|
|
|
# Create .md file
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md', encoding='utf-8') as f:
|
|
f.write("Markdown file")
|
|
files.append(f.name)
|
|
|
|
# Create .markdown file
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.markdown', encoding='utf-8') as f:
|
|
f.write("Markdown file 2")
|
|
files.append(f.name)
|
|
|
|
docs = load_documents(files)
|
|
assert len(docs) == 3
|
|
finally:
|
|
for file_path in files:
|
|
os.unlink(file_path)
|
|
|
|
def test_load_unknown_extension(self):
|
|
"""Test loading file with unknown extension (should default to text)"""
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.unknown', encoding='utf-8') as f:
|
|
f.write("Unknown file type")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
docs = load_documents([temp_path])
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == "Unknown file type"
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_documents_preserves_order(self):
|
|
"""Test that documents are loaded in the order specified"""
|
|
files = []
|
|
contents = ["First file", "Second file", "Third file"]
|
|
|
|
try:
|
|
for i, content in enumerate(contents):
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
files.append(f.name)
|
|
|
|
docs = load_documents(files)
|
|
assert len(docs) == 3
|
|
|
|
for i, doc in enumerate(docs):
|
|
assert doc.page_content == contents[i]
|
|
finally:
|
|
for file_path in files:
|
|
os.unlink(file_path)
|
|
|
|
def test_load_documents_with_special_characters_in_path(self):
|
|
"""Test loading documents with special characters in filename"""
|
|
content = "Test content"
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix=' test file.txt', encoding='utf-8') as f:
|
|
f.write(content)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
docs = load_documents([temp_path])
|
|
assert len(docs) == 1
|
|
assert docs[0].page_content == content
|
|
finally:
|
|
os.unlink(temp_path)
|