fix country code

This commit is contained in:
Раис Юсупалиев
2026-03-09 15:24:10 +03:00
parent 2d68aff34d
commit da301d4bc4
8 changed files with 155 additions and 45 deletions
@@ -94,11 +94,11 @@ class RecordingHTTPClient:
request = httpx.Request("GET", url, params=params)
self.city_lookup_timeouts.append(timeout)
assert headers == {"Authorization": "Bearer provider-token"}
city = params.get("city") if params else None
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), "city": city}],
json={"code": code_by_city.get(city, 44), "full_name": city},
request=request,
)
@@ -140,29 +140,32 @@ class RecordingHTTPClient:
)
def _make_request() -> DeliveryRequest:
return DeliveryRequest(
entity=DeliveryEntity.INDIVIDUAL,
from_city="Moscow",
to_city="Kazan",
weight_kg=1.2,
length_cm=20,
width_cm=10,
height_cm=5,
)
def _make_request(**overrides: object) -> DeliveryRequest:
payload: dict[str, object] = {
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": "Moscow",
"to_city": "Kazan",
"country_code": None,
"weight_kg": 1.2,
"length_cm": 20,
"width_cm": 10,
"height_cm": 5,
}
payload.update(overrides)
return DeliveryRequest(**payload)
def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
request = _make_request()
city_lookup_moscow = httpx.Response(
200,
json=[{"code": 44, "city": "Moscow"}],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/cities"),
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, "city": "Kazan"}],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/cities"),
json={"code": 424, "full_name": "Kazan"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
first_error = httpx.ReadTimeout(
"read timeout",
@@ -197,9 +200,7 @@ def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
assert len(http_client.calls) == 4
assert http_client.calls[0]["method"] == "GET"
assert http_client.calls[0]["params"] == {
"city": "Moscow",
"country_codes": "RU",
"size": 1,
"name": "Moscow",
}
assert http_client.calls[1]["method"] == "GET"
assert http_client.calls[2]["method"] == "POST"
@@ -209,17 +210,50 @@ def test_cdek_client_retries_timeout_and_applies_timeout_seconds() -> None:
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"),
)
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])
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,
)
asyncio.run(client.get_raw_price(request))
assert http_client.calls[0]["params"] == {
"name": "Moscow",
"country_code": "KZ",
}
def test_cdek_client_raises_on_non_retriable_http_error() -> None:
request = _make_request()
city_lookup_moscow = httpx.Response(
200,
json=[{"code": 44, "city": "Moscow"}],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/cities"),
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, "city": "Kazan"}],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/cities"),
json={"code": 424, "full_name": "Kazan"},
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
bad_response = httpx.Response(
400,
@@ -245,7 +279,7 @@ def test_cdek_client_raises_when_city_code_not_found() -> None:
city_lookup_empty = httpx.Response(
200,
json=[],
request=httpx.Request("GET", "https://api.cdek.test/v2/location/cities"),
request=httpx.Request("GET", "https://api.cdek.test/v2/location/suggest/cities"),
)
http_client = SequenceHTTPClient([city_lookup_empty])
client = CDEKClient(
+23
View File
@@ -177,6 +177,7 @@ def test_post_delivery_price_returns_prices_and_delegates_to_service() -> None:
entity=DeliveryEntity.INDIVIDUAL,
from_city="Moscow",
to_city="Kazan",
country_code=None,
weight_kg=2.5,
length_cm=30.0,
width_cm=20.0,
@@ -184,6 +185,28 @@ def test_post_delivery_price_returns_prices_and_delegates_to_service() -> None:
)
def test_post_delivery_price_accepts_optional_country_code() -> None:
service = StubAggregatorService(response=[])
app = create_app()
_install_service_override(app, service)
payload = _valid_payload()
payload["country_code"] = "kz"
async def run_request() -> httpx.Response:
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(
transport=transport,
base_url="http://testserver",
) as client:
return await client.post("/api/v1/delivery/price", json=payload)
response = asyncio.run(run_request())
assert response.status_code == 200
assert len(service.calls) == 1
assert service.calls[0].country_code == "kz"
def test_post_delivery_price_rejects_invalid_payload() -> None:
service = StubAggregatorService(response=[])
app = create_app()
+5
View File
@@ -28,6 +28,7 @@ def _make_request(**overrides) -> DeliveryRequest:
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": " Moscow ",
"to_city": " Saint Petersburg ",
"country_code": " ru ",
"weight_kg": 1.235,
"length_cm": 10.04,
"width_cm": 20.05,
@@ -45,15 +46,18 @@ def test_normalize_delivery_request_applies_rules_without_side_effects() -> None
assert normalized is not request
assert normalized.from_city == "Moscow"
assert normalized.to_city == "Saint Petersburg"
assert normalized.country_code == "RU"
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 "
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,
@@ -68,6 +72,7 @@ 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:
+24 -10
View File
@@ -58,16 +58,19 @@ class StubCache:
self._storage[key] = value
def _make_request() -> DeliveryRequest:
return DeliveryRequest(
entity=DeliveryEntity.INDIVIDUAL,
from_city="Moscow",
to_city="Kazan",
weight_kg=1.234,
length_cm=30.0,
width_cm=20.0,
height_cm=10.0,
)
def _make_request(**overrides: object) -> DeliveryRequest:
payload: dict[str, object] = {
"entity": DeliveryEntity.INDIVIDUAL,
"from_city": "Moscow",
"to_city": "Kazan",
"country_code": None,
"weight_kg": 1.234,
"length_cm": 30.0,
"width_cm": 20.0,
"height_cm": 10.0,
}
payload.update(overrides)
return DeliveryRequest(**payload)
def _make_price(provider: str, price: str) -> DeliveryPrice:
@@ -96,6 +99,17 @@ 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:
provider = StubProvider(name="cdek", response=_make_price("cdek", "150.00"))
service = AggregatorService([provider], cache=StubCache())
result = asyncio.run(service.get_all_prices(_make_request(country_code=" kz ")))
assert [price.provider for price in result] == ["cdek"]
assert len(provider.calls) == 1
assert provider.calls[0].country_code == "KZ"
def test_get_all_prices_partial_failure_excludes_failed_provider() -> None:
provider_ok = StubProvider(name="ok", response=_make_price("ok", "150.00"))
provider_failed = StubProvider(name="failed", error=RuntimeError("provider unavailable"))