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(