Добавлен tomtom

This commit is contained in:
Раис Юсупалиев
2026-04-03 04:19:55 +03:00
parent 44893e01ae
commit db1c74f1ce
16 changed files with 741 additions and 30 deletions
@@ -0,0 +1,258 @@
import asyncio
from typing import Any
import httpx
import pytest
from app.adapters.address_suggestions.tomtom.client import (
TomTomAddressSuggestionProvider,
TomTomClientError,
TomTomRequestError,
)
from app.config import TomTomAddressSuggestionsConfig
from app.schemas.request import AddressSuggestRequest
from app.schemas.response import AddressSuggestion
class SequenceHTTPClient:
def __init__(self, results: list[Any]) -> None:
self._results = results
self.calls: list[dict[str, Any]] = []
def _next_result(self) -> Any:
return self._results[len(self.calls) - 1]
async def get(
self,
url: str,
*,
params: dict[str, Any] | None = None,
timeout: float | None = None,
) -> httpx.Response:
self.calls.append(
{
"method": "GET",
"url": url,
"params": params,
"timeout": timeout,
}
)
result = self._next_result()
if isinstance(result, Exception):
raise result
return result
def _make_request(**overrides: object) -> AddressSuggestRequest:
payload: dict[str, object] = {
"country_code": "DE",
"city": "Berlin",
"query": "Alexanderplatz 1",
"limit": 5,
}
payload.update(overrides)
return AddressSuggestRequest(**payload)
def test_tomtom_provider_maps_response_to_unified_model() -> None:
response = httpx.Response(
200,
json={
"results": [
{
"address": {
"freeformAddress": "Alexanderplatz 1, 10178 Berlin",
"streetName": "Alexanderplatz",
"streetNumber": "1",
"postalCode": "10178",
}
}
]
},
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
),
)
http_client = SequenceHTTPClient([response])
provider = TomTomAddressSuggestionProvider(
http_client=http_client, # type: ignore[arg-type]
url="https://api.tomtom.test/search/2/search",
api_key="test-api-key",
timeout_seconds=5.5,
)
result = asyncio.run(provider.suggest(_make_request()))
assert result == [
AddressSuggestion(
address="Alexanderplatz 1, 10178 Berlin",
street="Alexanderplatz",
house="1",
flat=None,
postal_code="10178",
)
]
assert http_client.calls == [
{
"method": "GET",
"url": "https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
"params": {
"key": "test-api-key",
"countrySet": "DE",
"typeahead": "true",
"idxSet": "PAD,Addr,Str,EPP",
"limit": 5,
},
"timeout": 5.5,
}
]
def test_tomtom_provider_uses_config_factory_and_omits_limit_when_missing() -> None:
response = httpx.Response(
200,
json={
"results": [
{
"address": {
"freeformAddress": "Dam Square, 1012 JS Amsterdam",
"streetName": "Dam Square",
"postalCode": "1012 JS",
}
}
]
},
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Amsterdam%20Dam%20Square.json",
),
)
http_client = SequenceHTTPClient([response])
provider = TomTomAddressSuggestionProvider.from_config(
http_client=http_client, # type: ignore[arg-type]
config=TomTomAddressSuggestionsConfig(
url="https://api.tomtom.test/search/2/search",
api_key="config-api-key",
timeout_seconds=4.25,
),
)
result = asyncio.run(
provider.suggest(
_make_request(
country_code="NL",
city="Amsterdam",
query="Dam Square",
limit=None,
)
)
)
assert result == [
AddressSuggestion(
address="Dam Square, 1012 JS Amsterdam",
street="Dam Square",
house=None,
flat=None,
postal_code="1012 JS",
)
]
assert http_client.calls == [
{
"method": "GET",
"url": "https://api.tomtom.test/search/2/search/Amsterdam%20Dam%20Square.json",
"params": {
"key": "config-api-key",
"countrySet": "NL",
"typeahead": "true",
"idxSet": "PAD,Addr,Str,EPP",
},
"timeout": 4.25,
}
]
def test_tomtom_provider_maps_400_to_request_error() -> None:
response = httpx.Response(
400,
json={"errorText": "bad request"},
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
),
)
http_client = SequenceHTTPClient([response])
provider = TomTomAddressSuggestionProvider(
http_client=http_client, # type: ignore[arg-type]
url="https://api.tomtom.test/search/2/search",
api_key="test-api-key",
)
with pytest.raises(TomTomRequestError, match="status 400"):
asyncio.run(provider.suggest(_make_request()))
@pytest.mark.parametrize("status_code", [403, 429, 503])
def test_tomtom_provider_maps_unavailable_statuses_to_client_error(
status_code: int,
) -> None:
response = httpx.Response(
status_code,
json={"errorText": "provider error"},
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
),
)
http_client = SequenceHTTPClient([response])
provider = TomTomAddressSuggestionProvider(
http_client=http_client, # type: ignore[arg-type]
url="https://api.tomtom.test/search/2/search",
api_key="test-api-key",
)
with pytest.raises(TomTomClientError, match=f"status {status_code}"):
asyncio.run(provider.suggest(_make_request()))
def test_tomtom_provider_maps_transport_error_to_client_error() -> None:
transport_error = httpx.ReadTimeout(
"read timeout",
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
),
)
http_client = SequenceHTTPClient([transport_error])
provider = TomTomAddressSuggestionProvider(
http_client=http_client, # type: ignore[arg-type]
url="https://api.tomtom.test/search/2/search",
api_key="test-api-key",
)
with pytest.raises(TomTomClientError, match="request failed"):
asyncio.run(provider.suggest(_make_request()))
def test_tomtom_provider_raises_on_invalid_payload_shape() -> None:
response = httpx.Response(
200,
json={"results": [{"address": {"freeformAddress": 1}}]},
request=httpx.Request(
"GET",
"https://api.tomtom.test/search/2/search/Berlin%20Alexanderplatz%201.json",
),
)
http_client = SequenceHTTPClient([response])
provider = TomTomAddressSuggestionProvider(
http_client=http_client, # type: ignore[arg-type]
url="https://api.tomtom.test/search/2/search",
api_key="test-api-key",
)
with pytest.raises(
TomTomClientError,
match="response payload is invalid",
):
asyncio.run(provider.suggest(_make_request()))
@@ -19,6 +19,7 @@ address_suggestions:
country_to_provider:
RU: "dadata"
AM: "yandex_geosuggest"
DE: "tomtom"
dadata:
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
api_key: "default-dadata-key"
@@ -26,6 +27,10 @@ address_suggestions:
url: "https://suggest-maps.yandex.ru/v1/suggest"
api_key: "default-yandex-key"
timeout_seconds: 4.0
tomtom:
url: "https://api.tomtom.com/search/2/search"
api_key: "default-tomtom-key"
timeout_seconds: 4.25
observability:
enabled: false
@@ -19,6 +19,7 @@ address_suggestions:
country_to_provider:
RU: "dadata"
AM: "yandex_geosuggest"
DE: "tomtom"
dadata:
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
api_key: "override-dadata-key"
@@ -27,6 +28,10 @@ address_suggestions:
url: "https://suggest-maps.yandex.ru/v1/suggest"
api_key: "override-yandex-key"
timeout_seconds: 4.5
tomtom:
url: "https://api.tomtom.com/search/2/search"
api_key: "override-tomtom-key"
timeout_seconds: 5.25
observability:
enabled: true
+52 -12
View File
@@ -41,6 +41,51 @@ MISSING_OBSERVABILITY_ENDPOINT_CONFIG_FILE = (
/ "fixtures"
/ "config.missing_observability_endpoint.yaml"
)
_DADATA_COUNTRIES = ("RU", "BY", "KZ")
_YANDEX_COUNTRIES = ("AM", "AZ", "KG", "MD", "TJ", "TM", "UZ")
_TOMTOM_COUNTRIES = (
"AL",
"AT",
"BE",
"BG",
"CH",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GB",
"GR",
"HR",
"HU",
"IE",
"IT",
"LT",
"LV",
"NL",
"NO",
"PL",
"PT",
"RO",
"RS",
"SE",
"SI",
"SK",
"UA",
)
def _expected_country_mapping() -> dict[str, str]:
return {
**{country_code: "dadata" for country_code in _DADATA_COUNTRIES},
**{
country_code: "yandex_geosuggest"
for country_code in _YANDEX_COUNTRIES
},
**{country_code: "tomtom" for country_code in _TOMTOM_COUNTRIES},
}
def _use_runtime_config_files(
@@ -81,18 +126,7 @@ def test_configuration_sections_are_loaded_from_yaml_file(
assert settings.adapter.cdek_retry_backoff_seconds == 0.2
assert settings.adapter.cdek_timeout_seconds == 10.0
assert settings.adapter.cdek_cache_ttl_seconds == 900
assert settings.address_suggestions.country_to_provider == {
"RU": "dadata",
"BY": "dadata",
"KZ": "dadata",
"AM": "yandex_geosuggest",
"AZ": "yandex_geosuggest",
"KG": "yandex_geosuggest",
"MD": "yandex_geosuggest",
"TJ": "yandex_geosuggest",
"TM": "yandex_geosuggest",
"UZ": "yandex_geosuggest",
}
assert settings.address_suggestions.country_to_provider == _expected_country_mapping()
assert (
settings.address_suggestions.dadata.url
== "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
@@ -108,6 +142,9 @@ def test_configuration_sections_are_loaded_from_yaml_file(
== "test-yandex-geosuggest-api-key"
)
assert settings.address_suggestions.yandex_geosuggest.timeout_seconds == 6.5
assert settings.address_suggestions.tomtom.url == "https://api.tomtom.com/search/2/search"
assert settings.address_suggestions.tomtom.api_key == "test-tomtom-api-key"
assert settings.address_suggestions.tomtom.timeout_seconds == 5.5
assert settings.observability.enabled is False
assert settings.observability.service_name == "g2s-aggregator-test"
assert settings.observability.otlp_endpoint == "http://localhost:4317"
@@ -178,11 +215,14 @@ def test_get_settings_uses_config_test_yaml_in_pytest_environment(
assert settings.address_suggestions.country_to_provider == {
"RU": "dadata",
"AM": "yandex_geosuggest",
"DE": "tomtom",
}
assert settings.address_suggestions.dadata.api_key == "override-dadata-key"
assert settings.address_suggestions.dadata.timeout_seconds == 3.0
assert settings.address_suggestions.yandex_geosuggest.api_key == "override-yandex-key"
assert settings.address_suggestions.yandex_geosuggest.timeout_seconds == 4.5
assert settings.address_suggestions.tomtom.api_key == "override-tomtom-key"
assert settings.address_suggestions.tomtom.timeout_seconds == 5.25
assert settings.observability.enabled is True
assert settings.observability.service_name == "override-config-service"
assert settings.observability.otlp_endpoint == "http://override-collector:4317"
@@ -99,6 +99,18 @@ def test_post_address_suggest_uses_registered_provider_in_default_dependency(
_ = config
return stub_yandex_provider
class StubTomTomAddressSuggestionProvider:
@classmethod
def from_config(
cls,
*,
http_client,
config,
) -> StubAddressProvider:
assert http_client is stub_http_client
_ = config
return stub_tomtom_provider
class StubCache:
async def get(self, key: str) -> object | None:
_ = key
@@ -138,6 +150,17 @@ def test_post_address_suggest_uses_registered_provider_in_default_dependency(
)
],
)
stub_tomtom_provider = StubAddressProvider(
"tomtom",
[
AddressSuggestion(
address="Alexanderplatz 1, 10178 Berlin",
street="Alexanderplatz",
house="1",
postal_code="10178",
)
],
)
http_client_timeouts: list[float] = []
def fake_build_controller_http_client(timeout_seconds: float) -> StubHttpClient:
@@ -160,13 +183,18 @@ def test_post_address_suggest_uses_registered_provider_in_default_dependency(
"YandexGeosuggestAddressSuggestionProvider",
StubYandexGeosuggestAddressSuggestionProvider,
)
monkeypatch.setattr(
delivery_controller,
"TomTomAddressSuggestionProvider",
StubTomTomAddressSuggestionProvider,
)
monkeypatch.setattr(delivery_controller, "PriceCache", StubPriceCache)
app = create_app()
request_payload = _valid_payload(
country_code="AM",
city="Yerevan",
query="Tumanyan 12",
country_code="DE",
city="Berlin",
query="Alexanderplatz 1",
)
async def run_request() -> httpx.Response:
@@ -185,16 +213,17 @@ def test_post_address_suggest_uses_registered_provider_in_default_dependency(
assert response.status_code == 200
assert response.json() == [
{
"address": "Tumanyan Street, 12, Yerevan",
"street": "Tumanyan Street",
"house": "12",
"address": "Alexanderplatz 1, 10178 Berlin",
"street": "Alexanderplatz",
"house": "1",
"flat": None,
"postal_code": None,
"postal_code": "10178",
}
]
assert http_client_timeouts == [10.0]
assert stub_dadata_provider.calls == []
assert stub_yandex_provider.calls == [AddressSuggestRequest(**request_payload)]
assert stub_yandex_provider.calls == []
assert stub_tomtom_provider.calls == [AddressSuggestRequest(**request_payload)]
def test_post_address_suggest_returns_response_and_delegates_to_service() -> None:
+95 -2
View File
@@ -17,6 +17,38 @@ from app.services.aggregator import (
_DADATA_COUNTRIES = ("RU", "BY", "KZ")
_YANDEX_COUNTRIES = ("AM", "AZ", "KG", "MD", "TJ", "TM", "UZ")
_TOMTOM_COUNTRIES = (
"AL",
"AT",
"BE",
"BG",
"CH",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GB",
"GR",
"HR",
"HU",
"IE",
"IT",
"LT",
"LV",
"NL",
"NO",
"PL",
"PT",
"RO",
"RS",
"SE",
"SI",
"SK",
"UA",
)
class StubAddressSuggestionProvider:
@@ -74,6 +106,7 @@ def _make_country_mapping() -> dict[str, str]:
country_code: "yandex_geosuggest"
for country_code in _YANDEX_COUNTRIES
},
**{country_code: "tomtom" for country_code in _TOMTOM_COUNTRIES},
}
@@ -97,9 +130,13 @@ def test_suggest_addresses_routes_dadata_countries_to_dadata(
"yandex_geosuggest",
response=[_make_suggestion("Yerevan, Tumanyan 1")],
)
tomtom = StubAddressSuggestionProvider(
"tomtom",
response=[_make_suggestion("Alexanderplatz 1, 10178 Berlin")],
)
service = AggregatorService(
providers=[],
address_suggestion_providers=[dadata, yandex_geosuggest],
address_suggestion_providers=[dadata, yandex_geosuggest, tomtom],
address_suggestion_country_to_provider=_make_country_mapping(),
)
request = _make_request(
@@ -121,6 +158,7 @@ def test_suggest_addresses_routes_dadata_countries_to_dadata(
]
assert dadata.calls == [request]
assert yandex_geosuggest.calls == []
assert tomtom.calls == []
@pytest.mark.parametrize("country_code", _YANDEX_COUNTRIES)
@@ -135,9 +173,13 @@ def test_suggest_addresses_routes_cis_countries_to_yandex_geosuggest(
"yandex_geosuggest",
response=[_make_suggestion("Yerevan, Tumanyan 1")],
)
tomtom = StubAddressSuggestionProvider(
"tomtom",
response=[_make_suggestion("Alexanderplatz 1, 10178 Berlin")],
)
service = AggregatorService(
providers=[],
address_suggestion_providers=[dadata, yandex_geosuggest],
address_suggestion_providers=[dadata, yandex_geosuggest, tomtom],
address_suggestion_country_to_provider=_make_country_mapping(),
)
request = _make_request(country_code=country_code, city="Yerevan", query="Tumanyan")
@@ -147,6 +189,57 @@ def test_suggest_addresses_routes_cis_countries_to_yandex_geosuggest(
assert result == [AddressSuggestion(address="Yerevan, Tumanyan 1")]
assert dadata.calls == []
assert yandex_geosuggest.calls == [request]
assert tomtom.calls == []
@pytest.mark.parametrize("country_code", _TOMTOM_COUNTRIES)
def test_suggest_addresses_routes_european_countries_to_tomtom(
country_code: str,
) -> None:
dadata = StubAddressSuggestionProvider(
"dadata",
response=[_make_suggestion("Moscow, Lenina 1")],
)
yandex_geosuggest = StubAddressSuggestionProvider(
"yandex_geosuggest",
response=[_make_suggestion("Yerevan, Tumanyan 1")],
)
tomtom = StubAddressSuggestionProvider(
"tomtom",
response=[
_make_suggestion(
"Alexanderplatz 1, 10178 Berlin",
street="Alexanderplatz",
house="1",
postal_code="10178",
)
],
)
service = AggregatorService(
providers=[],
address_suggestion_providers=[dadata, yandex_geosuggest, tomtom],
address_suggestion_country_to_provider=_make_country_mapping(),
)
request = _make_request(
country_code=country_code,
city="Berlin",
query="Alexanderplatz 1",
)
result = asyncio.run(service.suggest_addresses(request))
assert result == [
AddressSuggestion(
address="Alexanderplatz 1, 10178 Berlin",
street="Alexanderplatz",
house="1",
flat=None,
postal_code="10178",
)
]
assert dadata.calls == []
assert yandex_geosuggest.calls == []
assert tomtom.calls == [request]
def test_suggest_addresses_raises_for_unsupported_country() -> None: