Skip to content

Commit 435c35a

Browse files
committed
use bootstrap in service layer tests [bootstrap_tests]
1 parent 5afd329 commit 435c35a

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

tests/unit/test_handlers.py

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import annotations
12
from datetime import date
23
from unittest import mock
34
import pytest
5+
from allocation import bootstrap
46
from allocation.adapters import repository
5-
from allocation.domain import commands, events
6-
from allocation.service_layer import handlers, messagebus, unit_of_work
7+
from allocation.domain import commands
8+
from allocation.service_layer import handlers, unit_of_work
79

810

911
class FakeRepository(repository.AbstractRepository):
@@ -38,87 +40,92 @@ def rollback(self):
3840
pass
3941

4042

43+
def bootstrap_test_app():
44+
return bootstrap.bootstrap(
45+
start_orm=False,
46+
uow=FakeUnitOfWork(),
47+
send_mail=lambda *args: None,
48+
publish=lambda *args: None,
49+
)
50+
4151

4252
class TestAddBatch:
4353

4454
def test_for_new_product(self):
45-
uow = FakeUnitOfWork()
46-
messagebus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
47-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
48-
assert uow.committed
55+
bus = bootstrap_test_app()
56+
bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None))
57+
assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None
58+
assert bus.uow.committed
4959

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

6366

6467
class TestAllocate:
6568

6669
def test_allocates(self):
67-
uow = FakeUnitOfWork()
68-
messagebus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None), uow)
69-
messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow)
70-
[batch] = uow.products.get("COMPLICATED-LAMP").batches
70+
bus = bootstrap_test_app()
71+
bus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None))
72+
bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10))
73+
[batch] = bus.uow.products.get("COMPLICATED-LAMP").batches
7174
assert batch.available_quantity == 90
7275

7376
def test_errors_for_invalid_sku(self):
74-
uow = FakeUnitOfWork()
75-
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
77+
bus = bootstrap_test_app()
78+
bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None))
7679

7780
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
78-
messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow)
81+
bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10))
7982

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

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

97105

98106
class TestChangeBatchQuantity:
99107

100108
def test_changes_available_quantity(self):
101-
uow = FakeUnitOfWork()
102-
messagebus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow)
103-
[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
104112
assert batch.available_quantity == 100
105113

106-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
107-
114+
bus.handle(commands.ChangeBatchQuantity("batch1", 50))
108115
assert batch.available_quantity == 50
109116

110117

111118
def test_reallocates_if_necessary(self):
112-
uow = FakeUnitOfWork()
113-
messagebus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), uow)
114-
messagebus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
115-
messagebus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20), uow)
116-
messagebus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20), uow)
117-
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
119+
bus = bootstrap_test_app()
120+
bus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None))
121+
bus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()))
122+
bus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20))
123+
bus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20))
124+
[batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches
118125
assert batch1.available_quantity == 10
119126
assert batch2.available_quantity == 50
120127

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

123130
# order1 or order2 will be deallocated, so we"ll have 25 - 20
124131
assert batch1.available_quantity == 5

0 commit comments

Comments
 (0)