195 lines
4.9 KiB
Python
195 lines
4.9 KiB
Python
"""Юнит-тесты для процессоров чанкинга."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from src.processors.base import Chunk, Chunker
|
|
from src.processors.noop import NoOpChunker
|
|
from src.processors.paragraph import ParagraphChunker
|
|
|
|
# --- Chunk dataclass ---
|
|
|
|
|
|
def test_chunk_creation():
|
|
chunk = Chunk(text="hello", index=0, metadata={"source": "kafka"})
|
|
|
|
assert chunk.text == "hello"
|
|
assert chunk.index == 0
|
|
assert chunk.metadata == {"source": "kafka"}
|
|
|
|
|
|
def test_chunk_default_metadata():
|
|
chunk = Chunk(text="hello", index=1)
|
|
|
|
assert chunk.metadata == {}
|
|
|
|
|
|
def test_chunk_frozen():
|
|
chunk = Chunk(text="hello", index=0)
|
|
|
|
with pytest.raises(AttributeError):
|
|
chunk.text = "world" # type: ignore[misc]
|
|
|
|
|
|
def test_chunk_equality():
|
|
c1 = Chunk(text="hello", index=0, metadata={"a": 1})
|
|
c2 = Chunk(text="hello", index=0, metadata={"a": 1})
|
|
c3 = Chunk(text="hello", index=1, metadata={"a": 1})
|
|
|
|
assert c1 == c2
|
|
assert c1 != c3
|
|
|
|
|
|
# --- NoOpChunker ---
|
|
|
|
|
|
async def test_noop_single_chunk():
|
|
chunker = NoOpChunker()
|
|
metadata = {"source": "kafka:documents", "document_id": "doc-1"}
|
|
|
|
result = await chunker.chunk("Some text content", metadata)
|
|
|
|
assert len(result) == 1
|
|
assert result[0].text == "Some text content"
|
|
assert result[0].index == 0
|
|
assert result[0].metadata == metadata
|
|
|
|
|
|
async def test_noop_empty_text():
|
|
chunker = NoOpChunker()
|
|
|
|
result = await chunker.chunk("", {})
|
|
|
|
assert result == []
|
|
|
|
|
|
async def test_noop_preserves_metadata():
|
|
chunker = NoOpChunker()
|
|
metadata = {"key": "value", "nested": {"a": 1}}
|
|
|
|
result = await chunker.chunk("text", metadata)
|
|
|
|
assert result[0].metadata is metadata
|
|
|
|
|
|
# --- ParagraphChunker ---
|
|
|
|
|
|
async def test_paragraph_single_paragraph_short_text():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
|
|
result = await chunker.chunk("Some text content", {})
|
|
|
|
assert len(result) == 1
|
|
assert result[0].text == "Some text content"
|
|
assert result[0].index == 0
|
|
|
|
|
|
async def test_paragraph_multiple_paragraphs():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
|
|
result = await chunker.chunk("First paragraph.\n\nSecond paragraph.", {})
|
|
|
|
assert len(result) == 2
|
|
assert result[0].text == "First paragraph."
|
|
assert result[0].index == 0
|
|
assert result[1].text == "Second paragraph."
|
|
assert result[1].index == 1
|
|
|
|
|
|
async def test_paragraph_empty_text():
|
|
chunker = ParagraphChunker(max_tokens=10)
|
|
|
|
result = await chunker.chunk("", {})
|
|
|
|
assert result == []
|
|
|
|
|
|
async def test_paragraph_strips_whitespace_paragraphs():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
|
|
result = await chunker.chunk(" text \n\n\n\n more ", {})
|
|
|
|
assert len(result) == 2
|
|
assert result[0].text == "text"
|
|
assert result[1].text == "more"
|
|
|
|
|
|
async def test_paragraph_metadata_propagated():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
metadata = {"source": "kafka", "document_id": "doc-1"}
|
|
|
|
result = await chunker.chunk("text", metadata)
|
|
|
|
assert all(c.metadata == metadata for c in result)
|
|
|
|
|
|
async def test_paragraph_sequential_indices():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
|
|
result = await chunker.chunk("a\n\nb\n\nc", {})
|
|
|
|
indices = [c.index for c in result]
|
|
assert indices == [0, 1, 2]
|
|
|
|
|
|
async def test_paragraph_max_tokens_none_no_limit():
|
|
chunker = ParagraphChunker(max_tokens=None)
|
|
|
|
long_text = "word " * 500
|
|
result = await chunker.chunk(long_text, {})
|
|
|
|
assert len(result) == 1
|
|
assert result[0].text == long_text.strip()
|
|
|
|
|
|
async def test_paragraph_splits_long_paragraph_by_sentences():
|
|
chunker = ParagraphChunker(max_tokens=10)
|
|
|
|
text = "Short one. This is a longer second sentence here."
|
|
result = await chunker.chunk(text, {})
|
|
|
|
assert len(result) >= 2
|
|
full = " ".join(c.text for c in result)
|
|
assert full == text
|
|
|
|
|
|
async def test_paragraph_splits_very_long_sentence_by_tokens():
|
|
chunker = ParagraphChunker(max_tokens=5)
|
|
|
|
text = "supercalifragilisticexpialidocious"
|
|
result = await chunker.chunk(text, {})
|
|
|
|
assert len(result) >= 2
|
|
for chunk in result:
|
|
assert len(chunker._encoding.encode(chunk.text)) <= 5
|
|
|
|
|
|
async def test_paragraph_skips_empty_paragraphs():
|
|
chunker = ParagraphChunker(max_tokens=100)
|
|
|
|
result = await chunker.chunk("text\n\n\n\nmore", {})
|
|
|
|
assert len(result) == 2
|
|
assert result[0].text == "text"
|
|
assert result[1].text == "more"
|
|
|
|
|
|
def test_paragraph_invalid_max_tokens_zero():
|
|
with pytest.raises(ValueError, match="max_tokens должен быть положительным или None"):
|
|
ParagraphChunker(max_tokens=0)
|
|
|
|
|
|
def test_paragraph_invalid_max_tokens_negative():
|
|
with pytest.raises(ValueError, match="max_tokens должен быть положительным или None"):
|
|
ParagraphChunker(max_tokens=-1)
|
|
|
|
|
|
# --- Chunker ABC ---
|
|
|
|
|
|
def test_chunker_is_abstract():
|
|
with pytest.raises(TypeError):
|
|
Chunker() # type: ignore[abstract]
|