Skip to content

Commit 636f734

Browse files
committed
go DI, still use mocks
1 parent 8e47075 commit 636f734

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

test_sync.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
from datetime import date
12
from unittest import mock
23

34
from use_cases import create_shipment, API_URL
45
from domain import Shipment, OrderLine
56
from cargo_api import RealCargoAPI
67

8+
class FakeCargoAPI:
9+
10+
def __init__(self):
11+
self._shipments = {}
12+
13+
def get_latest_eta(self, reference) -> date:
14+
return self._shipments[reference].eta
15+
16+
def sync(self, shipment: Shipment):
17+
self._shipments[shipment.reference] = shipment
18+
19+
def __contains__(self, shipment):
20+
return shipment in self._shipments.values()
21+
22+
723
def test_create_shipment_syncs_to_api():
8-
with mock.patch('use_cases.cargo_api') as mock_cargo_api:
9-
shipment = create_shipment({'sku1': 10}, incoterm='EXW')
10-
assert mock_cargo_api.sync.call_args == mock.call(shipment)
24+
mock_api = mock.Mock()
25+
shipment = create_shipment({'sku1': 10}, incoterm='EXW', cargo_api=mock_api)
26+
assert mock_api.sync.call_args == mock.call(shipment)

use_cases.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
from domain import Shipment, OrderLine
1010
from notifications import notify_delay, notify_new_large_shipment
11-
from cargo_api import RealCargoAPI
12-
cargo_api = RealCargoAPI()
11+
from cargo_api import CargoAPI, RealCargoAPI
1312

1413
API_URL = 'https://example.org'
1514

1615

17-
def create_shipment(quantities: Dict[str, int], incoterm) -> Shipment:
16+
def create_shipment(quantities: Dict[str, int], incoterm: str, cargo_api: CargoAPI) -> Shipment:
1817
print(uuid)
1918
reference = uuid.uuid4().hex[:10]
2019
order_lines = [OrderLine(sku=sku, qty=qty) for sku, qty in quantities.items()]

0 commit comments

Comments
 (0)