Skip to content

Commit f1fa974

Browse files
committed
use a FakeBus for service layer tests [fakebus]
1 parent f1a16e2 commit f1fa974

1 file changed

Lines changed: 51 additions & 48 deletions

File tree

‎tests/unit/test_handlers.py‎

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from __future__ import annotations
12
from datetime import date
23
from unittest import mock
34
import pytest
45
from allocation.adapters import repository
5-
from allocation.domain import commands, events
6+
from allocation.domain import commands
67
from allocation.service_layer import handlers, messagebus, unit_of_work
78

89

@@ -39,93 +40,95 @@ def rollback(self):
3940

4041

4142

43+
class FakeBus(messagebus.MessageBus):
44+
def __init__(self):
45+
uow = FakeUnitOfWork()
46+
super().__init__(
47+
uow=uow,
48+
send_mail=mock.Mock(),
49+
publish=mock.Mock(),
50+
)
51+
uow.bus = self
52+
53+
54+
4255
class TestAddBatch:
4356

4457
@staticmethod
4558
def test_for_new_product():
46-
uow = FakeUnitOfWork()
47-
messagebus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
48-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
49-
assert uow.committed
59+
bus = FakeBus()
60+
bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None))
61+
assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None
62+
assert bus.uow.committed
5063

5164
@staticmethod
5265
def test_for_existing_product():
53-
uow = FakeUnitOfWork()
54-
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
55-
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
56-
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
57-
58-
59-
60-
@pytest.fixture(autouse=True)
61-
def fake_redis_publish():
62-
with mock.patch("allocation.adapters.redis_eventpublisher.publish"):
63-
yield
66+
bus = FakeBus()
67+
bus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None))
68+
bus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None))
69+
assert "b2" in [b.reference for b in bus.uow.products.get("GARISH-RUG").batches]
6470

6571

6672
class TestAllocate:
6773

6874
@staticmethod
6975
def test_allocates():
70-
uow = FakeUnitOfWork()
71-
messagebus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None), uow)
72-
messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow)
73-
[batch] = uow.products.get("COMPLICATED-LAMP").batches
76+
bus = FakeBus()
77+
bus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None))
78+
bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10))
79+
[batch] = bus.uow.products.get("COMPLICATED-LAMP").batches
7480
assert batch.available_quantity == 90
7581

7682
@staticmethod
7783
def test_errors_for_invalid_sku():
78-
uow = FakeUnitOfWork()
79-
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
84+
bus = FakeBus()
85+
bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None))
8086

8187
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
82-
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow)
88+
bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10))
8389

8490
@staticmethod
8591
def test_commits():
86-
uow = FakeUnitOfWork()
87-
messagebus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow)
88-
messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow)
89-
assert uow.committed
92+
bus = FakeBus()
93+
bus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None))
94+
bus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10))
95+
assert bus.uow.committed
9096

9197
@staticmethod
9298
def test_sends_email_on_out_of_stock_error():
93-
uow = FakeUnitOfWork()
94-
messagebus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow)
95-
96-
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
97-
messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow)
98-
assert mock_send_mail.call_args == mock.call(
99-
"stock@made.com",
100-
f"Out of stock for POPULAR-CURTAINS",
101-
)
99+
bus = FakeBus()
100+
bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
101+
bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
102+
assert bus.dependencies["send_mail"].call_args == mock.call(
103+
"stock@made.com",
104+
f"Out of stock for POPULAR-CURTAINS",
105+
)
102106

103107

104108
class TestChangeBatchQuantity:
105109

106110
@staticmethod
107111
def test_changes_available_quantity():
108-
uow = FakeUnitOfWork()
109-
messagebus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow)
110-
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
112+
bus = FakeBus()
113+
bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None))
114+
[batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches
111115
assert batch.available_quantity == 100
112116

113-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
114-
117+
bus.handle(commands.ChangeBatchQuantity("batch1", 50))
115118
assert batch.available_quantity == 50
116119

117120

118121
@staticmethod
119122
def test_reallocates_if_necessary():
120-
uow = FakeUnitOfWork()
121-
messagebus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), uow)
122-
messagebus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
123-
messagebus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20), uow)
124-
messagebus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20), uow)
125-
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
123+
bus = FakeBus()
124+
bus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None))
125+
bus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()))
126+
bus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20))
127+
bus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20))
128+
[batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches
126129
assert batch1.available_quantity == 10
127130

128-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow)
131+
bus.handle(commands.ChangeBatchQuantity("batch1", 25))
129132

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

0 commit comments

Comments
 (0)