"""Фаза 4/5: парсеры новых источников (opendata, БИМ-дерево, XSD-скрейп). Моки MockTransport."""
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
import xsd_client # noqa: E402
def test_opendata_list_parses_items():
resp = {"total": 2, "items": [
{"id": 2, "datasetName": "КСР", "lastChangeDate": "2026-05-20"},
{"id": 4, "datasetName": "ФСНБ-2022", "lastChangeDate": "2026-05-15"}]}
with httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(200, json=resp))) as c:
items = fc.opendata_list(client=c)
assert [i["id"] for i in items] == [2, 4]
def test_opendata_list_format_error():
with httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(200, json={"x": 1}))) as c:
with pytest.raises(fc.FgisFormatError):
fc.opendata_list(client=c)
def test_bim_tree_recursion_collects_leaf_docs():
# корень (parentGuid отсутствует) → папка F1; F1 → лист L1 (есть filePath) + папка F2; F2 → лист L2
def handler(r):
pg = r.url.params.get("parentGuid")
if pg is None:
return httpx.Response(200, json={"": [{"guid": "F1", "name": "2026", "filePath": None}]})
if pg == "F1":
return httpx.Response(200, json={"": [
{"guid": "L1", "name": "письмо A", "filePath": "/a.pdf"},
{"guid": "F2", "name": "2 кв", "filePath": None}]})
if pg == "F2":
return httpx.Response(200, json={"": [{"guid": "L2", "name": "письмо B", "filePath": "/b.pdf"}]})
return httpx.Response(200, json={"": []})
with httpx.Client(transport=httpx.MockTransport(handler)) as c:
docs = fc.bim_leaf_docs(client=c)
guids = {d["guid"] for d in docs}
assert guids == {"L1", "L2"} # только листья с filePath, папки — не документы
def test_xsd_schema_links_extracted():
html = 'КАЦ нет ' \
'ЛСР'
with httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(200, text=html))) as c:
links = xsd_client.schema_links(client=c)
assert links == ["/upload/x/KAC-4_02.zip", "/upload/y/LSR.zip"]
assert xsd_client._fname("/upload/x/KAC-4_02.zip") == "KAC-4_02.zip"
def test_xsd_fail_loud_on_zero_links():
"""0 ссылок = вёрстка сменилась → FetchError, а не тихое «обновлений нет»."""
with httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(200, text="no links"))) as c:
with pytest.raises(Exception):
xsd_client.schema_links(client=c)