Skip to content

Commit 53a99b3

Browse files
committed
handlers take commands now, modify tests too
1 parent ab409fd commit 53a99b3

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/allocation/service_layer/handlers.py

Lines changed: 10 additions & 13 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
if TYPE_CHECKING:
77
from . import unit_of_work
@@ -12,23 +12,23 @@ class InvalidSku(Exception):
1212

1313

1414
def add_batch(
15-
event: events.BatchCreated, uow: unit_of_work.AbstractUnitOfWork
15+
cmd: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork
1616
):
1717
with uow:
18-
product = uow.products.get(sku=event.sku)
18+
product = uow.products.get(sku=cmd.sku)
1919
if product is None:
20-
product = model.Product(event.sku, batches=[])
20+
product = model.Product(cmd.sku, batches=[])
2121
uow.products.add(product)
2222
product.batches.append(model.Batch(
23-
event.ref, event.sku, event.qty, event.eta
23+
cmd.ref, cmd.sku, cmd.qty, cmd.eta
2424
))
2525
uow.commit()
2626

2727

2828
def allocate(
29-
event: events.AllocationRequired, uow: unit_of_work.AbstractUnitOfWork
29+
cmd: commands.Allocate, uow: unit_of_work.AbstractUnitOfWork
3030
) -> str:
31-
line = OrderLine(event.orderid, event.sku, event.qty)
31+
line = OrderLine(cmd.orderid, cmd.sku, cmd.qty)
3232
with uow:
3333
product = uow.products.get(sku=line.sku)
3434
if product is None:
@@ -38,18 +38,15 @@ def allocate(
3838
return batchref
3939

4040

41-
4241
def change_batch_quantity(
43-
event: events.BatchQuantityChanged, uow: unit_of_work.AbstractUnitOfWork
42+
cmd: commands.ChangeBatchQuantity, uow: unit_of_work.AbstractUnitOfWork
4443
):
4544
with uow:
46-
product = uow.products.get_by_batchref(batchref=event.ref)
47-
product.change_batch_quantity(ref=event.ref, qty=event.qty)
45+
product = uow.products.get_by_batchref(batchref=cmd.ref)
46+
product.change_batch_quantity(ref=cmd.ref, qty=cmd.qty)
4847
uow.commit()
4948

5049

51-
# pylint: disable=unused-argument
52-
5350
def send_out_of_stock_notification(
5451
event: events.OutOfStock, uow: unit_of_work.AbstractUnitOfWork,
5552
):

tests/unit/test_handlers.py

Lines changed: 25 additions & 24 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

@@ -46,61 +45,63 @@ class TestAddBatch:
4645
def test_for_new_product(self):
4746
uow = FakeUnitOfWork()
4847
messagebus.handle(
49-
events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
48+
commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
5049
)
5150
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
5251
assert uow.committed
5352

5453

5554
def test_for_existing_product(self):
5655
uow = FakeUnitOfWork()
57-
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
58-
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
56+
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
57+
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
5958
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
6059

6160

6261
class TestAllocate:
6362

64-
def test_returns_allocation(self):
63+
def test_allocates(self):
6564
uow = FakeUnitOfWork()
6665
messagebus.handle(
67-
events.BatchCreated("batch1", "COMPLICATED-LAMP", 100, None), uow
66+
commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None), uow
6867
)
6968
results = messagebus.handle(
70-
events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow
69+
commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow
7170
)
7271
assert results.pop(0) == "batch1"
72+
[batch] = uow.products.get("COMPLICATED-LAMP").batches
73+
assert batch.available_quantity == 90
7374

7475

7576
def test_errors_for_invalid_sku(self):
7677
uow = FakeUnitOfWork()
77-
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
78+
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
7879

7980
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
8081
messagebus.handle(
81-
events.AllocationRequired("o1", "NONEXISTENTSKU", 10), uow
82+
commands.Allocate("o1", "NONEXISTENTSKU", 10), uow
8283
)
8384

8485
def test_commits(self):
8586
uow = FakeUnitOfWork()
8687
messagebus.handle(
87-
events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow
88+
commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow
8889
)
8990
messagebus.handle(
90-
events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow
91+
commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow
9192
)
9293
assert uow.committed
9394

9495

9596
def test_sends_email_on_out_of_stock_error(self):
9697
uow = FakeUnitOfWork()
9798
messagebus.handle(
98-
events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow
99+
commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow
99100
)
100101

101102
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
102103
messagebus.handle(
103-
events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow
104+
commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow
104105
)
105106
assert mock_send_mail.call_args == mock.call(
106107
"stock@made.com", f"Out of stock for POPULAR-CURTAINS"
@@ -113,31 +114,31 @@ class TestChangeBatchQuantity:
113114
def test_changes_available_quantity(self):
114115
uow = FakeUnitOfWork()
115116
messagebus.handle(
116-
events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow
117+
commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow
117118
)
118119
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
119120
assert batch.available_quantity == 100
120121

121-
messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow)
122+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
122123

123124
assert batch.available_quantity == 50
124125

125126

126127
def test_reallocates_if_necessary(self):
127128
uow = FakeUnitOfWork()
128-
event_history = [
129-
events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None),
130-
events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()),
131-
events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20),
132-
events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20),
129+
history = [
130+
commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None),
131+
commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()),
132+
commands.Allocate("order1", "INDIFFERENT-TABLE", 20),
133+
commands.Allocate("order2", "INDIFFERENT-TABLE", 20),
133134
]
134-
for e in event_history:
135-
messagebus.handle(e, uow)
135+
for msg in history:
136+
messagebus.handle(msg, uow)
136137
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
137138
assert batch1.available_quantity == 10
138139
assert batch2.available_quantity == 50
139140

140-
messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow)
141+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow)
141142

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

0 commit comments

Comments
 (0)