Skip to content

Commit 0bc35ef

Browse files
committed
tests change to use bus [handler_tests]
1 parent 0182ccf commit 0bc35ef

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

tests/unit/test_handlers.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# pylint: disable=no-self-use
22
from unittest import mock
33
import pytest
4+
45
from allocation.adapters import repository
5-
from allocation.service_layer import services, unit_of_work
6+
from allocation.domain import events
7+
from allocation.service_layer import handlers, messagebus, unit_of_work
68

79

810
class FakeRepository(repository.AbstractRepository):
@@ -36,49 +38,64 @@ class TestAddBatch:
3638

3739
def test_for_new_product(self):
3840
uow = FakeUnitOfWork()
39-
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
41+
messagebus.handle(
42+
events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
43+
)
4044
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
4145
assert uow.committed
4246

4347

4448
def test_for_existing_product(self):
4549
uow = FakeUnitOfWork()
46-
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
47-
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
50+
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
51+
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
4852
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
4953

5054

55+
5156
class TestAllocate:
5257

5358
def test_returns_allocation(self):
5459
uow = FakeUnitOfWork()
55-
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
56-
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
60+
messagebus.handle(
61+
events.BatchCreated("batch1", "COMPLICATED-LAMP", 100, None), uow
62+
)
63+
result = messagebus.handle(
64+
events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow
65+
)
5766
assert result == "batch1"
5867

5968

6069
def test_errors_for_invalid_sku(self):
6170
uow = FakeUnitOfWork()
62-
services.add_batch("b1", "AREALSKU", 100, None, uow)
63-
64-
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
65-
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
71+
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
6672

73+
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
74+
messagebus.handle(
75+
events.AllocationRequired("o1", "NONEXISTENTSKU", 10), uow
76+
)
6777

6878
def test_commits(self):
6979
uow = FakeUnitOfWork()
70-
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
71-
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
80+
messagebus.handle(
81+
events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow
82+
)
83+
messagebus.handle(
84+
events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow
85+
)
7286
assert uow.committed
7387

7488

7589
def test_sends_email_on_out_of_stock_error(self):
7690
uow = FakeUnitOfWork()
77-
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
91+
messagebus.handle(
92+
events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow
93+
)
7894

79-
with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail:
80-
services.allocate("o1", "POPULAR-CURTAINS", 10, uow)
95+
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
96+
messagebus.handle(
97+
events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow
98+
)
8199
assert mock_send_mail.call_args == mock.call(
82-
"stock@made.com",
83-
f"Out of stock for POPULAR-CURTAINS",
100+
"stock@made.com", f"Out of stock for POPULAR-CURTAINS"
84101
)

0 commit comments

Comments
 (0)