"""Unit tests for search_engine.py — normalize, ФССЦ, winter, resources.""" import json import os import sys from pathlib import Path import pytest # Add parent dir to path for imports sys.path.insert(0, str(Path(__file__).parent.parent)) # Морфология — обязательная зависимость поиска. Импорт должен падать при её отсутствии, # чтобы полный гейт не пропускал поиск из-за неполного окружения. import pymorphy3 # noqa: F401 from search_engine import _normalize, _search_fssc, _search_winter, _search_resources # === _normalize (pymorphy3) === class TestNormalize: def test_noun_genitive(self): assert _normalize("бетона") == "бетон" def test_adjective_plural(self): assert _normalize("кровельных") == "кровельный" def test_noun_accusative(self): assert _normalize("очистку") == "очистка" def test_adjective_feminine(self): assert _normalize("сметная") == "сметный" def test_noun_genitive_plural(self): assert _normalize("щебня") == "щебень" def test_code_unchanged(self): assert _normalize("м300") == "м300" def test_number_unchanged(self): assert _normalize("1046") == "1046" def test_short_word(self): assert _normalize("на") == "на" def test_punctuation_stripped(self): result = _normalize("бетона?") assert result == "бетон" # === _search_winter === class TestSearchWinter: def test_yakutia(self): result = _search_winter("зона зимнего удорожания для Якутии") assert "VII" in result def test_moscow(self): result = _search_winter("зимнее для Москвы") assert "II" in result def test_krasnodar(self): result = _search_winter("зимнее Краснодарский край") assert "I" in result def test_khabarovsk(self): result = _search_winter("зона для Хабаровского края") assert "IV" in result def test_novosibirsk(self): result = _search_winter("зимнее для Новосибирской области") assert "V" in result def test_unknown_region(self): result = _search_winter("зимнее для Атлантиды") assert result == "" # === _search_resources === class TestSearchResources: @pytest.fixture def resources(self): res_path = Path(os.environ.get( "SMETA_PROJECT_DIR", os.path.expanduser("~/smeta-railway") )) / "resources.json" if not res_path.exists(): pytest.skip("resources.json not found") with open(res_path, encoding="utf-8") as f: return json.load(f) def test_ochist_shchebnya(self, resources): result = _search_resources("очистка щебня", resources) assert "28-01-068" in result or "28-01-073" in result def test_zamena_relsov(self, resources): result = _search_resources("замена рельсов", resources) assert "28-01" in result def test_empty_query(self, resources): result = _search_resources("", resources) assert result == "" def test_no_match(self, resources): result = _search_resources("квантовый компьютер", resources) assert result == "" # === _search_fssc === class TestSearchFSSC: def test_cement(self): result = _search_fssc("цена цемента") if not result: pytest.skip("ФССЦ data not available") assert "цемент" in result.lower() def test_sand(self): result = _search_fssc("песок строительный") if not result: pytest.skip("ФССЦ data not available") assert "песо" in result.lower() def test_empty_query(self): result = _search_fssc("") assert result == ""