ETL: потоковое чтение S3, парсер content, чекпоинты, конкурентные эмбеддинги, CLI прогресса
This commit is contained in:
1 parent
e3f6084527
commit
c6b2f0a520
21 files changed
+1808
-65
No files matched your search
@@ -4,7 +4,10 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
import pytest
|
||||
|
||||
from src.parsers.base import ParsedObject
|
||||
from src.parsers.csv_content import ContentCsvRowParser
|
||||
from src.parsers.csv_stream import CsvStreamReader
|
||||
from src.parsers.json_stream import JsonArrayStreamReader
|
||||
from src.parsers.stub import StubCsvRowParser, StubJsonObjectParser
|
||||
@@ -168,6 +171,126 @@ def test_csv_row_parser_more_values_than_fieldnames():
|
||||
assert result.metadata == {"col1": "a"}
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Тесты ContentCsvRowParser
|
||||
# =========================================================================
|
||||
|
||||
|
||||
def test_content_parser_uses_text_column_by_name():
|
||||
"""Текст берётся из колонки content, а не из первой по порядку."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
result = parser.parse_row(
|
||||
["42", "Заголовок", "Текст для векторизации", "https://example.com"],
|
||||
["id", "title", "content", "url"],
|
||||
)
|
||||
|
||||
assert result.text == "Текст для векторизации"
|
||||
assert result.metadata == {
|
||||
"id": "42",
|
||||
"title": "Заголовок",
|
||||
"url": "https://example.com",
|
||||
"document_id": "42",
|
||||
}
|
||||
|
||||
|
||||
def test_content_parser_skips_empty_metadata_values():
|
||||
"""Пустые значения колонок не попадают в метаданные."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
result = parser.parse_row(
|
||||
["7", "", "Текст", ""],
|
||||
["id", "title", "content", "url"],
|
||||
)
|
||||
|
||||
assert result.text == "Текст"
|
||||
assert result.metadata == {"id": "7", "document_id": "7"}
|
||||
|
||||
|
||||
def test_content_parser_custom_text_field():
|
||||
"""Имя текстовой колонки можно переопределить."""
|
||||
parser = ContentCsvRowParser(text_field="body", id_field=None)
|
||||
|
||||
result = parser.parse_row(["Заголовок", "Полный текст"], ["title", "body"])
|
||||
|
||||
assert result.text == "Полный текст"
|
||||
assert result.metadata == {"title": "Заголовок"}
|
||||
|
||||
|
||||
def test_content_parser_without_id_field_does_not_set_document_id():
|
||||
"""При id_field=None document_id не подставляется."""
|
||||
parser = ContentCsvRowParser(id_field=None)
|
||||
|
||||
result = parser.parse_row(["42", "Текст"], ["id", "content"])
|
||||
|
||||
assert result.metadata == {"id": "42"}
|
||||
|
||||
|
||||
def test_content_parser_does_not_override_document_id():
|
||||
"""Существующий document_id в строке сохраняется."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
result = parser.parse_row(
|
||||
["42", "Текст", "custom-id"],
|
||||
["id", "content", "document_id"],
|
||||
)
|
||||
|
||||
assert result.metadata["document_id"] == "custom-id"
|
||||
|
||||
|
||||
def test_content_parser_missing_text_column_raises():
|
||||
"""Если колонки content нет — явная ошибка, а не пустой текст."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
with pytest.raises(ValueError, match="content"):
|
||||
parser.parse_row(["1", "a"], ["id", "title"])
|
||||
|
||||
|
||||
def test_content_parser_short_row():
|
||||
"""Значений меньше, чем колонок — недостающие просто отсутствуют."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
result = parser.parse_row(["1", "Текст"], ["id", "content", "url"])
|
||||
|
||||
assert result.text == "Текст"
|
||||
assert result.metadata == {"id": "1", "document_id": "1"}
|
||||
|
||||
|
||||
def test_content_parser_empty_row():
|
||||
"""Пустая строка → пустой ParsedObject."""
|
||||
parser = ContentCsvRowParser()
|
||||
|
||||
assert parser.parse_row([], ["id", "content"]) == ParsedObject(text="", metadata={})
|
||||
|
||||
|
||||
async def test_csv_stream_reader_with_content_parser():
|
||||
"""Сквозная проверка: заголовок + строки реальной схемы."""
|
||||
reader = CsvStreamReader(parser=ContentCsvRowParser())
|
||||
|
||||
async def lines() -> AsyncIterator[str]:
|
||||
for line in [
|
||||
"id,title,content,url",
|
||||
'1,Первый,"Текст, с запятой",https://a',
|
||||
"2,Второй,Другой текст,",
|
||||
]:
|
||||
yield line
|
||||
|
||||
results = []
|
||||
async for obj in reader.iter_objects(lines()):
|
||||
results.append(obj)
|
||||
|
||||
assert len(results) == 2
|
||||
assert results[0].text == "Текст, с запятой"
|
||||
assert results[0].metadata == {
|
||||
"id": "1",
|
||||
"title": "Первый",
|
||||
"url": "https://a",
|
||||
"document_id": "1",
|
||||
}
|
||||
assert results[1].text == "Другой текст"
|
||||
assert results[1].metadata == {"id": "2", "title": "Второй", "document_id": "2"}
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Тесты CsvStreamReader
|
||||
# =========================================================================
|
||||
|
||||
Reference in new issue
Block a user