Skip to content

Commit d1b1922

Browse files
committed
Rewrite handlers tests in terms of messagebus
1 parent 79e7d5a commit d1b1922

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/allocation/service_layer/unit_of_work.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import abc
2+
from typing import Generator, Optional
23

34
from allocation import config
45
from allocation.adapters import repository
6+
from allocation.domain import events
57
from sqlalchemy import create_engine
68
from sqlalchemy.orm import sessionmaker
79

@@ -33,7 +35,9 @@ def commit(self):
3335
def rollback(self):
3436
self._rollback()
3537

36-
def collect_new_events(self):
38+
def collect_new_events(
39+
self,
40+
) -> Optional[Generator[events.Event, None, None]]:
3741
for product in self.products.seen:
3842
while product.events:
3943
yield product.events.pop(0)

tests/e2e/test_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_add_batch(url):
3838
},
3939
)
4040

41-
assert response.status_code == 201
41+
assert response.status_code == 201, response.text
4242

4343
response = requests.post(
4444
f"{url}/add_batch",
@@ -49,7 +49,7 @@ def test_add_batch(url):
4949
"eta": "2021-01-01",
5050
},
5151
)
52-
assert response.status_code == 201
52+
assert response.status_code == 201, response.text
5353

5454

5555
@pytest.mark.usefixtures("restart_api")
@@ -66,7 +66,7 @@ def test_returns_200_and_allocated_batch(url, post_to_add_stock):
6666
data = {"orderid": random_orderid(), "sku": sku, "qty": 3}
6767
response = requests.post(f"{url}/allocate", json=data)
6868

69-
assert response.status_code == 201
69+
assert response.status_code == 201, response.text
7070
assert response.json()["batchref"] == earlybatch
7171

7272

@@ -82,7 +82,7 @@ def test_retuns_400_and_out_of_stock_message(url, post_to_add_stock):
8282
data = {"orderid": large_order, "sku": sku, "qty": 20}
8383
response = requests.post(f"{url}/allocate", json=data)
8484

85-
assert response.status_code == 400
85+
assert response.status_code == 400, response.text
8686
assert response.json()["message"] == "Out of stock"
8787

8888

@@ -93,7 +93,7 @@ def test_returns_400_invalid_sku_message(url):
9393
data = {"orderid": orderid, "sku": unknown_sku, "qty": 20}
9494
response = requests.post(f"{url}/allocate", json=data)
9595

96-
assert response.status_code == 400
96+
assert response.status_code == 400, response.text
9797
assert response.json()["message"] == f"Invalid sku {unknown_sku}"
9898

9999

tests/unit/test_handlers.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def test_add_batch_for_new_product():
5656
event = events.BatchCreated(
5757
reference="batch1", sku="COMPLICATED-LAMP", qty=100
5858
)
59-
handlers.add_batch(event=event, uow=uow)
59+
60+
messagebus.handle(event, uow)
6061

6162
assert uow.products.get("COMPLICATED-LAMP") is not None
6263
product = uow.products.get("COMPLICATED-LAMP")
@@ -66,14 +67,13 @@ def test_add_batch_for_new_product():
6667

6768
def test_add_batch_for_existing_product():
6869
uow = FakeUnitOfWork()
69-
event1 = events.BatchCreated(
70-
reference="batch1", sku="CRUNCHY-ARMCHAIR", qty=10
71-
)
72-
event2 = events.BatchCreated(
73-
reference="batch2", sku="CRUNCHY-ARMCHAIR", qty=15
74-
)
75-
handlers.add_batch(event=event1, uow=uow)
76-
handlers.add_batch(event=event2, uow=uow)
70+
event_history = [
71+
events.BatchCreated(reference="batch1", sku="CRUNCHY-ARMCHAIR", qty=10),
72+
events.BatchCreated(reference="batch2", sku="CRUNCHY-ARMCHAIR", qty=15),
73+
]
74+
75+
for event in event_history:
76+
messagebus.handle(event, uow)
7777

7878
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
7979
product = uow.products.get("CRUNCHY-ARMCHAIR")
@@ -87,43 +87,43 @@ def test_allocate_returns_allocation():
8787
event = events.BatchCreated(
8888
reference="batch1", sku="COMPLICATED-LAMP", qty=100
8989
)
90-
handlers.add_batch(event, uow)
90+
messagebus.handle(event, uow)
9191

9292
event = events.AllocationRequired(
9393
orderid="order1", sku="COMPLICATED-LAMP", qty=10
9494
)
95-
result = handlers.allocate(event=event, uow=uow)
95+
[batchref] = messagebus.handle(event, uow)
9696

97-
assert result == "batch1"
97+
assert batchref == "batch1"
9898
assert uow.committed
9999

100100

101101
def test_allocate_errors_for_invalid_sku():
102102
uow = FakeUnitOfWork()
103103
event = events.BatchCreated(reference="batch1", sku="AREALSKU", qty=100)
104-
handlers.add_batch(event, uow)
104+
messagebus.handle(event, uow)
105105

106106
with pytest.raises(
107107
handlers.InvalidSku, match="Invalid sku NON-EXISTENTSKU"
108108
):
109109
event = events.AllocationRequired(
110110
orderid="order1", sku="NON-EXISTENTSKU", qty=10
111111
)
112-
handlers.allocate(event=event, uow=uow)
112+
messagebus.handle(event, uow)
113113

114114

115115
def test_deallocate():
116116
uow = FakeUnitOfWork()
117117
event = events.BatchCreated(
118118
reference="batch1", sku="COMPLICATED-LAMP", qty=100
119119
)
120-
handlers.add_batch(event, uow)
120+
messagebus.handle(event, uow)
121121

122122
event = events.AllocationRequired(
123123
orderid="order1", sku="COMPLICATED-LAMP", qty=10
124124
)
125-
result = handlers.allocate(event=event, uow=uow)
126-
assert result == "batch1"
125+
[batchref] = messagebus.handle(event, uow)
126+
assert batchref == "batch1"
127127
product = uow.products.get("COMPLICATED-LAMP")
128128
batch = product.batches[0]
129129
assert batch.reference == "batch1"
@@ -132,7 +132,7 @@ def test_deallocate():
132132
event = events.DeallocationRequired(
133133
orderid="order1", sku="COMPLICATED-LAMP", qty=10
134134
)
135-
handlers.deallocate(event, uow)
135+
messagebus.handle(event, uow)
136136

137137
assert batch.allocated_quaitity == 0
138138
assert uow.committed

0 commit comments

Comments
 (0)