Files
etl-service/tests/test_prepare.py

106 lines
2.7 KiB
Python

"""Юнит-тесты для препроцессоров текста."""
from __future__ import annotations
import pytest
from src.prepare.base_prepare import PreProcessor
from src.prepare.min_words import DEFAULT_MIN_WORDS, MinWordsPreProcessor
# --- PreProcessor ABC ---
def test_preprocessor_is_abstract():
with pytest.raises(TypeError):
PreProcessor() # type: ignore[abstract]
# --- MinWordsPreProcessor ---
async def test_min_words_passes_long_enough_text():
processor = MinWordsPreProcessor(min_words=20)
text = " ".join(f"word{i}" for i in range(20))
result = await processor.prepare(text, {})
assert result == text
async def test_min_words_skips_empty_text():
processor = MinWordsPreProcessor()
result = await processor.prepare("", {})
assert result is None
async def test_min_words_skips_whitespace_only():
processor = MinWordsPreProcessor()
result = await processor.prepare(" \n\t ", {})
assert result is None
async def test_min_words_skips_short_text():
processor = MinWordsPreProcessor(min_words=20)
text = "one two three four five"
result = await processor.prepare(text, {})
assert result is None
async def test_min_words_boundary_exact_min():
processor = MinWordsPreProcessor(min_words=20)
text = " ".join(f"word{i}" for i in range(20))
result = await processor.prepare(text, {})
assert result == text
async def test_min_words_boundary_one_less():
processor = MinWordsPreProcessor(min_words=20)
text = " ".join(f"word{i}" for i in range(19))
result = await processor.prepare(text, {})
assert result is None
async def test_min_words_default_min_words():
processor = MinWordsPreProcessor()
text = " ".join(f"word{i}" for i in range(DEFAULT_MIN_WORDS))
result = await processor.prepare(text, {})
assert result == text
async def test_min_words_custom_threshold():
processor = MinWordsPreProcessor(min_words=3)
assert await processor.prepare("a b c", {}) == "a b c"
assert await processor.prepare("a b", {}) is None
async def test_min_words_ignores_metadata():
processor = MinWordsPreProcessor(min_words=5)
text = "one two three four five"
result = await processor.prepare(text, {"source": "kafka", "document_id": "doc-1"})
assert result == text
def test_min_words_invalid_threshold_zero():
with pytest.raises(ValueError, match="min_words должен быть положительным"):
MinWordsPreProcessor(min_words=0)
def test_min_words_invalid_threshold_negative():
with pytest.raises(ValueError, match="min_words должен быть положительным"):
MinWordsPreProcessor(min_words=-1)