Skip to content

Commit ed224de

Browse files
committed
tests change to use bus [handler_tests]
1 parent 6c65c1f commit ed224de

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

tests/unit/test_handlers.py

Lines changed: 27 additions & 16 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):
@@ -32,44 +34,53 @@ def rollback(self):
3234
class TestAddBatch:
3335
def test_for_new_product(self):
3436
uow = FakeUnitOfWork()
35-
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
37+
messagebus.handle(
38+
events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow
39+
)
3640
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
3741
assert uow.committed
3842

3943
def test_for_existing_product(self):
4044
uow = FakeUnitOfWork()
41-
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
42-
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
45+
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
46+
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
4347
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
4448

4549

4650
class TestAllocate:
4751
def test_returns_allocation(self):
4852
uow = FakeUnitOfWork()
49-
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
50-
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
53+
messagebus.handle(
54+
events.BatchCreated("batch1", "COMPLICATED-LAMP", 100, None), uow
55+
)
56+
result = messagebus.handle(
57+
events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow
58+
)
5159
assert result == "batch1"
5260

5361
def test_errors_for_invalid_sku(self):
5462
uow = FakeUnitOfWork()
55-
services.add_batch("b1", "AREALSKU", 100, None, uow)
63+
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
5664

57-
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
58-
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
65+
with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
66+
messagebus.handle(
67+
events.AllocationRequired("o1", "NONEXISTENTSKU", 10), uow
68+
)
5969

6070
def test_commits(self):
6171
uow = FakeUnitOfWork()
62-
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
63-
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
72+
messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow)
73+
messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow)
6474
assert uow.committed
6575

6676
def test_sends_email_on_out_of_stock_error(self):
6777
uow = FakeUnitOfWork()
68-
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
78+
messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow)
6979

70-
with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail:
71-
services.allocate("o1", "POPULAR-CURTAINS", 10, uow)
80+
with mock.patch("allocation.adapters.email.send") as mock_send_mail:
81+
messagebus.handle(
82+
events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow
83+
)
7284
assert mock_send_mail.call_args == mock.call(
73-
"stock@made.com",
74-
f"Out of stock for POPULAR-CURTAINS",
85+
"stock@made.com", f"Out of stock for POPULAR-CURTAINS"
7586
)

0 commit comments

Comments
 (0)