020 update city code resolving

This commit is contained in:
Раис Юсупалиев
2026-03-21 10:03:46 +03:00
parent c4175121a0
commit 163f379a65
14 changed files with 45939 additions and 271 deletions
@@ -11,9 +11,10 @@ from app.adapters.delivery_providers.cdek.client import (
CDEKProvider,
CDEKRequestError,
)
from app.cities import cities_map
from app import config as config_module
from app.config import AdapterConfig, Settings
from app.schemas.request import DeliveryEntity, DeliveryRequest
from app.schemas.request import DeliveryCalculationRequest, DeliveryEntity
class StubAuthClient:
@@ -62,28 +63,14 @@ class SequenceHTTPClient:
headers: dict[str, str] | None = None,
timeout: float | None = None,
) -> httpx.Response:
self.calls.append(
{
"method": "GET",
"url": url,
"json": None,
"data": None,
"params": params,
"headers": headers,
"timeout": timeout,
}
)
result = self._next_result()
if isinstance(result, Exception):
raise result
return result
raise AssertionError("CDEK city lookup API must not be called.")
class RecordingHTTPClient:
def __init__(self) -> None:
self.auth_timeouts: list[float | None] = []
self.city_lookup_timeouts: list[float | None] = []
self.tariff_timeouts: list[float | None] = []
self.tariff_payloads: list[dict[str, Any]] = []
async def get(
self,
@@ -93,16 +80,7 @@ class RecordingHTTPClient:
headers: dict[str, str] | None = None,
timeout: float | None = None,
) -> httpx.Response:
request = httpx.Request("GET", url, params=params)
self.city_lookup_timeouts.append(timeout)
assert headers == {"Authorization": "Bearer provider-token"}
city = params.get("name") if params else None
code_by_city = {"Moscow": 44, "Kazan": 424}
return httpx.Response(
200,
json={"code": code_by_city.get(city, 44), "full_name": city},
request=request,
)
raise AssertionError("CDEK city lookup API must not be called.")
async def post(
self,
@@ -125,6 +103,7 @@ class RecordingHTTPClient:
assert headers == {"Authorization": "Bearer provider-token"}
assert data is None
assert json is not None
self.tariff_payloads.append(json)
return httpx.Response(
200,
json={
@@ -149,33 +128,29 @@ class RecordingHTTPClient:
)
def _make_request(**overrides: object) -> DeliveryRequest:
def _make_request(**overrides: object) -> DeliveryCalculationRequest:
payload: dict[str, object] = {
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": "Moscow",
"to_city": "Kazan",
"country_code": None,
"from_city": 1,
"to_city": 2,
"weight_kg": 1.2,
"length_cm": 20,
"width_cm": 10,
"height_cm": 5,
}
payload.update(overrides)
return DeliveryRequest(**payload)
return DeliveryCalculationRequest(**payload)
def _cdek_code(city_id: str) -> int:
city_entry = cities_map[city_id]
cdek_data = city_entry["cdek"]
assert isinstance(cdek_data, dict)
return int(cdek_data["code"])
def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
request = _make_request()
city_lookup_moscow = httpx.Response(
200,
json={"code": 44, "full_name": "Moscow"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
city_lookup_kazan = httpx.Response(
200,
json={"code": 424, "full_name": "Kazan"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
first_error = httpx.ReadTimeout(
"read timeout",
request=httpx.Request("POST", "https://api.cdek.test/v2/calculator/tarifflist"),
@@ -185,9 +160,7 @@ def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
json={"tariff_codes": [{"delivery_sum": 500}]},
request=httpx.Request("POST", "https://api.cdek.test/v2/calculator/tarifflist"),
)
http_client = SequenceHTTPClient(
[city_lookup_moscow, city_lookup_kazan, first_error, second_response]
)
http_client = SequenceHTTPClient([first_error, second_response])
sleep_calls: list[float] = []
async def fake_sleep(seconds: float) -> None:
@@ -206,37 +179,22 @@ def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
result = asyncio.run(client.get_raw_price(request))
assert result == {"tariff_codes": [{"delivery_sum": 500}]}
assert len(http_client.calls) == 4
assert http_client.calls[0]["method"] == "GET"
assert http_client.calls[0]["params"] == {
"name": "Moscow",
}
assert http_client.calls[1]["method"] == "GET"
assert http_client.calls[2]["method"] == "POST"
assert http_client.calls[2]["json"]["from_location"] == {"code": 44}
assert http_client.calls[2]["json"]["to_location"] == {"code": 424}
assert http_client.calls[2]["timeout"] == 10.0
assert len(http_client.calls) == 2
assert http_client.calls[0]["method"] == "POST"
assert http_client.calls[0]["json"]["from_location"] == {"code": _cdek_code("1")}
assert http_client.calls[0]["json"]["to_location"] == {"code": _cdek_code("2")}
assert http_client.calls[0]["timeout"] == 10.0
assert sleep_calls == [0.5]
def test_cdek_client_passes_country_code_when_provided() -> None:
request = _make_request(country_code="KZ")
city_lookup_from = httpx.Response(
200,
json={"code": 88, "full_name": "Moscow"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
city_lookup_to = httpx.Response(
200,
json={"code": 99, "full_name": "Kazan"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
def test_cdek_client_builds_payload_from_cities_map_codes() -> None:
request = _make_request()
tariff_response = httpx.Response(
200,
json={"tariff_codes": [{"delivery_sum": 500}]},
request=httpx.Request("POST", "https://api.cdek.test/v2/calculator/tarifflist"),
)
http_client = SequenceHTTPClient([city_lookup_from, city_lookup_to, tariff_response])
http_client = SequenceHTTPClient([tariff_response])
client = CDEKClient(
http_client=http_client, # type: ignore[arg-type]
auth_client=StubAuthClient(), # type: ignore[arg-type]
@@ -246,30 +204,54 @@ def test_cdek_client_passes_country_code_when_provided() -> None:
asyncio.run(client.get_raw_price(request))
assert http_client.calls[0]["params"] == {
"name": "Moscow",
"country_code": "KZ",
}
assert http_client.calls[0]["json"]["from_location"] == {"code": _cdek_code("1")}
assert http_client.calls[0]["json"]["to_location"] == {"code": _cdek_code("2")}
assert http_client.calls[0]["json"]["packages"] == [
{"weight": 1200, "length": 20, "width": 10, "height": 5}
]
assert http_client.calls[0]["params"] is None
def test_cdek_client_raises_when_city_mapping_is_missing() -> None:
request = _make_request(from_city=999999)
http_client = SequenceHTTPClient([])
client = CDEKClient(
http_client=http_client, # type: ignore[arg-type]
auth_client=StubAuthClient(), # type: ignore[arg-type]
base_url="https://api.cdek.test/v2",
retry_attempts=0,
)
with pytest.raises(CDEKRequestError, match="city id 999999"):
asyncio.run(client.get_raw_price(request))
assert http_client.calls == []
def test_cdek_client_raises_when_cdek_code_is_not_configured() -> None:
request = _make_request(to_city=3)
http_client = SequenceHTTPClient([])
client = CDEKClient(
http_client=http_client, # type: ignore[arg-type]
auth_client=StubAuthClient(), # type: ignore[arg-type]
base_url="https://api.cdek.test/v2",
retry_attempts=0,
)
with pytest.raises(CDEKRequestError, match="city id 3"):
asyncio.run(client.get_raw_price(request))
assert http_client.calls == []
def test_cdek_client_raises_on_non_retriable_http_error() -> None:
request = _make_request()
city_lookup_moscow = httpx.Response(
200,
json={"code": 44, "full_name": "Moscow"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
city_lookup_kazan = httpx.Response(
200,
json={"code": 424, "full_name": "Kazan"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
bad_response = httpx.Response(
400,
json={"error": "bad request"},
request=httpx.Request("POST", "https://api.cdek.test/v2/calculator/tarifflist"),
)
http_client = SequenceHTTPClient([city_lookup_moscow, city_lookup_kazan, bad_response])
http_client = SequenceHTTPClient([bad_response])
client = CDEKClient(
http_client=http_client, # type: ignore[arg-type]
auth_client=StubAuthClient(), # type: ignore[arg-type]
@@ -280,26 +262,7 @@ def test_cdek_client_raises_on_non_retriable_http_error() -> None:
with pytest.raises(CDEKClientError):
asyncio.run(client.get_raw_price(request))
assert len(http_client.calls) == 3
def test_cdek_client_raises_when_city_code_not_found() -> None:
request = _make_request()
city_lookup_empty = httpx.Response(
200,
json=[],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
http_client = SequenceHTTPClient([city_lookup_empty])
client = CDEKClient(
http_client=http_client, # type: ignore[arg-type]
auth_client=StubAuthClient(), # type: ignore[arg-type]
base_url="https://api.cdek.test/v2",
retry_attempts=0,
)
with pytest.raises(CDEKRequestError, match="no matches"):
asyncio.run(client.get_raw_price(request))
assert len(http_client.calls) == 1
def test_provider_uses_adapter_yaml_config_for_timeout_and_cache_ttl(
@@ -353,8 +316,17 @@ observability:
assert provider.cache_ttl_seconds == 777
assert http_client.auth_timeouts == [7.5]
assert http_client.city_lookup_timeouts == [7.5, 7.5]
assert http_client.tariff_timeouts == [7.5]
assert http_client.tariff_payloads == [
{
"type": 2,
"from_location": {"code": _cdek_code("1")},
"to_location": {"code": _cdek_code("2")},
"packages": [
{"weight": 1200, "length": 20, "width": 10, "height": 5}
],
}
]
assert [price.provider for price in result] == ["cdek", "cdek"]
assert [price.service_name for price in result] == ["Express", "Economy"]
assert [price.price for price in result] == [Decimal("899.00"), Decimal("499.00")]
@@ -375,6 +347,5 @@ def test_provider_uses_default_adapter_timeout_and_cache_ttl() -> None:
assert provider.cache_ttl_seconds == 900
assert http_client.auth_timeouts == [10.0]
assert http_client.city_lookup_timeouts == [10.0, 10.0]
assert http_client.tariff_timeouts == [10.0]
assert len(result) == 2
+25 -18
View File
@@ -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:
+9 -13
View File
@@ -7,7 +7,7 @@ from app.domain.price import (
normalize_delivery_request,
sort_prices_by_price,
)
from app.schemas.request import DeliveryEntity, DeliveryRequest, ParcelType
from app.schemas.request import DeliveryCalculationRequest, DeliveryEntity, ParcelType
from app.schemas.response import DeliveryPrice
@@ -24,19 +24,18 @@ def _make_price(**overrides) -> DeliveryPrice:
return DeliveryPrice.model_construct(**payload)
def _make_request(**overrides) -> DeliveryRequest:
def _make_request(**overrides) -> DeliveryCalculationRequest:
payload = {
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": " Moscow ",
"to_city": " Saint Petersburg ",
"country_code": " ru ",
"from_city": 1,
"to_city": 2,
"weight_kg": 1.235,
"length_cm": 10.04,
"width_cm": 20.05,
"height_cm": 30.06,
}
payload.update(overrides)
return DeliveryRequest(**payload)
return DeliveryCalculationRequest(**payload)
def test_normalize_delivery_request_applies_rules_without_side_effects() -> None:
@@ -45,20 +44,18 @@ def test_normalize_delivery_request_applies_rules_without_side_effects() -> None
normalized = normalize_delivery_request(request, weight_round_scale=2)
assert normalized is not request
assert normalized.from_city == "Moscow"
assert normalized.to_city == "Saint Petersburg"
assert normalized.country_code == "RU"
assert normalized.from_city == 1
assert normalized.to_city == 2
assert normalized.weight_kg == Decimal("1.24")
assert normalized.length_cm == Decimal("10.0")
assert normalized.width_cm == Decimal("20.1")
assert normalized.height_cm == Decimal("30.1")
assert request.from_city == " Moscow "
assert request.country_code == " ru "
assert request.from_city == 1
assert request.to_city == 2
def test_normalize_delivery_request_handles_normalization_boundaries() -> None:
request = _make_request(
country_code=" ",
weight_kg=0.0001,
length_cm=0.01,
width_cm=0.04,
@@ -73,7 +70,6 @@ def test_normalize_delivery_request_handles_normalization_boundaries() -> None:
assert low_scale.width_cm == Decimal("0.1")
assert low_scale.height_cm == Decimal("0.2")
assert high_scale.weight_kg == Decimal("0.01")
assert low_scale.country_code is None
def test_filter_valid_prices_handles_empty_input() -> None:
+26 -12
View File
@@ -4,7 +4,12 @@ from decimal import Decimal
import pytest
from app.adapters.delivery_providers.base import ProviderRequestError
from app.schemas.request import DeliveryEntity, DeliveryRequest, ParcelType
from app.domain.price import normalize_delivery_request
from app.schemas.request import (
DeliveryCalculationRequest,
DeliveryEntity,
ParcelType,
)
from app.schemas.response import DeliveryPrice
from app.services.aggregator import AggregatorService, InvalidDeliveryRequestError
@@ -25,9 +30,11 @@ class StubProvider:
self._response = response
self._error = error
self.cache_ttl_seconds = cache_ttl_seconds
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)
if self._error is not None:
raise self._error
@@ -58,12 +65,11 @@ class StubCache:
self._storage[key] = value
def _make_request(**overrides: object) -> DeliveryRequest:
def _make_request(**overrides: object) -> DeliveryCalculationRequest:
payload: dict[str, object] = {
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": "Moscow",
"to_city": "Kazan",
"country_code": None,
"from_city": 1,
"to_city": 2,
"weight_kg": 1.234,
"length_cm": 30.0,
"width_cm": 20.0,
@@ -71,7 +77,7 @@ def _make_request(**overrides: object) -> DeliveryRequest:
"parcel_type": None,
}
payload.update(overrides)
return DeliveryRequest(**payload)
return DeliveryCalculationRequest(**payload)
def _make_price(provider: str, price: str, *, service_name: str = "standard") -> DeliveryPrice:
@@ -118,15 +124,23 @@ def test_get_all_prices_full_success_returns_sorted_and_updates_cache() -> None:
assert [ttl for _, _, ttl in cache.set_calls] == [111, 222]
def test_get_all_prices_normalizes_and_forwards_country_code_to_providers() -> None:
def test_get_all_prices_forwards_city_identifiers_and_uses_updated_cache_key() -> None:
provider = StubProvider(name="cdek", response=[_make_price("cdek", "150.00")])
service = AggregatorService([provider], cache=StubCache())
cache = StubCache()
service = AggregatorService([provider], cache=cache)
request = _make_request()
result = asyncio.run(service.get_all_prices(_make_request(country_code=" kz ")))
result = asyncio.run(service.get_all_prices(request))
assert [price.provider for price in result] == ["cdek"]
assert len(provider.calls) == 1
assert provider.calls[0].country_code == "KZ"
assert provider.calls[0].from_city == 1
assert provider.calls[0].to_city == 2
expected_key = AggregatorService._build_cache_key(
provider_name=provider.name,
request=normalize_delivery_request(request),
)
assert cache.get_calls == [expected_key]
def test_get_all_prices_partial_failure_excludes_failed_provider() -> None: