Skip to content

Commit f923760

Browse files
committed
use bootstrap in service layer tests [bootstrap_tests]
1 parent c3282c9 commit f923760

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

tests/unit/test_handlers.py

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# pylint: disable=no-self-use
2+
from __future__ import annotations
23
from datetime import date
34
from unittest import mock
45
import pytest
6+
from allocation import bootstrap
57
from allocation.adapters import repository
6-
from allocation.domain import commands, events
7-
from allocation.service_layer import handlers, messagebus, unit_of_work
8+
from allocation.domain import commands
9+
from allocation.service_layer import handlers, unit_of_work
810

911

1012
class FakeRepository(repository.AbstractRepository):
@@ -37,94 +39,96 @@ def rollback(self):
3739
pass
3840

3941

42+
def bootstrap_test_app():
43+
return bootstrap.bootstrap(
44+
start_orm=False,
45+
uow=FakeUnitOfWork(),
46+
send_mail=lambda *args: None,
47+
publish=lambda *args: None,
48+
)
49+
50+
4051
class TestAddBatch:
4152
def test_for_new_product(self):
42-
uow = FakeUnitOfWork()
43-
messagebus.handle(
44-
commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
45-
)
46-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
47-
assert uow.committed
53+
bus = bootstrap_test_app()
54+
bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None))
55+
assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None
56+
assert bus.uow.committed
4857

4958
def test_for_existing_product(self):
50-
uow = FakeUnitOfWork()
51-
messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow)
52-
messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow)
53-
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
54-
55-
56-
@pytest.fixture(autouse=True)
57-
def fake_redis_publish():
58-
with mock.patch("allocation.adapters.redis_eventpublisher.publish"):
59-
yield
59+
bus = bootstrap_test_app()
60+
bus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None))
61+
bus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None))
62+
assert "b2" in [
63+
b.reference for b in bus.uow.products.get("GARISH-RUG").batches
64+
]
6065

6166

6267
class TestAllocate:
6368
def test_allocates(self):
64-
uow = FakeUnitOfWork()
65-
messagebus.handle(
66-
commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None), uow
67-
)
68-
messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow)
69-
[batch] = uow.products.get("COMPLICATED-LAMP").batches
69+
bus = bootstrap_test_app()
70+
bus.handle(commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None))
71+
bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10))
72+
[batch] = bus.uow.products.get("COMPLICATED-LAMP").batches
7073
assert batch.available_quantity == 90
7174

7275
def test_errors_for_invalid_sku(self):
73-
uow = FakeUnitOfWork()
74-
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
76+
bus = bootstrap_test_app()
77+
bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None))
7578

7679
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
77-
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow)
80+
bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10))
7881

7982
def test_commits(self):
80-
uow = FakeUnitOfWork()
81-
messagebus.handle(
82-
commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow
83-
)
84-
messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow)
85-
assert uow.committed
83+
bus = bootstrap_test_app()
84+
bus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None))
85+
bus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10))
86+
assert bus.uow.committed
8687

8788
def test_sends_email_on_out_of_stock_error(self):
88-
uow = FakeUnitOfWork()
89-
messagebus.handle(
90-
commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow
91-
)
89+
emails = []
9290

93-
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
94-
messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow)
95-
assert mock_send_mail.call_args == mock.call(
96-
"[email protected]", f"Out of stock for POPULAR-CURTAINS"
97-
)
91+
def fake_send_mail(*args):
92+
emails.append(args)
93+
94+
bus = bootstrap.bootstrap(
95+
start_orm=False,
96+
uow=FakeUnitOfWork(),
97+
send_mail=fake_send_mail,
98+
publish=lambda *args: None,
99+
)
100+
bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
101+
bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
102+
assert emails == [
103+
("[email protected]", f"Out of stock for POPULAR-CURTAINS"),
104+
]
98105

99106

100107
class TestChangeBatchQuantity:
101108
def test_changes_available_quantity(self):
102-
uow = FakeUnitOfWork()
103-
messagebus.handle(
104-
commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow
105-
)
106-
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
109+
bus = bootstrap_test_app()
110+
bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None))
111+
[batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches
107112
assert batch.available_quantity == 100
108113

109-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
110-
114+
bus.handle(commands.ChangeBatchQuantity("batch1", 50))
111115
assert batch.available_quantity == 50
112116

113117
def test_reallocates_if_necessary(self):
114-
uow = FakeUnitOfWork()
118+
bus = bootstrap_test_app()
115119
history = [
116120
commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None),
117121
commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()),
118122
commands.Allocate("order1", "INDIFFERENT-TABLE", 20),
119123
commands.Allocate("order2", "INDIFFERENT-TABLE", 20),
120124
]
121125
for msg in history:
122-
messagebus.handle(msg, uow)
123-
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
126+
bus.handle(msg)
127+
[batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches
124128
assert batch1.available_quantity == 10
125129
assert batch2.available_quantity == 50
126130

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

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

0 commit comments

Comments
 (0)