224 lines
7.0 KiB
Python
224 lines
7.0 KiB
Python
"""Тесты настроек и описания источников пайплайна."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from src.config.settings import PipelineSource, Settings
|
|
from src.pipeline.sources import (
|
|
SourceSpec,
|
|
build_source_specs,
|
|
settings_for_source,
|
|
validate_source_specs,
|
|
)
|
|
|
|
# =========================================================================
|
|
# Тесты Settings.enabled_sources
|
|
# =========================================================================
|
|
|
|
|
|
def test_enabled_sources_from_list():
|
|
settings = Settings(pipeline_sources="csv, documents ,csv")
|
|
|
|
assert settings.enabled_sources() == [
|
|
PipelineSource.CSV,
|
|
PipelineSource.DOCUMENTS,
|
|
]
|
|
|
|
|
|
def test_enabled_sources_falls_back_to_single_source():
|
|
settings = Settings(pipeline_source="documents", pipeline_sources="")
|
|
|
|
assert settings.enabled_sources() == [PipelineSource.DOCUMENTS]
|
|
|
|
|
|
def test_enabled_sources_empty_list_falls_back():
|
|
settings = Settings(pipeline_source="csv", pipeline_sources=" , ")
|
|
|
|
assert settings.enabled_sources() == [PipelineSource.CSV]
|
|
|
|
|
|
def test_enabled_sources_rejects_unknown_value():
|
|
settings = Settings(pipeline_sources="csv,unknown")
|
|
|
|
with pytest.raises(ValueError, match="Неизвестный источник"):
|
|
settings.enabled_sources()
|
|
|
|
|
|
# =========================================================================
|
|
# Тесты Settings.excluded_title_type_ids
|
|
# =========================================================================
|
|
|
|
|
|
def test_excluded_title_type_ids_from_list():
|
|
settings = Settings(title_excluded_type_ids="2, 3 ,4,2, ")
|
|
|
|
assert settings.excluded_title_type_ids() == frozenset({"2", "3", "4"})
|
|
|
|
|
|
def test_excluded_title_type_ids_empty_list():
|
|
settings = Settings(title_excluded_type_ids=" , ")
|
|
|
|
assert settings.excluded_title_type_ids() == frozenset()
|
|
|
|
|
|
def test_excluded_title_type_ids_from_env(monkeypatch):
|
|
monkeypatch.setenv("TITLE_EXCLUDED_TYPE_IDS", "5, 6 ,5")
|
|
settings = Settings(_env_file=None)
|
|
|
|
assert settings.excluded_title_type_ids() == frozenset({"5", "6"})
|
|
|
|
|
|
# =========================================================================
|
|
# Тесты build_source_specs / settings_for_source
|
|
# =========================================================================
|
|
|
|
|
|
def _settings() -> Settings:
|
|
return Settings(
|
|
pipeline_sources="csv,documents",
|
|
s3_prefix="input/",
|
|
s3_processed_prefix="processed/",
|
|
s3_documents_prefix="trusted/",
|
|
s3_documents_processed_prefix="trusted_processed/",
|
|
s3_failed_prefix="trusted_failed/",
|
|
qdrant_collection_name="documents",
|
|
qdrant_documents_collection_name="trusted_documents",
|
|
)
|
|
|
|
|
|
def test_build_source_specs_maps_prefixes_and_collections():
|
|
specs = build_source_specs(_settings())
|
|
|
|
assert [spec.name for spec in specs] == [
|
|
PipelineSource.CSV,
|
|
PipelineSource.DOCUMENTS,
|
|
]
|
|
assert specs[0].input_prefix == "input/"
|
|
assert specs[0].processed_prefix == "processed/"
|
|
assert specs[0].failed_prefix is None
|
|
assert specs[0].collection_name == "documents"
|
|
|
|
assert specs[1].input_prefix == "trusted/"
|
|
assert specs[1].processed_prefix == "trusted_processed/"
|
|
assert specs[1].failed_prefix == "trusted_failed/"
|
|
assert specs[1].collection_name == "trusted_documents"
|
|
|
|
|
|
def test_settings_for_csv_source():
|
|
settings = _settings()
|
|
spec = build_source_specs(settings)[0]
|
|
|
|
source_settings = settings_for_source(settings, spec)
|
|
|
|
assert source_settings.pipeline_source == PipelineSource.CSV
|
|
assert source_settings.s3_prefix == "input/"
|
|
assert source_settings.s3_processed_prefix == "processed/"
|
|
assert source_settings.qdrant_collection_name == "documents"
|
|
|
|
|
|
def test_settings_for_documents_source():
|
|
settings = _settings()
|
|
spec = build_source_specs(settings)[1]
|
|
|
|
source_settings = settings_for_source(settings, spec)
|
|
|
|
assert source_settings.pipeline_source == PipelineSource.DOCUMENTS
|
|
assert source_settings.s3_documents_prefix == "trusted/"
|
|
assert source_settings.s3_documents_processed_prefix == "trusted_processed/"
|
|
assert source_settings.s3_failed_prefix == "trusted_failed/"
|
|
assert source_settings.qdrant_collection_name == "trusted_documents"
|
|
|
|
|
|
# =========================================================================
|
|
# Тесты validate_source_specs
|
|
# =========================================================================
|
|
|
|
|
|
def test_validate_accepts_distinct_sources():
|
|
validate_source_specs(build_source_specs(_settings()))
|
|
|
|
|
|
def test_validate_rejects_empty_sources():
|
|
with pytest.raises(ValueError, match="ни один источник"):
|
|
validate_source_specs([])
|
|
|
|
|
|
def test_validate_rejects_duplicate_sources():
|
|
spec = SourceSpec(
|
|
name=PipelineSource.CSV,
|
|
input_prefix="input/",
|
|
processed_prefix="processed/",
|
|
failed_prefix=None,
|
|
collection_name="documents",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="дублироваться"):
|
|
validate_source_specs([spec, spec])
|
|
|
|
|
|
def test_validate_rejects_overlapping_input_prefixes():
|
|
csv_spec = SourceSpec(
|
|
name=PipelineSource.CSV,
|
|
input_prefix="input/",
|
|
processed_prefix="processed/",
|
|
failed_prefix=None,
|
|
collection_name="documents",
|
|
)
|
|
documents_spec = SourceSpec(
|
|
name=PipelineSource.DOCUMENTS,
|
|
input_prefix="input/trusted/",
|
|
processed_prefix="trusted_processed/",
|
|
failed_prefix="trusted_failed/",
|
|
collection_name="trusted_documents",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="пересекаются"):
|
|
validate_source_specs([csv_spec, documents_spec])
|
|
|
|
|
|
def test_validate_rejects_input_equal_to_output():
|
|
spec = SourceSpec(
|
|
name=PipelineSource.CSV,
|
|
input_prefix="input/",
|
|
processed_prefix="input/",
|
|
failed_prefix=None,
|
|
collection_name="documents",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="совпадают или пересекаются"):
|
|
validate_source_specs([spec])
|
|
|
|
|
|
def test_validate_rejects_failed_overlapping_output():
|
|
spec = SourceSpec(
|
|
name=PipelineSource.DOCUMENTS,
|
|
input_prefix="trusted/",
|
|
processed_prefix="trusted_processed/",
|
|
failed_prefix="trusted_processed/failed/",
|
|
collection_name="trusted_documents",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="failed-префикс"):
|
|
validate_source_specs([spec])
|
|
|
|
|
|
def test_validate_rejects_same_collection_for_two_sources():
|
|
csv_spec = SourceSpec(
|
|
name=PipelineSource.CSV,
|
|
input_prefix="input/",
|
|
processed_prefix="processed/",
|
|
failed_prefix=None,
|
|
collection_name="shared",
|
|
)
|
|
documents_spec = SourceSpec(
|
|
name=PipelineSource.DOCUMENTS,
|
|
input_prefix="trusted/",
|
|
processed_prefix="trusted_processed/",
|
|
failed_prefix="trusted_failed/",
|
|
collection_name="shared",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="разные коллекции"):
|
|
validate_source_specs([csv_spec, documents_spec])
|