Skip to content

Commit 27c2e48

Browse files
committed
handlers take commands now, modify tests too
1 parent 5c52ee1 commit 27c2e48

2 files changed

Lines changed: 34 additions & 36 deletions

File tree

‎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: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from datetime import date
22
from unittest import mock
33
import pytest
4-
54
from allocation.adapters import repository
6-
from allocation.domain import events
5+
from allocation.domain import commands, events
76
from allocation.service_layer import handlers, messagebus, unit_of_work
87

98

@@ -39,56 +38,58 @@ def rollback(self):
3938
pass
4039

4140

41+
4242
class TestAddBatch:
4343

4444
@staticmethod
4545
def test_for_new_product():
4646
uow = FakeUnitOfWork()
47-
messagebus.handle(events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
47+
messagebus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
4848
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
4949
assert uow.committed
5050

5151
@staticmethod
5252
def test_for_existing_product():
5353
uow = FakeUnitOfWork()
54-
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
55-
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
54+
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
55+
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
5656
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
5757

5858

5959

6060
class TestAllocate:
6161

6262
@staticmethod
63-
def test_returns_allocation():
63+
def test_allocates():
6464
uow = FakeUnitOfWork()
65-
messagebus.handle(events.BatchCreated("b1", "COMPLICATED-LAMP", 100, None), uow)
66-
results = messagebus.handle(events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow)
67-
assert results.pop() == "b1"
65+
messagebus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None), uow)
66+
result = messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow)
67+
assert result == "b1"
68+
[batch] = uow.products.get("COMPLICATED-LAMP").batches
69+
assert batch.available_quantity == 90
6870

6971
@staticmethod
7072
def test_errors_for_invalid_sku():
7173
uow = FakeUnitOfWork()
72-
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
74+
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
7375

7476
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
75-
messagebus.handle(events.AllocationRequired("o1", "NONEXISTENTSKU", 10) , uow)
76-
77+
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow)
7778

7879
@staticmethod
7980
def test_commits():
8081
uow = FakeUnitOfWork()
81-
messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow)
82-
messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow)
82+
messagebus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow)
83+
messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow)
8384
assert uow.committed
8485

8586
@staticmethod
8687
def test_sends_email_on_out_of_stock_error():
8788
uow = FakeUnitOfWork()
88-
messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow)
89+
messagebus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow)
8990

9091
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
91-
messagebus.handle(events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow)
92+
messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow)
9293
assert mock_send_mail.call_args == mock.call(
9394
"stock@made.com",
9495
f"Out of stock for POPULAR-CURTAINS",
@@ -100,26 +101,26 @@ class TestChangeBatchQuantity:
100101
@staticmethod
101102
def test_changes_available_quantity():
102103
uow = FakeUnitOfWork()
103-
messagebus.handle(events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow)
104+
messagebus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow)
104105
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
105106
assert batch.available_quantity == 100
106107

107-
messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow)
108+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
108109

109110
assert batch.available_quantity == 50
110111

111112

112113
@staticmethod
113114
def test_reallocates_if_necessary():
114115
uow = FakeUnitOfWork()
115-
messagebus.handle(events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None), uow)
116-
messagebus.handle(events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
117-
messagebus.handle(events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20), uow)
118-
messagebus.handle(events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20), uow)
116+
messagebus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), uow)
117+
messagebus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
118+
messagebus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20), uow)
119+
messagebus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20), uow)
119120
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
120121
assert batch1.available_quantity == 10
121122

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

124125
# order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1
125126
assert batch1.available_quantity == 5

0 commit comments

Comments
 (0)