Добавлена dadata
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from app.adapters.address_suggestions.dadata.client import (
|
||||
DadataAddressSuggestionProvider,
|
||||
DadataClientError,
|
||||
DadataRequestError,
|
||||
)
|
||||
from app.config import DadataAddressSuggestionsConfig
|
||||
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 post(
|
||||
self,
|
||||
url: str,
|
||||
*,
|
||||
json: dict[str, Any] | None = None,
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: float | None = None,
|
||||
) -> httpx.Response:
|
||||
self.calls.append(
|
||||
{
|
||||
"method": "POST",
|
||||
"url": url,
|
||||
"json": json,
|
||||
"headers": headers,
|
||||
"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": "RU",
|
||||
"city": "Москва",
|
||||
"query": "Хабаровская",
|
||||
"limit": 5,
|
||||
}
|
||||
payload.update(overrides)
|
||||
return AddressSuggestRequest(**payload)
|
||||
|
||||
|
||||
def test_dadata_provider_maps_response_to_unified_model() -> None:
|
||||
response = httpx.Response(
|
||||
200,
|
||||
json={
|
||||
"suggestions": [
|
||||
{
|
||||
"value": "г Москва, ул Хабаровская",
|
||||
"unrestricted_value": "107241, г Москва, ул Хабаровская",
|
||||
"data": {
|
||||
"postal_code": "107241",
|
||||
"country_iso_code": "RU",
|
||||
"geo_lat": "55.821168",
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([response])
|
||||
provider = DadataAddressSuggestionProvider(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="test-api-key",
|
||||
timeout_seconds=7.5,
|
||||
)
|
||||
|
||||
result = asyncio.run(provider.suggest(_make_request()))
|
||||
|
||||
assert result == [
|
||||
AddressSuggestion(
|
||||
provider="dadata",
|
||||
address="107241, г Москва, ул Хабаровская",
|
||||
postal_code="107241",
|
||||
)
|
||||
]
|
||||
assert http_client.calls == [
|
||||
{
|
||||
"method": "POST",
|
||||
"url": "https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
"json": {
|
||||
"query": "Москва Хабаровская",
|
||||
"count": 5,
|
||||
},
|
||||
"headers": {
|
||||
"Authorization": "Token test-api-key",
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
"timeout": 7.5,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_dadata_provider_uses_foreign_country_payload_and_config_factory() -> None:
|
||||
response = httpx.Response(
|
||||
200,
|
||||
json={"suggestions": []},
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([response])
|
||||
provider = DadataAddressSuggestionProvider.from_config(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
config=DadataAddressSuggestionsConfig(
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="config-api-key",
|
||||
timeout_seconds=3.0,
|
||||
),
|
||||
)
|
||||
|
||||
result = asyncio.run(
|
||||
provider.suggest(
|
||||
_make_request(
|
||||
country_code="BY",
|
||||
city="Гомель",
|
||||
query="Советская",
|
||||
limit=3,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
assert result == []
|
||||
assert http_client.calls[0]["json"] == {
|
||||
"query": "Гомель Советская",
|
||||
"count": 3,
|
||||
"locations": [{"country": "*"}],
|
||||
}
|
||||
assert http_client.calls[0]["headers"]["Authorization"] == "Token config-api-key"
|
||||
assert http_client.calls[0]["timeout"] == 3.0
|
||||
|
||||
|
||||
def test_dadata_provider_maps_4xx_to_request_error() -> None:
|
||||
response = httpx.Response(
|
||||
403,
|
||||
json={"detail": "forbidden"},
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([response])
|
||||
provider = DadataAddressSuggestionProvider(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="test-api-key",
|
||||
)
|
||||
|
||||
with pytest.raises(DadataRequestError, match="status 403"):
|
||||
asyncio.run(provider.suggest(_make_request()))
|
||||
|
||||
|
||||
def test_dadata_provider_maps_5xx_to_client_error() -> None:
|
||||
response = httpx.Response(
|
||||
503,
|
||||
json={"detail": "unavailable"},
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([response])
|
||||
provider = DadataAddressSuggestionProvider(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="test-api-key",
|
||||
)
|
||||
|
||||
with pytest.raises(DadataClientError, match="status 503"):
|
||||
asyncio.run(provider.suggest(_make_request()))
|
||||
|
||||
|
||||
def test_dadata_provider_maps_transport_error_to_client_error() -> None:
|
||||
transport_error = httpx.ReadTimeout(
|
||||
"read timeout",
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([transport_error])
|
||||
provider = DadataAddressSuggestionProvider(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="test-api-key",
|
||||
)
|
||||
|
||||
with pytest.raises(DadataClientError, match="request failed"):
|
||||
asyncio.run(provider.suggest(_make_request()))
|
||||
|
||||
|
||||
def test_dadata_provider_raises_on_invalid_payload_shape() -> None:
|
||||
response = httpx.Response(
|
||||
200,
|
||||
json={"suggestions": [{"data": {"postal_code": "107241"}}]},
|
||||
request=httpx.Request(
|
||||
"POST",
|
||||
"https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
),
|
||||
)
|
||||
http_client = SequenceHTTPClient([response])
|
||||
provider = DadataAddressSuggestionProvider(
|
||||
http_client=http_client, # type: ignore[arg-type]
|
||||
url="https://suggestions.dadata.test/suggestions/api/4_1/rs/suggest/address",
|
||||
api_key="test-api-key",
|
||||
)
|
||||
|
||||
with pytest.raises(DadataClientError, match="response payload is invalid"):
|
||||
asyncio.run(provider.suggest(_make_request()))
|
||||
@@ -15,6 +15,13 @@ adapter:
|
||||
cdek_client_id: "yaml-id"
|
||||
cdek_client_secret: "yaml-secret"
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "default-dadata-key"
|
||||
|
||||
observability:
|
||||
enabled: false
|
||||
service_name: "default-config-service"
|
||||
|
||||
@@ -23,6 +23,13 @@ adapter:
|
||||
cdek_timeout_seconds: 10.0
|
||||
cdek_cache_ttl_seconds: 900
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "invalid-price-multiplier-dadata-key"
|
||||
|
||||
observability:
|
||||
enabled: false
|
||||
service_name: "invalid-price-multiplier-service"
|
||||
|
||||
@@ -12,6 +12,13 @@ repository:
|
||||
redis_dsn: "redis://localhost:6379/0"
|
||||
price_cache_ttl_seconds: 900
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "missing-adapter-dadata-key"
|
||||
|
||||
observability:
|
||||
enabled: false
|
||||
service_name: "missing-adapter-service"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
controller:
|
||||
api_prefix: "/api/v1"
|
||||
request_id_header: "X-Request-ID"
|
||||
|
||||
service:
|
||||
provider_timeout_seconds: 10.0
|
||||
max_parallel_providers: 8
|
||||
|
||||
business_logic:
|
||||
weight_round_scale: 2
|
||||
provider_price_multiplier: 1.0
|
||||
|
||||
repository:
|
||||
redis_dsn: "redis://localhost:6379/0"
|
||||
price_cache_ttl_seconds: 900
|
||||
|
||||
adapter:
|
||||
cdek_base_url: "https://api.cdek.ru/v2"
|
||||
cdek_client_id: "test-client-id"
|
||||
cdek_client_secret: "test-client-secret"
|
||||
cdek_retry_attempts: 2
|
||||
cdek_retry_backoff_seconds: 0.2
|
||||
cdek_timeout_seconds: 10.0
|
||||
cdek_cache_ttl_seconds: 900
|
||||
|
||||
observability:
|
||||
enabled: false
|
||||
service_name: "missing-address-suggestions-service"
|
||||
otlp_endpoint: "http://collector:4317"
|
||||
otlp_insecure: true
|
||||
@@ -22,3 +22,10 @@ adapter:
|
||||
cdek_retry_backoff_seconds: 0.2
|
||||
cdek_timeout_seconds: 10.0
|
||||
cdek_cache_ttl_seconds: 900
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "missing-observability-dadata-key"
|
||||
|
||||
@@ -23,6 +23,13 @@ adapter:
|
||||
cdek_timeout_seconds: 10.0
|
||||
cdek_cache_ttl_seconds: 900
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "missing-observability-endpoint-dadata-key"
|
||||
|
||||
observability:
|
||||
enabled: true
|
||||
service_name: "g2s-aggregator"
|
||||
|
||||
@@ -22,6 +22,13 @@ adapter:
|
||||
cdek_timeout_seconds: 10.0
|
||||
cdek_cache_ttl_seconds: 900
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "missing-price-multiplier-dadata-key"
|
||||
|
||||
observability:
|
||||
enabled: false
|
||||
service_name: "missing-price-multiplier-service"
|
||||
|
||||
@@ -15,6 +15,15 @@ adapter:
|
||||
cdek_client_id: "test-id"
|
||||
cdek_client_secret: "test-secret"
|
||||
|
||||
address_suggestions:
|
||||
country_to_provider:
|
||||
RU: "dadata"
|
||||
AM: "europe"
|
||||
dadata:
|
||||
url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
api_key: "override-dadata-key"
|
||||
timeout_seconds: 3.0
|
||||
|
||||
observability:
|
||||
enabled: true
|
||||
service_name: "override-config-service"
|
||||
|
||||
@@ -12,6 +12,13 @@ TEST_CONFIG_FILE = PROJECT_ROOT / "config.test.yaml"
|
||||
MISSING_REQUIRED_SECTION_CONFIG_FILE = (
|
||||
PROJECT_ROOT / "tests" / "config" / "fixtures" / "config.missing_adapter.yaml"
|
||||
)
|
||||
MISSING_ADDRESS_SUGGESTIONS_CONFIG_FILE = (
|
||||
PROJECT_ROOT
|
||||
/ "tests"
|
||||
/ "config"
|
||||
/ "fixtures"
|
||||
/ "config.missing_address_suggestions.yaml"
|
||||
)
|
||||
DEFAULT_CONFIG_FILE_FIXTURE = (
|
||||
PROJECT_ROOT / "tests" / "config" / "fixtures" / "config.default.yaml"
|
||||
)
|
||||
@@ -74,6 +81,17 @@ 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",
|
||||
}
|
||||
assert (
|
||||
settings.address_suggestions.dadata.url
|
||||
== "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
|
||||
)
|
||||
assert settings.address_suggestions.dadata.api_key == "test-dadata-api-key"
|
||||
assert settings.address_suggestions.dadata.timeout_seconds == 7.5
|
||||
assert settings.observability.enabled is False
|
||||
assert settings.observability.service_name == "g2s-aggregator-test"
|
||||
assert settings.observability.otlp_endpoint == "http://localhost:4317"
|
||||
@@ -96,6 +114,22 @@ def test_get_settings_fails_when_required_yaml_section_is_missing(
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
def test_get_settings_fails_when_address_suggestions_section_is_missing(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_use_runtime_config_files(
|
||||
monkeypatch,
|
||||
test_config_file=MISSING_ADDRESS_SUGGESTIONS_CONFIG_FILE,
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as error:
|
||||
get_settings()
|
||||
|
||||
locations = {tuple(item["loc"]) for item in error.value.errors()}
|
||||
assert ("address_suggestions",) in locations
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
def test_get_settings_returns_cached_instance(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
_use_runtime_config_files(monkeypatch, test_config_file=TEST_CONFIG_FILE)
|
||||
|
||||
@@ -105,6 +139,7 @@ def test_get_settings_returns_cached_instance(monkeypatch: pytest.MonkeyPatch) -
|
||||
assert first is second
|
||||
assert first.service.provider_timeout_seconds == 10.0
|
||||
assert first.business_logic.provider_price_multiplier == Decimal("1.0")
|
||||
assert first.address_suggestions.country_to_provider["RU"] == "dadata"
|
||||
assert first.observability.service_name == "g2s-aggregator-test"
|
||||
get_settings.cache_clear()
|
||||
|
||||
@@ -124,6 +159,12 @@ def test_get_settings_uses_config_test_yaml_in_pytest_environment(
|
||||
assert settings.controller.api_prefix == "/from-config-test-yaml"
|
||||
assert settings.business_logic.provider_price_multiplier == Decimal("1.25")
|
||||
assert settings.adapter.cdek_client_id == "test-id"
|
||||
assert settings.address_suggestions.country_to_provider == {
|
||||
"RU": "dadata",
|
||||
"AM": "europe",
|
||||
}
|
||||
assert settings.address_suggestions.dadata.api_key == "override-dadata-key"
|
||||
assert settings.address_suggestions.dadata.timeout_seconds == 3.0
|
||||
assert settings.observability.enabled is True
|
||||
assert settings.observability.service_name == "override-config-service"
|
||||
assert settings.observability.otlp_endpoint == "http://override-collector:4317"
|
||||
|
||||
Reference in New Issue
Block a user