013 add price multiplier
This commit is contained in:
@@ -10,6 +10,7 @@ from app.adapters.delivery_providers.cdek.client import (
|
||||
CDEKProvider,
|
||||
CDEKRequestError,
|
||||
)
|
||||
from app import config as config_module
|
||||
from app.config import AdapterConfig, Settings
|
||||
from app.schemas.request import DeliveryEntity, DeliveryRequest
|
||||
|
||||
@@ -299,6 +300,22 @@ def test_provider_uses_adapter_yaml_config_for_timeout_and_cache_ttl(
|
||||
config_file = tmp_path / "config.yaml"
|
||||
config_file.write_text(
|
||||
"""
|
||||
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.test/v2"
|
||||
cdek_client_id: "yaml-id"
|
||||
@@ -310,7 +327,7 @@ adapter:
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setenv("G2S_CONFIG_FILE", str(config_file))
|
||||
monkeypatch.setattr(config_module, "_resolve_runtime_config_file", lambda: str(config_file))
|
||||
settings = Settings()
|
||||
http_client = RecordingHTTPClient()
|
||||
provider = CDEKProvider.from_adapter_config(
|
||||
|
||||
@@ -6,6 +6,7 @@ service:
|
||||
|
||||
business_logic:
|
||||
weight_round_scale: 2
|
||||
provider_price_multiplier: 1.0
|
||||
|
||||
repository:
|
||||
redis_dsn: "redis://localhost:6379/0"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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: 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
|
||||
@@ -6,6 +6,7 @@ service:
|
||||
|
||||
business_logic:
|
||||
weight_round_scale: 2
|
||||
provider_price_multiplier: 1.0
|
||||
|
||||
repository:
|
||||
redis_dsn: "redis://localhost:6379/0"
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
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
|
||||
|
||||
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
|
||||
@@ -6,6 +6,7 @@ service:
|
||||
|
||||
business_logic:
|
||||
weight_round_scale: 2
|
||||
provider_price_multiplier: 1.25
|
||||
|
||||
repository:
|
||||
redis_dsn: "redis://localhost:6379/0"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -17,6 +18,12 @@ DEFAULT_CONFIG_FILE_FIXTURE = (
|
||||
TEST_OVERRIDE_CONFIG_FILE_FIXTURE = (
|
||||
PROJECT_ROOT / "tests" / "config" / "fixtures" / "config.test.override.yaml"
|
||||
)
|
||||
INVALID_PRICE_MULTIPLIER_CONFIG_FILE = (
|
||||
PROJECT_ROOT / "tests" / "config" / "fixtures" / "config.invalid_price_multiplier.yaml"
|
||||
)
|
||||
MISSING_PRICE_MULTIPLIER_CONFIG_FILE = (
|
||||
PROJECT_ROOT / "tests" / "config" / "fixtures" / "config.missing_price_multiplier.yaml"
|
||||
)
|
||||
|
||||
|
||||
def _use_runtime_config_files(
|
||||
@@ -47,6 +54,7 @@ def test_configuration_sections_are_loaded_from_yaml_file(
|
||||
assert settings.service.provider_timeout_seconds == 10.0
|
||||
assert settings.service.max_parallel_providers == 8
|
||||
assert settings.business_logic.weight_round_scale == 2
|
||||
assert settings.business_logic.provider_price_multiplier == Decimal("1.0")
|
||||
assert settings.repository.redis_dsn == "redis://localhost:6379/0"
|
||||
assert settings.repository.price_cache_ttl_seconds == 900
|
||||
assert settings.adapter.cdek_base_url == "https://api.cdek.ru/v2"
|
||||
@@ -82,6 +90,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")
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
@@ -98,5 +107,38 @@ def test_get_settings_uses_config_test_yaml_in_pytest_environment(
|
||||
|
||||
assert settings.service.provider_timeout_seconds == 17.0
|
||||
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"
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
def test_get_settings_fails_when_provider_price_multiplier_is_invalid(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_use_runtime_config_files(
|
||||
monkeypatch,
|
||||
test_config_file=INVALID_PRICE_MULTIPLIER_CONFIG_FILE,
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as error:
|
||||
get_settings()
|
||||
|
||||
locations = {tuple(item["loc"]) for item in error.value.errors()}
|
||||
assert ("business_logic", "provider_price_multiplier") in locations
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
def test_get_settings_fails_when_provider_price_multiplier_is_missing(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_use_runtime_config_files(
|
||||
monkeypatch,
|
||||
test_config_file=MISSING_PRICE_MULTIPLIER_CONFIG_FILE,
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as error:
|
||||
get_settings()
|
||||
|
||||
locations = {tuple(item["loc"]) for item in error.value.errors()}
|
||||
assert ("business_logic", "provider_price_multiplier") in locations
|
||||
get_settings.cache_clear()
|
||||
|
||||
@@ -69,7 +69,7 @@ def test_post_delivery_price_uses_registered_provider_in_default_dependency(
|
||||
return DeliveryPrice(
|
||||
provider=self.name,
|
||||
service_name="stub-service",
|
||||
price=Decimal("123.45"),
|
||||
price=Decimal("123.50"),
|
||||
currency="RUB",
|
||||
delivery_days_min=2,
|
||||
delivery_days_max=3,
|
||||
@@ -136,7 +136,7 @@ def test_post_delivery_price_uses_registered_provider_in_default_dependency(
|
||||
{
|
||||
"provider": "stub-provider",
|
||||
"service_name": "stub-service",
|
||||
"price": "123.45",
|
||||
"price": "124",
|
||||
"currency": "RUB",
|
||||
"delivery_days_min": 2,
|
||||
"delivery_days_max": 3,
|
||||
|
||||
@@ -80,6 +80,18 @@ def test_filter_valid_prices_handles_empty_input() -> None:
|
||||
assert filter_and_sort_prices([]) == []
|
||||
|
||||
|
||||
def test_filter_valid_prices_applies_multiplier_and_rounds_half_up() -> None:
|
||||
low = _make_price(provider="cdek", price=Decimal("100.40"))
|
||||
high = _make_price(provider="boxberry", price=Decimal("100.50"))
|
||||
|
||||
result = filter_valid_prices(
|
||||
[low, high],
|
||||
price_multiplier=Decimal("1.1"),
|
||||
)
|
||||
|
||||
assert [price.price for price in result] == [Decimal("110"), Decimal("111")]
|
||||
|
||||
|
||||
def test_filter_valid_prices_excludes_invalid_records() -> None:
|
||||
valid = _make_price(provider=" cdek ", service_name=" express ", currency="rub")
|
||||
invalid_provider = _make_price(provider=" ")
|
||||
@@ -128,6 +140,17 @@ def test_filter_valid_prices_excludes_none_provider_and_service() -> None:
|
||||
assert result[0].service_name == "express"
|
||||
|
||||
|
||||
def test_filter_valid_prices_excludes_prices_rounded_to_zero_after_multiplier() -> None:
|
||||
low_price = _make_price(provider="cdek", price=Decimal("0.40"))
|
||||
|
||||
result = filter_valid_prices(
|
||||
[low_price],
|
||||
price_multiplier=Decimal("0.5"),
|
||||
)
|
||||
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_sort_prices_by_price_keeps_equal_price_order_stable() -> None:
|
||||
price_a = _make_price(provider="a", price=Decimal("150.00"))
|
||||
price_b = _make_price(provider="b", price=Decimal("150.00"))
|
||||
@@ -143,6 +166,10 @@ def test_filter_and_sort_prices_combines_domain_steps() -> None:
|
||||
price_b = _make_price(provider="b", price=Decimal("100.00"))
|
||||
invalid = _make_price(provider="", price=Decimal("50.00"))
|
||||
|
||||
result = filter_and_sort_prices([price_a, invalid, price_b])
|
||||
result = filter_and_sort_prices(
|
||||
[price_a, invalid, price_b],
|
||||
price_multiplier=Decimal("1.3"),
|
||||
)
|
||||
|
||||
assert [price.provider for price in result] == ["b", "a"]
|
||||
assert [price.price for price in result] == [Decimal("130"), Decimal("286")]
|
||||
|
||||
+21
@@ -154,9 +154,30 @@ def test_repository_yaml_config_sets_redis_connection_and_ttl(
|
||||
config_file = tmp_path / "config.yaml"
|
||||
config_file.write_text(
|
||||
"""
|
||||
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://redis.internal:6380/5"
|
||||
price_cache_ttl_seconds: 123
|
||||
|
||||
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
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
@@ -85,14 +85,19 @@ def _make_price(provider: str, price: str) -> DeliveryPrice:
|
||||
|
||||
|
||||
def test_get_all_prices_full_success_returns_sorted_and_updates_cache() -> None:
|
||||
provider_a = StubProvider(name="a", response=_make_price("a", "300.00"), cache_ttl_seconds=111)
|
||||
provider_b = StubProvider(name="b", response=_make_price("b", "100.00"), cache_ttl_seconds=222)
|
||||
provider_a = StubProvider(name="a", response=_make_price("a", "300.49"), cache_ttl_seconds=111)
|
||||
provider_b = StubProvider(name="b", response=_make_price("b", "100.40"), cache_ttl_seconds=222)
|
||||
cache = StubCache()
|
||||
service = AggregatorService([provider_a, provider_b], cache=cache)
|
||||
service = AggregatorService(
|
||||
[provider_a, provider_b],
|
||||
cache=cache,
|
||||
provider_price_multiplier=Decimal("1.1"),
|
||||
)
|
||||
|
||||
result = asyncio.run(service.get_all_prices(_make_request()))
|
||||
|
||||
assert [price.provider for price in result] == ["b", "a"]
|
||||
assert [price.price for price in result] == [Decimal("110"), Decimal("331")]
|
||||
assert len(provider_a.calls) == 1
|
||||
assert len(provider_b.calls) == 1
|
||||
assert len(cache.get_calls) == 2
|
||||
@@ -148,15 +153,19 @@ def test_get_all_prices_raises_invalid_request_for_provider_request_errors() ->
|
||||
|
||||
|
||||
def test_get_all_prices_cache_hit_skips_provider_call() -> None:
|
||||
cached_payload = _make_price("cdek", "99.00").model_dump(mode="json")
|
||||
cached_payload = _make_price("cdek", "100.40").model_dump(mode="json")
|
||||
cache = StubCache(forced_get_value=cached_payload)
|
||||
provider = StubProvider(name="cdek", response=_make_price("cdek", "150.00"))
|
||||
service = AggregatorService([provider], cache=cache)
|
||||
service = AggregatorService(
|
||||
[provider],
|
||||
cache=cache,
|
||||
provider_price_multiplier=Decimal("1.1"),
|
||||
)
|
||||
|
||||
result = asyncio.run(service.get_all_prices(_make_request()))
|
||||
|
||||
assert [price.provider for price in result] == ["cdek"]
|
||||
assert result[0].price == Decimal("99.00")
|
||||
assert result[0].price == Decimal("110")
|
||||
assert provider.calls == []
|
||||
assert len(cache.get_calls) == 1
|
||||
assert cache.set_calls == []
|
||||
@@ -166,19 +175,23 @@ def test_get_all_prices_delegates_filtering_and_sorting_to_domain_logic() -> Non
|
||||
provider_a = StubProvider(name="a", response=_make_price("a", "300.00"))
|
||||
provider_b = StubProvider(name="b", response=_make_price("b", "100.00"))
|
||||
delegated_inputs: list[list[DeliveryPrice]] = []
|
||||
delegated_multipliers: list[Decimal] = []
|
||||
|
||||
def fake_filter_and_sort(prices):
|
||||
def fake_filter_and_sort(prices, *, price_multiplier):
|
||||
price_list = list(prices)
|
||||
delegated_inputs.append(price_list)
|
||||
delegated_multipliers.append(price_multiplier)
|
||||
return [price_list[0]]
|
||||
|
||||
service = AggregatorService(
|
||||
[provider_a, provider_b],
|
||||
cache=StubCache(),
|
||||
provider_price_multiplier=Decimal("1.23"),
|
||||
filter_and_sort_prices_fn=fake_filter_and_sort,
|
||||
)
|
||||
|
||||
result = asyncio.run(service.get_all_prices(_make_request()))
|
||||
|
||||
assert [price.provider for price in delegated_inputs[0]] == ["a", "b"]
|
||||
assert delegated_multipliers == [Decimal("1.23")]
|
||||
assert [price.provider for price in result] == ["a"]
|
||||
|
||||
Reference in New Issue
Block a user