Skip to content

Commit a347b24

Browse files
committed
handlers take commands now, modify tests too
1 parent 7fbb3af commit a347b24

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

src/allocation/service_layer/handlers.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
33
from allocation.adapters import email
4-
from allocation.domain import events, model
4+
from allocation.domain import commands, events, model
55
from allocation.domain.model import OrderLine
66

77
if TYPE_CHECKING:
@@ -13,25 +13,23 @@ class InvalidSku(Exception):
1313

1414

1515
def add_batch(
16-
event: events.BatchCreated,
16+
cmd: commands.CreateBatch,
1717
uow: unit_of_work.AbstractUnitOfWork,
1818
):
1919
with uow:
20-
product = uow.products.get(sku=event.sku)
20+
product = uow.products.get(sku=cmd.sku)
2121
if product is None:
22-
product = model.Product(event.sku, batches=[])
22+
product = model.Product(cmd.sku, batches=[])
2323
uow.products.add(product)
24-
product.batches.append(
25-
model.Batch(event.ref, event.sku, event.qty, event.eta)
26-
)
24+
product.batches.append(model.Batch(cmd.ref, cmd.sku, cmd.qty, cmd.eta))
2725
uow.commit()
2826

2927

3028
def allocate(
31-
event: events.AllocationRequired,
29+
cmd: commands.Allocate,
3230
uow: unit_of_work.AbstractUnitOfWork,
3331
) -> str:
34-
line = OrderLine(event.orderid, event.sku, event.qty)
32+
line = OrderLine(cmd.orderid, cmd.sku, cmd.qty)
3533
with uow:
3634
product = uow.products.get(sku=line.sku)
3735
if product is None:
@@ -42,18 +40,15 @@ def allocate(
4240

4341

4442
def change_batch_quantity(
45-
event: events.BatchQuantityChanged,
43+
cmd: commands.ChangeBatchQuantity,
4644
uow: unit_of_work.AbstractUnitOfWork,
4745
):
4846
with uow:
49-
product = uow.products.get_by_batchref(batchref=event.ref)
50-
product.change_batch_quantity(ref=event.ref, qty=event.qty)
47+
product = uow.products.get_by_batchref(batchref=cmd.ref)
48+
product.change_batch_quantity(ref=cmd.ref, qty=cmd.qty)
5149
uow.commit()
5250

5351

54-
# pylint: disable=unused-argument
55-
56-
5752
def send_out_of_stock_notification(
5853
event: events.OutOfStock,
5954
uow: unit_of_work.AbstractUnitOfWork,

tests/unit/test_handlers.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from datetime import date
33
from unittest import mock
44
import pytest
5-
65
from allocation.adapters import repository
7-
from allocation.domain import events
6+
from allocation.domain import commands, events
87
from allocation.service_layer import handlers, messagebus, unit_of_work
98

109

@@ -42,52 +41,54 @@ class TestAddBatch:
4241
def test_for_new_product(self):
4342
uow = FakeUnitOfWork()
4443
messagebus.handle(
45-
events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
44+
commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
4645
)
4746
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
4847
assert uow.committed
4948

5049
def test_for_existing_product(self):
5150
uow = FakeUnitOfWork()
52-
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
53-
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
51+
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
52+
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
5453
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
5554

5655

5756
class TestAllocate:
58-
def test_returns_allocation(self):
57+
def test_allocates(self):
5958
uow = FakeUnitOfWork()
6059
messagebus.handle(
61-
events.BatchCreated("batch1", "COMPLICATED-LAMP", 100, None), uow
60+
commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None), uow
6261
)
6362
results = messagebus.handle(
64-
events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow
63+
commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow
6564
)
6665
assert results.pop(0) == "batch1"
66+
[batch] = uow.products.get("COMPLICATED-LAMP").batches
67+
assert batch.available_quantity == 90
6768

6869
def test_errors_for_invalid_sku(self):
6970
uow = FakeUnitOfWork()
70-
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
71+
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
7172

7273
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
73-
messagebus.handle(
74-
events.AllocationRequired("o1", "NONEXISTENTSKU", 10), uow
75-
)
74+
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow)
7675

7776
def test_commits(self):
7877
uow = FakeUnitOfWork()
79-
messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow)
80-
messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow)
78+
messagebus.handle(
79+
commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow
80+
)
81+
messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow)
8182
assert uow.committed
8283

8384
def test_sends_email_on_out_of_stock_error(self):
8485
uow = FakeUnitOfWork()
85-
messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow)
86+
messagebus.handle(
87+
commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow
88+
)
8689

8790
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
88-
messagebus.handle(
89-
events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow
90-
)
91+
messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow)
9192
assert mock_send_mail.call_args == mock.call(
9293
"stock@made.com", f"Out of stock for POPULAR-CURTAINS"
9394
)
@@ -97,30 +98,30 @@ class TestChangeBatchQuantity:
9798
def test_changes_available_quantity(self):
9899
uow = FakeUnitOfWork()
99100
messagebus.handle(
100-
events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow
101+
commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow
101102
)
102103
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
103104
assert batch.available_quantity == 100
104105

105-
messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow)
106+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
106107

107108
assert batch.available_quantity == 50
108109

109110
def test_reallocates_if_necessary(self):
110111
uow = FakeUnitOfWork()
111-
event_history = [
112-
events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None),
113-
events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()),
114-
events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20),
115-
events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20),
112+
history = [
113+
commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None),
114+
commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()),
115+
commands.Allocate("order1", "INDIFFERENT-TABLE", 20),
116+
commands.Allocate("order2", "INDIFFERENT-TABLE", 20),
116117
]
117-
for e in event_history:
118-
messagebus.handle(e, uow)
118+
for msg in history:
119+
messagebus.handle(msg, uow)
119120
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
120121
assert batch1.available_quantity == 10
121122
assert batch2.available_quantity == 50
122123

123-
messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow)
124+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow)
124125

125126
# order1 or order2 will be deallocated, so we'll have 25 - 20
126127
assert batch1.available_quantity == 5

0 commit comments

Comments
 (0)