Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96e76ec

Browse files
committed
use bootstrap in service layer tests [bootstrap_tests]
1 parent b3e08bc commit 96e76ec

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

tests/unit/test_handlers.py

+52-62
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):
@@ -39,113 +41,101 @@ def rollback(self):
3941
pass
4042

4143

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

4353
class TestAddBatch:
4454

4555
def test_for_new_product(self):
46-
uow = FakeUnitOfWork()
47-
messagebus.handle(
48-
commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
49-
)
50-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
51-
assert uow.committed
56+
bus = bootstrap_test_app()
57+
bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None))
58+
assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None
59+
assert bus.uow.committed
5260

5361

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

6768

6869

6970
class TestAllocate:
7071

7172
def test_allocates(self):
72-
uow = FakeUnitOfWork()
73-
messagebus.handle(
74-
commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None), uow
75-
)
76-
messagebus.handle(
77-
commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow
78-
)
79-
[batch] = uow.products.get("COMPLICATED-LAMP").batches
73+
bus = bootstrap_test_app()
74+
bus.handle(commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None))
75+
bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10))
76+
[batch] = bus.uow.products.get("COMPLICATED-LAMP").batches
8077
assert batch.available_quantity == 90
8178

8279

8380
def test_errors_for_invalid_sku(self):
84-
uow = FakeUnitOfWork()
85-
messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow)
81+
bus = bootstrap_test_app()
82+
bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None))
8683

8784
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
88-
messagebus.handle(
89-
commands.Allocate("o1", "NONEXISTENTSKU", 10), uow
90-
)
85+
bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10))
9186

9287
def test_commits(self):
93-
uow = FakeUnitOfWork()
94-
messagebus.handle(
95-
commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow
96-
)
97-
messagebus.handle(
98-
commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow
99-
)
100-
assert uow.committed
88+
bus = bootstrap_test_app()
89+
bus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None))
90+
bus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10))
91+
assert bus.uow.committed
10192

10293

10394
def test_sends_email_on_out_of_stock_error(self):
104-
uow = FakeUnitOfWork()
105-
messagebus.handle(
106-
commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow
95+
emails = []
96+
def fake_send_mail(*args):
97+
emails.append(args)
98+
bus = bootstrap.bootstrap(
99+
start_orm=False,
100+
uow=FakeUnitOfWork(),
101+
send_mail=fake_send_mail,
102+
publish=lambda *args: None,
107103
)
108-
109-
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
110-
messagebus.handle(
111-
commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow
112-
)
113-
assert mock_send_mail.call_args == mock.call(
114-
"[email protected]", f"Out of stock for POPULAR-CURTAINS"
115-
)
104+
bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
105+
bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
106+
assert emails == [
107+
("[email protected]", f"Out of stock for POPULAR-CURTAINS"),
108+
]
116109

117110

118111

119112
class TestChangeBatchQuantity:
120113

121114
def test_changes_available_quantity(self):
122-
uow = FakeUnitOfWork()
123-
messagebus.handle(
124-
commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow
125-
)
126-
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
115+
bus = bootstrap_test_app()
116+
bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None))
117+
[batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches
127118
assert batch.available_quantity == 100
128119

129-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow)
130-
120+
bus.handle(commands.ChangeBatchQuantity("batch1", 50))
131121
assert batch.available_quantity == 50
132122

133123

134124
def test_reallocates_if_necessary(self):
135-
uow = FakeUnitOfWork()
125+
bus = bootstrap_test_app()
136126
history = [
137127
commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None),
138128
commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()),
139129
commands.Allocate("order1", "INDIFFERENT-TABLE", 20),
140130
commands.Allocate("order2", "INDIFFERENT-TABLE", 20),
141131
]
142132
for msg in history:
143-
messagebus.handle(msg, uow)
144-
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
133+
bus.handle(msg)
134+
[batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches
145135
assert batch1.available_quantity == 10
146136
assert batch2.available_quantity == 50
147137

148-
messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow)
138+
bus.handle(commands.ChangeBatchQuantity("batch1", 25))
149139

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

0 commit comments

Comments
 (0)