Require parent_id or parent_fias_id when searching houses.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
7e892fc13a
commit
1e9e3052ee
|
|
@ -70,10 +70,19 @@ class LocalitySearchParams(SearchParams):
|
|||
region_id: int
|
||||
|
||||
|
||||
HOUSE_PARENT_REQUIRED = "Для поиска домов необходимо указать parent_id или parent_fias_id"
|
||||
|
||||
|
||||
class HouseSearchParams(SearchParams):
|
||||
"""Параметры поиска домов. Фильтры parent_* относятся к улице или планировке."""
|
||||
|
||||
|
||||
def require_house_parent(params: HouseSearchParams) -> None:
|
||||
if params.parent_id or params.parent_fias_id:
|
||||
return
|
||||
raise HttpError(422, HOUSE_PARENT_REQUIRED)
|
||||
|
||||
|
||||
LOCALITY_LEVELS = (
|
||||
AddressObject.Level.DISTRICT,
|
||||
AddressObject.Level.CITY,
|
||||
|
|
@ -399,6 +408,7 @@ def streets(request, params: Query[SearchParams]):
|
|||
|
||||
|
||||
def search_houses(params: HouseSearchParams) -> HouseSearchResponse:
|
||||
require_house_parent(params)
|
||||
return cached_search(make_search_key("houses", params), lambda: _search_houses(params))
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
from uuid import UUID
|
||||
|
||||
import grpc
|
||||
from ninja.errors import HttpError
|
||||
|
||||
from address.api import (
|
||||
HOUSE_PARENT_REQUIRED,
|
||||
LOCALITY_LEVELS,
|
||||
PARENT_CHAIN,
|
||||
STREET_LEVELS,
|
||||
|
|
@ -188,7 +190,10 @@ class AddressServicer(address_pb2_grpc.AddressServiceServicer):
|
|||
params = _search_params(request)
|
||||
except ValueError:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, "Некорректный fias_id")
|
||||
try:
|
||||
result = search_houses(params)
|
||||
except HttpError as exc:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc) or HOUSE_PARENT_REQUIRED)
|
||||
return address_pb2.HouseSearchResponse(
|
||||
count=result.count,
|
||||
items=[_to_house(item) for item in result.items],
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import uuid
|
|||
import grpc
|
||||
import pytest
|
||||
|
||||
from address.api import HOUSE_PARENT_REQUIRED
|
||||
from address.grpc import address_pb2, address_pb2_grpc
|
||||
from address.grpc.server import create_server
|
||||
from address.grpc.servicer import AddressServicer
|
||||
|
|
@ -236,7 +237,7 @@ def test_search_houses_by_parent_kladr_id(addresses):
|
|||
)
|
||||
|
||||
response = AddressServicer().SearchHouses(
|
||||
address_pb2.SearchRequest(parent_kladr_id=street.kladr_id),
|
||||
address_pb2.SearchRequest(parent_kladr_id=street.kladr_id, parent_id=street.pk),
|
||||
_Context(),
|
||||
)
|
||||
|
||||
|
|
@ -276,7 +277,7 @@ def test_search_houses_by_fias_id(addresses):
|
|||
)
|
||||
|
||||
response = AddressServicer().SearchHouses(
|
||||
address_pb2.SearchRequest(fias_id=str(house.fias_id)),
|
||||
address_pb2.SearchRequest(fias_id=str(house.fias_id), parent_id=street.pk),
|
||||
_Context(),
|
||||
)
|
||||
|
||||
|
|
@ -284,6 +285,18 @@ def test_search_houses_by_fias_id(addresses):
|
|||
assert response.items[0].fias_id == str(house.fias_id)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_search_houses_require_parent():
|
||||
with pytest.raises(grpc.RpcError) as error:
|
||||
AddressServicer().SearchHouses(
|
||||
address_pb2.SearchRequest(query="10"),
|
||||
_Context(),
|
||||
)
|
||||
|
||||
assert error.value.code() == grpc.StatusCode.INVALID_ARGUMENT
|
||||
assert error.value.details() == HOUSE_PARENT_REQUIRED
|
||||
|
||||
|
||||
def test_health():
|
||||
response = AddressServicer().Health(address_pb2.HealthRequest(), _Context())
|
||||
assert response.status == "ok"
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import uuid
|
|||
import pytest
|
||||
from django.test import Client
|
||||
|
||||
from address.api import HOUSE_PARENT_REQUIRED
|
||||
from address.models import AddressObject, House
|
||||
from address.tests.paths import api_url
|
||||
|
||||
|
|
@ -95,7 +96,7 @@ def test_houses_are_searched_by_parent_kladr_id():
|
|||
house_number="10",
|
||||
)
|
||||
|
||||
response = Client().get(api_url("houses"), {"parent_kladr_id": street.kladr_id})
|
||||
response = Client().get(api_url("houses"), {"parent_kladr_id": street.kladr_id, "parent_id": street.pk})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["count"] == 1
|
||||
|
|
@ -167,8 +168,24 @@ def test_houses_are_searched_by_fias_id():
|
|||
house_number="11",
|
||||
)
|
||||
|
||||
response = Client().get(api_url("houses"), {"fias_id": str(house.fias_id)})
|
||||
response = Client().get(api_url("houses"), {"fias_id": str(house.fias_id), "parent_id": street.pk})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["count"] == 1
|
||||
assert response.json()["items"][0]["object_id"] == house.object_id
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_houses_require_parent_id_or_parent_fias_id():
|
||||
response = Client().get(api_url("houses"), {"query": "10"})
|
||||
|
||||
assert response.status_code == 422
|
||||
assert response.json() == {"detail": HOUSE_PARENT_REQUIRED}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_houses_reject_parent_kladr_id_without_parent():
|
||||
response = Client().get(api_url("houses"), {"parent_kladr_id": "25000001000001700"})
|
||||
|
||||
assert response.status_code == 422
|
||||
assert response.json() == {"detail": HOUSE_PARENT_REQUIRED}
|
||||
|
|
|
|||
Loading…
Reference in New Issue