All checks were successful
CI Trade-In / changes (pull_request) Successful in 8s
CI / changes (pull_request) Successful in 10s
CI Trade-In / backend-tests (pull_request) Has been skipped
CI Trade-In / browser-tests (pull_request) Has been skipped
CI Trade-In / frontend-checks (pull_request) Has been skipped
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Successful in 2m31s
CI / backend-tests (pull_request) Successful in 17m27s
Шесть функций в site_finder работают на ОБЩЕЙ с analyze_parcel сессии и глотают (OperationalError, ProgrammingError) вокруг db.execute. Без SAVEPOINT проглоченная ошибка оставляет транзакцию в aborted-состоянии: сам lookup возвращает пустой результат «штатно», а падает следующий за ним запрос — в другом месте отчёта и с другой причиной в логе. Соседний ppt_tep_lookup — та же семья, та же таблица planning_projects — SAVEPOINT имел. Правка сводит шесть отставших к нему: planning_lookup, granddoc_lookup, ird_overlay_lookup, functional_zone_lookup, reservation_lookup — по одному db.execute zone_regulation — upsert был защищён, SELECT нет Тест параметризован по всем шести именам, плюс два контроля: «без SAVEPOINT сессия травится» и «здоровый путь по-прежнему читает строки». На origin/main шесть падают с AbortedTransactionError, оба контроля зелёные. Стабы _FakeDB/_DB в пяти существующих тестах получили no-op begin_nested — без него 28 тестов падали на AttributeError. В шапке каждого стаба указано, где SAVEPOINT проверяется по-настоящему, чтобы no-op не читался как покрытие. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
130 lines
5 KiB
Python
130 lines
5 KiB
Python
"""Тесты reservation_lookup (#1091, #1062) — read-side lookup из land_reservation."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from contextlib import contextmanager
|
||
from datetime import date
|
||
from typing import Any
|
||
|
||
from sqlalchemy.exc import OperationalError, ProgrammingError
|
||
|
||
from app.services.site_finder.reservation_lookup import parcel_reservations
|
||
|
||
# ── Моки БД (аналог test_ird_overlay_lookup.py) ───────────────────────────────
|
||
|
||
|
||
class _Result:
|
||
def __init__(self, rows: list[dict[str, Any]]) -> None:
|
||
self._rows = rows
|
||
|
||
def mappings(self) -> _Result:
|
||
return self
|
||
|
||
def all(self) -> list[dict[str, Any]]:
|
||
return self._rows
|
||
|
||
|
||
class _FakeDB:
|
||
@contextmanager
|
||
def begin_nested(self): # type: ignore[no-untyped-def]
|
||
"""#2464: lookup оборачивает свой db.execute в SAVEPOINT (сессия общая с
|
||
analyze_parcel). Здесь достаточно пустого контекст-менеджера: эти тесты про
|
||
логику самого lookup'а. Что SAVEPOINT РЕАЛЬНО откатывает aborted-транзакцию,
|
||
проверяет tests/services/site_finder/test_2464_ird_lookups_savepoint.py на
|
||
двойнике с настоящей семантикой Postgres."""
|
||
yield
|
||
|
||
def __init__(
|
||
self,
|
||
rows: list[dict[str, Any]] | None = None,
|
||
raise_exc: Exception | None = None,
|
||
) -> None:
|
||
self._rows = rows or []
|
||
self._raise = raise_exc
|
||
|
||
def execute(self, sql: Any, params: dict[str, Any] | None = None) -> _Result:
|
||
if self._raise is not None:
|
||
raise self._raise
|
||
return _Result(self._rows)
|
||
|
||
|
||
# ── Фикстура строк ────────────────────────────────────────────────────────────
|
||
|
||
_ROWS = [
|
||
{
|
||
"reservation_kind": "изъятие",
|
||
"basis_act": "Постановление Правительства СО № 509-ПП",
|
||
"act_number": "509-ПП",
|
||
"act_date": date(2024, 3, 12),
|
||
"purpose": "реконструкция улицы Татищева",
|
||
"doc_url": "https://pravo.gov66.ru/documents/view/509-pp",
|
||
},
|
||
{
|
||
"reservation_kind": "резервирование",
|
||
"basis_act": "Постановление Правительства СО № 210-ПП",
|
||
"act_number": "210-ПП",
|
||
"act_date": date(2024, 6, 5),
|
||
"purpose": "строительство дороги",
|
||
"doc_url": "https://pravo.gov66.ru/documents/view/210-pp",
|
||
},
|
||
]
|
||
|
||
|
||
# ── Тесты ────────────────────────────────────────────────────────────────────
|
||
|
||
|
||
def test_maps_rows_correctly() -> None:
|
||
"""Строки из БД корректно маппируются в список словарей."""
|
||
result = parcel_reservations(_FakeDB(_ROWS), "66:41:0101001:123")
|
||
assert len(result) == 2
|
||
assert result[0]["reservation_kind"] == "изъятие"
|
||
assert result[0]["act_number"] == "509-ПП"
|
||
assert result[0]["act_date"] == date(2024, 3, 12)
|
||
assert result[1]["reservation_kind"] == "резервирование"
|
||
|
||
|
||
def test_expected_keys_present() -> None:
|
||
"""Каждый элемент содержит все ожидаемые ключи."""
|
||
result = parcel_reservations(_FakeDB(_ROWS), "66:41:0101001:123")
|
||
expected_keys = {
|
||
"reservation_kind",
|
||
"basis_act",
|
||
"act_number",
|
||
"act_date",
|
||
"purpose",
|
||
"doc_url",
|
||
}
|
||
for item in result:
|
||
assert set(item.keys()) == expected_keys
|
||
|
||
|
||
def test_empty_when_no_rows() -> None:
|
||
"""Нет записей в БД → пустой список."""
|
||
result = parcel_reservations(_FakeDB([]), "66:41:0101001:999")
|
||
assert result == []
|
||
|
||
|
||
def test_empty_when_cad_num_none() -> None:
|
||
"""cad_num=None → пустой список, БД не вызывается."""
|
||
result = parcel_reservations(_FakeDB(_ROWS), None)
|
||
assert result == []
|
||
|
||
|
||
def test_empty_when_cad_num_empty_string() -> None:
|
||
"""cad_num='' → пустой список (falsy guard)."""
|
||
result = parcel_reservations(_FakeDB(_ROWS), "")
|
||
assert result == []
|
||
|
||
|
||
def test_graceful_on_operational_error() -> None:
|
||
"""OperationalError (таблица не задеплоена) → [] без падения."""
|
||
db = _FakeDB(raise_exc=OperationalError("stmt", {}, Exception("no such table")))
|
||
result = parcel_reservations(db, "66:41:0101001:123")
|
||
assert result == []
|
||
|
||
|
||
def test_graceful_on_programming_error() -> None:
|
||
"""ProgrammingError → [] без падения."""
|
||
db = _FakeDB(raise_exc=ProgrammingError("stmt", {}, Exception("column not found")))
|
||
result = parcel_reservations(db, "66:41:0101001:123")
|
||
assert result == []
|