@@ -13,11 +13,11 @@ from app.services.waybill_poller import WaybillPollerService
|
||||
@dataclass
|
||||
class StoredOrder:
|
||||
order_uuid: str
|
||||
cdek_order_uuid: str | None = None
|
||||
cdek_order_status: str | None = None
|
||||
cdek_waybill_uuid: str | None = None
|
||||
cdek_waybill_url: str | None = None
|
||||
cdek_polled_at: datetime | None = None
|
||||
provider_order_id: str | None = None
|
||||
provider_order_status: str | None = None
|
||||
provider_waybill_id: str | None = None
|
||||
provider_waybill_url: str | None = None
|
||||
provider_polled_at: datetime | None = None
|
||||
|
||||
|
||||
class StubSessionContext:
|
||||
@@ -69,10 +69,10 @@ class StubRepository:
|
||||
order = self._orders.get(order_uuid)
|
||||
if order is None:
|
||||
return None
|
||||
order.cdek_order_status = order_status
|
||||
if waybill_uuid is not None and order.cdek_waybill_uuid is None:
|
||||
order.cdek_waybill_uuid = waybill_uuid
|
||||
order.cdek_polled_at = polled_at
|
||||
order.provider_order_status = order_status
|
||||
if waybill_uuid is not None and order.provider_waybill_id is None:
|
||||
order.provider_waybill_id = waybill_uuid
|
||||
order.provider_polled_at = polled_at
|
||||
return order
|
||||
|
||||
async def record_waybill_poll(
|
||||
@@ -96,9 +96,9 @@ class StubRepository:
|
||||
order = self._orders.get(order_uuid)
|
||||
if order is None:
|
||||
return None
|
||||
if waybill_url is not None and order.cdek_waybill_url is None:
|
||||
order.cdek_waybill_url = waybill_url
|
||||
order.cdek_polled_at = polled_at
|
||||
if waybill_url is not None and order.provider_waybill_url is None:
|
||||
order.provider_waybill_url = waybill_url
|
||||
order.provider_polled_at = polled_at
|
||||
return order
|
||||
|
||||
|
||||
@@ -107,9 +107,9 @@ class StubOrderInfoAdapter:
|
||||
self._results = results
|
||||
self.calls: list[str] = []
|
||||
|
||||
async def get_order(self, cdek_order_uuid: str) -> CDEKOrderInfo:
|
||||
self.calls.append(cdek_order_uuid)
|
||||
result = self._results[cdek_order_uuid]
|
||||
async def get_order(self, provider_order_id: str) -> CDEKOrderInfo:
|
||||
self.calls.append(provider_order_id)
|
||||
result = self._results[provider_order_id]
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
return result
|
||||
@@ -120,9 +120,9 @@ class StubWaybillInfoAdapter:
|
||||
self._results = results
|
||||
self.calls: list[str] = []
|
||||
|
||||
async def get_waybill(self, cdek_waybill_uuid: str) -> CDEKWaybillInfo:
|
||||
self.calls.append(cdek_waybill_uuid)
|
||||
result = self._results[cdek_waybill_uuid]
|
||||
async def get_waybill(self, provider_waybill_id: str) -> CDEKWaybillInfo:
|
||||
self.calls.append(provider_waybill_id)
|
||||
result = self._results[provider_waybill_id]
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
return result
|
||||
@@ -147,7 +147,7 @@ def _make_service(
|
||||
|
||||
|
||||
def test_poll_once_fetches_order_info_when_waybill_uuid_is_missing() -> None:
|
||||
order = StoredOrder(order_uuid="o", cdek_order_uuid="cdek-o")
|
||||
order = StoredOrder(order_uuid="o", provider_order_id="cdek-o")
|
||||
repo = StubRepository([order])
|
||||
order_info = StubOrderInfoAdapter(
|
||||
{
|
||||
@@ -164,16 +164,16 @@ def test_poll_once_fetches_order_info_when_waybill_uuid_is_missing() -> None:
|
||||
|
||||
assert summary.processed == 1 and summary.succeeded == 1 and summary.failed == 0
|
||||
assert order_info.calls == ["cdek-o"]
|
||||
assert order.cdek_order_status == "ACCEPTED"
|
||||
assert order.cdek_waybill_uuid == "waybill-1"
|
||||
assert order.cdek_polled_at == _POLLED_AT
|
||||
assert order.provider_order_status == "ACCEPTED"
|
||||
assert order.provider_waybill_id == "waybill-1"
|
||||
assert order.provider_polled_at == _POLLED_AT
|
||||
|
||||
|
||||
def test_poll_once_fetches_waybill_info_when_waybill_uuid_is_present() -> None:
|
||||
order = StoredOrder(
|
||||
order_uuid="o",
|
||||
cdek_order_uuid="cdek-o",
|
||||
cdek_waybill_uuid="waybill-1",
|
||||
provider_order_id="cdek-o",
|
||||
provider_waybill_id="waybill-1",
|
||||
)
|
||||
repo = StubRepository([order])
|
||||
waybill_info = StubWaybillInfoAdapter(
|
||||
@@ -190,12 +190,12 @@ def test_poll_once_fetches_waybill_info_when_waybill_uuid_is_present() -> None:
|
||||
|
||||
assert summary.processed == 1 and summary.succeeded == 1 and summary.failed == 0
|
||||
assert waybill_info.calls == ["waybill-1"]
|
||||
assert order.cdek_waybill_url == "https://cdek.test/1.pdf"
|
||||
assert order.cdek_polled_at == _POLLED_AT
|
||||
assert order.provider_waybill_url == "https://cdek.test/1.pdf"
|
||||
assert order.provider_polled_at == _POLLED_AT
|
||||
|
||||
|
||||
def test_poll_once_records_terminal_status_without_waybill() -> None:
|
||||
order = StoredOrder(order_uuid="o", cdek_order_uuid="cdek-o")
|
||||
order = StoredOrder(order_uuid="o", provider_order_id="cdek-o")
|
||||
repo = StubRepository([order])
|
||||
order_info = StubOrderInfoAdapter(
|
||||
{
|
||||
@@ -211,16 +211,16 @@ def test_poll_once_records_terminal_status_without_waybill() -> None:
|
||||
summary = asyncio.run(service.poll_once())
|
||||
|
||||
assert summary.succeeded == 1
|
||||
assert order.cdek_order_status == "INVALID"
|
||||
assert order.cdek_waybill_uuid is None
|
||||
assert order.provider_order_status == "INVALID"
|
||||
assert order.provider_waybill_id is None
|
||||
|
||||
|
||||
def test_poll_once_failure_on_one_order_does_not_break_batch() -> None:
|
||||
failing = StoredOrder(order_uuid="bad", cdek_order_uuid="cdek-bad")
|
||||
failing = StoredOrder(order_uuid="bad", provider_order_id="cdek-bad")
|
||||
good = StoredOrder(
|
||||
order_uuid="good",
|
||||
cdek_order_uuid="cdek-good",
|
||||
cdek_waybill_uuid="waybill-good",
|
||||
provider_order_id="cdek-good",
|
||||
provider_waybill_id="waybill-good",
|
||||
)
|
||||
repo = StubRepository([failing, good])
|
||||
order_info = StubOrderInfoAdapter({"cdek-bad": RuntimeError("cdek down")})
|
||||
@@ -241,7 +241,7 @@ def test_poll_once_failure_on_one_order_does_not_break_batch() -> None:
|
||||
assert summary.processed == 2
|
||||
assert summary.succeeded == 1
|
||||
assert summary.failed == 1
|
||||
assert good.cdek_waybill_url == "https://cdek.test/good.pdf"
|
||||
assert good.provider_waybill_url == "https://cdek.test/good.pdf"
|
||||
|
||||
|
||||
def test_run_forever_exits_when_stop_event_is_set() -> None:
|
||||
|
||||
Reference in New Issue
Block a user