020 update city code resolving
This commit is contained in:
@@ -7,7 +7,7 @@ import pytest
|
||||
from app.controllers.v1 import delivery as delivery_controller
|
||||
from app.controllers.v1.delivery import get_aggregator_service
|
||||
from app.main import create_app
|
||||
from app.schemas.request import DeliveryEntity, DeliveryRequest, ParcelType
|
||||
from app.schemas.request import DeliveryCalculationRequest, DeliveryEntity, ParcelType
|
||||
from app.schemas.response import DeliveryPrice
|
||||
from app.services.aggregator import (
|
||||
AggregatorService,
|
||||
@@ -20,9 +20,9 @@ class StubAggregatorService:
|
||||
def __init__(self, *, response: object, error: Exception | None = None) -> None:
|
||||
self._response = response
|
||||
self._error = error
|
||||
self.calls: list[DeliveryRequest] = []
|
||||
self.calls: list[DeliveryCalculationRequest] = []
|
||||
|
||||
async def get_all_prices(self, request: DeliveryRequest) -> object:
|
||||
async def get_all_prices(self, request: DeliveryCalculationRequest) -> object:
|
||||
self.calls.append(request)
|
||||
if self._error is not None:
|
||||
raise self._error
|
||||
@@ -34,9 +34,11 @@ class StubPriceProvider:
|
||||
self.name = "stub-provider"
|
||||
self.cache_ttl_seconds = 900
|
||||
self._response = response
|
||||
self.calls: list[DeliveryRequest] = []
|
||||
self.calls: list[DeliveryCalculationRequest] = []
|
||||
|
||||
async def get_prices(self, request: DeliveryRequest) -> list[DeliveryPrice]:
|
||||
async def get_prices(
|
||||
self, request: DeliveryCalculationRequest
|
||||
) -> list[DeliveryPrice]:
|
||||
self.calls.append(request)
|
||||
return self._response
|
||||
|
||||
@@ -51,8 +53,8 @@ def _install_service_override(app, service: StubAggregatorService) -> None:
|
||||
def _valid_payload() -> dict[str, object]:
|
||||
return {
|
||||
"entity": "individual",
|
||||
"from_city": "Moscow",
|
||||
"to_city": "Kazan",
|
||||
"from_city": 1,
|
||||
"to_city": 2,
|
||||
"weight_kg": 2.5,
|
||||
"length_cm": 30.0,
|
||||
"width_cm": 20.0,
|
||||
@@ -91,9 +93,11 @@ def test_post_delivery_price_uses_registered_provider_in_default_dependency(
|
||||
cache_ttl_seconds = 900
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[DeliveryRequest] = []
|
||||
self.calls: list[DeliveryCalculationRequest] = []
|
||||
|
||||
async def get_prices(self, request: DeliveryRequest) -> list[DeliveryPrice]:
|
||||
async def get_prices(
|
||||
self, request: DeliveryCalculationRequest
|
||||
) -> list[DeliveryPrice]:
|
||||
self.calls.append(request)
|
||||
return [
|
||||
DeliveryPrice(
|
||||
@@ -230,11 +234,10 @@ def test_post_delivery_price_returns_prices_and_delegates_to_service() -> None:
|
||||
assert response.status_code == 200
|
||||
assert response.json() == [price.model_dump(mode="json") for price in expected_prices]
|
||||
assert len(service.calls) == 1
|
||||
assert service.calls[0] == DeliveryRequest(
|
||||
assert service.calls[0] == DeliveryCalculationRequest(
|
||||
entity=DeliveryEntity.INDIVIDUAL,
|
||||
from_city="Moscow",
|
||||
to_city="Kazan",
|
||||
country_code=None,
|
||||
from_city=1,
|
||||
to_city=2,
|
||||
weight_kg=2.5,
|
||||
length_cm=30.0,
|
||||
width_cm=20.0,
|
||||
@@ -243,12 +246,16 @@ def test_post_delivery_price_returns_prices_and_delegates_to_service() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_post_delivery_price_accepts_optional_country_code() -> None:
|
||||
@pytest.mark.parametrize(("field_name", "field_value"), [("from_city", "1"), ("to_city", "2")])
|
||||
def test_post_delivery_price_rejects_non_integer_city_identifier(
|
||||
field_name: str,
|
||||
field_value: str,
|
||||
) -> None:
|
||||
service = StubAggregatorService(response=[])
|
||||
app = create_app()
|
||||
_install_service_override(app, service)
|
||||
payload = _valid_payload()
|
||||
payload["country_code"] = "kz"
|
||||
payload[field_name] = field_value
|
||||
|
||||
async def run_request() -> httpx.Response:
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
@@ -260,9 +267,9 @@ def test_post_delivery_price_accepts_optional_country_code() -> None:
|
||||
|
||||
response = asyncio.run(run_request())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert len(service.calls) == 1
|
||||
assert service.calls[0].country_code == "kz"
|
||||
assert response.status_code == 422
|
||||
assert response.json()["detail"][0]["loc"] == ["body", field_name]
|
||||
assert service.calls == []
|
||||
|
||||
|
||||
def test_post_delivery_price_accepts_optional_parcel_type() -> None:
|
||||
|
||||
Reference in New Issue
Block a user