"""Срез 1.1: клиент справочной иерархии ФГИС ЦС. Юниты — на РЕАЛЬНЫХ фикстурах, снятых с ФГИС (мок обязан совпадать с реальностью). Живой смоук — только при EXT_SMOKE=1 (чтобы regression_gate/деплой оставались офлайновыми). """ import json import os import sys from pathlib import Path import httpx import pytest sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import fgis_client as fc # noqa: E402 FIX = Path(__file__).resolve().parent / "fixtures" / "ext" def _load(name): return json.loads((FIX / name).read_text(encoding="utf-8")) def _handler(request): p = request.url.path if p == "/api/alive": return httpx.Response(200, text="") if p == "/api/EstimatedPrice/CountrySubjects": return httpx.Response(200, json=_load("fgis_country_subjects.json")) if p == "/api/EstimatedPrice/PriceZones": return httpx.Response(200, json=_load("fgis_price_zones_323.json")) if p == "/api/EstimatedPrice/Periods": return httpx.Response(200, json=_load("fgis_periods_191.json")) return httpx.Response(404) def _client(): return httpx.Client(transport=httpx.MockTransport(_handler)) def test_country_subjects_shape(): with _client() as c: subs = fc.country_subjects(client=c) assert len(subs) == 86 assert all("id" in s and "name" in s for s in subs) def test_subject_id_by_name(): with _client() as c: assert fc.subject_id_by_name("Алтайский", client=c) == 281 # известный из фикстуры assert fc.subject_id_by_name("нетакогорегиона", client=c) is None def test_price_zones_and_periods(): with _client() as c: zones = fc.price_zones(323, client=c) assert zones == [{"id": 191, "name": "город Москва"}] pers = fc.periods(191, client=c) assert len(pers) == 10 def test_latest_period_is_max_id(): with _client() as c: lp = fc.latest_period(191, client=c) assert lp["id"] == 426 and "2 квартал 2026" in lp["name"] # свежий = наибольший id def test_format_error_on_wrong_shape(): def bad(request): return httpx.Response(200, json=[{"foo": "bar"}]) # нет id/name with httpx.Client(transport=httpx.MockTransport(bad)) as c: with pytest.raises(fc.FgisFormatError): fc.country_subjects(client=c) def test_alive(): with _client() as c: assert fc.alive(client=c) is True with httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(503))) as c: assert fc.alive(client=c) is False @pytest.mark.skipif(os.environ.get("EXT_SMOKE") != "1", reason="живой запрос к ФГИС ЦС только при EXT_SMOKE=1") def test_smoke_live_fgis(): assert fc.alive() is True lp = fc.latest_period(191) # Москва, зона 191 assert lp and lp["id"] >= 426