Skip to content

Commit 1e27052

Browse files
committed
handlers take commands now, modify tests too
1 parent 4c23280 commit 1e27052

2 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/allocation/handlers.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
3-
from allocation import events, email, exceptions, model
3+
from allocation import commands, events, email, exceptions, model
44
from allocation.model import OrderLine
55
if TYPE_CHECKING:
66
from allocation import unit_of_work
77

88

99
def add_batch(
10-
event: events.BatchCreated, uow: unit_of_work.AbstractUnitOfWork
10+
cmd: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork
1111
):
1212
with uow:
13-
product = uow.products.get(sku=event.sku)
13+
product = uow.products.get(sku=cmd.sku)
1414
if product is None:
15-
product = model.Product(event.sku, batches=[])
15+
product = model.Product(cmd.sku, batches=[])
1616
uow.products.add(product)
1717
product.batches.append(model.Batch(
18-
event.ref, event.sku, event.qty, event.eta
18+
cmd.ref, cmd.sku, cmd.qty, cmd.eta
1919
))
2020
uow.commit()
2121

2222

2323
def allocate(
24-
event: events.AllocationRequired, uow: unit_of_work.AbstractUnitOfWork
24+
cmd: commands.Allocate, uow: unit_of_work.AbstractUnitOfWork
2525
) -> str:
26-
line = OrderLine(event.orderid, event.sku, event.qty)
26+
line = OrderLine(cmd.orderid, cmd.sku, cmd.qty)
2727
with uow:
2828
product = uow.products.get(sku=line.sku)
2929
if product is None:
@@ -33,18 +33,15 @@ def allocate(
3333
return batchref
3434

3535

36-
3736
def change_batch_quantity(
38-
event: events.BatchQuantityChanged, uow: unit_of_work.AbstractUnitOfWork
37+
cmd: commands.ChangeBatchQuantity, uow: unit_of_work.AbstractUnitOfWork
3938
):
4039
with uow:
41-
product = uow.products.get_by_batchref(batchref=event.ref)
42-
product.change_batch_quantity(ref=event.ref, qty=event.qty)
40+
product = uow.products.get_by_batchref(batchref=cmd.ref)
41+
product.change_batch_quantity(ref=cmd.ref, qty=cmd.qty)
4342
uow.commit()
4443

4544

46-
# pylint: disable=unused-argument
47-
4845
def send_out_of_stock_notification(
4946
event: events.OutOfStock, uow: unit_of_work.AbstractUnitOfWork,
5047
):

tests/unit/test_handlers.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import date
22
from unittest import mock
33
import pytest
4-
from allocation import events, exceptions, messagebus, repository, unit_of_work
4+
from allocation import commands, exceptions, messagebus, repository, unit_of_work
55

66

77
class FakeRepository(repository.AbstractRepository):
@@ -36,56 +36,58 @@ def rollback(self):
3636
pass
3737

3838

39+
3940
class TestAddBatch:
4041

4142
@staticmethod
4243
def test_for_new_product():
4344
uow = FakeUnitOfWork()
44-
messagebus.handle(events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
45+
messagebus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
4546
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
4647
assert uow.committed
4748

4849
@staticmethod
4950
def test_for_existing_product():
5051
uow = FakeUnitOfWork()
51-
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
52-
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
52+
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
53+
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
5354
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
5455

5556

5657

5758
class TestAllocate:
5859

5960
@staticmethod
60-
def test_returns_allocation():
61+
def test_allocates():
6162
uow = FakeUnitOfWork()
62-
messagebus.handle(events.BatchCreated("b1", "COMPLICATED-LAMP", 100, None), uow)
63-
results = messagebus.handle(events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow)
64-
assert results.pop() == "b1"
63+
messagebus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None), uow)
64+
result = messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow)
65+
assert result == "b1"
66+
[batch] = uow.products.get("COMPLICATED-LAMP").batches
67+
assert batch.available_quantity == 90
6568

6669
@staticmethod
6770
def test_errors_for_invalid_sku():
6871
uow = FakeUnitOfWork()
69-
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
72+
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
7073

7174
with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
72-
messagebus.handle(events.AllocationRequired("o1", "NONEXISTENTSKU", 10) , uow)
73-
75+
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10) , uow)
7476

7577
@staticmethod
7678
def test_commits():
7779
uow = FakeUnitOfWork()
78-
messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow)
79-
messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow)
80+
messagebus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow)
81+
messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow)
8082
assert uow.committed
8183

8284
@staticmethod
8385
def test_sends_email_on_out_of_stock_error():
8486
uow = FakeUnitOfWork()
85-
messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow)
87+
messagebus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow)
8688

8789
with mock.patch("allocation.email.send") as mock_send_mail:
88-
messagebus.handle(events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow)
90+
messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow)
8991
assert mock_send_mail.call_args == mock.call(
9092
9193
f"Out of stock for POPULAR-CURTAINS",
@@ -97,26 +99,26 @@ class TestChangeBatchQuantity:
9799
@staticmethod
98100
def test_changes_available_quantity():
99101
uow = FakeUnitOfWork()
100-
messagebus.handle(events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow)
102+
messagebus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow)
101103
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
102104
assert batch.available_quantity == 100
103105

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

106108
assert batch.available_quantity == 50
107109

108110

109111
@staticmethod
110112
def test_reallocates_if_necessary():
111113
uow = FakeUnitOfWork()
112-
messagebus.handle(events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None), uow)
113-
messagebus.handle(events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
114-
messagebus.handle(events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20), uow)
115-
messagebus.handle(events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20), uow)
114+
messagebus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), uow)
115+
messagebus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
116+
messagebus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20), uow)
117+
messagebus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20), uow)
116118
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
117119
assert batch1.available_quantity == 10
118120

119-
messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow)
121+
messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow)
120122

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

0 commit comments

Comments
 (0)