Skip to content

Commit bb56062

Browse files
committed
change handler/services tests to use events [handler_tests]
1 parent 3ee6ed8 commit bb56062

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

tests/unit/test_handlers.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import mock
22
import pytest
3-
from allocation import services, exceptions, repository, unit_of_work
3+
from allocation import events, exceptions, messagebus, repository, unit_of_work
44

55

66
class FakeRepository(repository.AbstractRepository):
@@ -30,49 +30,57 @@ def rollback(self):
3030

3131

3232

33-
def test_add_batch_for_new_product():
34-
uow = FakeUnitOfWork()
35-
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
36-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
37-
assert uow.committed
33+
class TestAddBatch:
3834

35+
@staticmethod
36+
def test_for_new_product():
37+
uow = FakeUnitOfWork()
38+
messagebus.handle(events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow)
39+
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
40+
assert uow.committed
3941

40-
def test_add_batch_for_existing_product():
41-
uow = FakeUnitOfWork()
42-
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
43-
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
44-
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
42+
@staticmethod
43+
def test_for_existing_product():
44+
uow = FakeUnitOfWork()
45+
messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow)
46+
messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow)
47+
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
4548

4649

47-
def test_allocate_returns_allocation():
48-
uow = FakeUnitOfWork()
49-
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
50-
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
51-
assert result == "batch1"
5250

51+
class TestAllocate:
5352

54-
def test_allocate_errors_for_invalid_sku():
55-
uow = FakeUnitOfWork()
56-
services.add_batch("b1", "AREALSKU", 100, None, uow)
53+
@staticmethod
54+
def test_returns_allocation():
55+
uow = FakeUnitOfWork()
56+
messagebus.handle(events.BatchCreated("b1", "COMPLICATED-LAMP", 100, None), uow)
57+
result = messagebus.handle(events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow)
58+
assert result == "b1"
5759

58-
with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
59-
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
60+
@staticmethod
61+
def test_errors_for_invalid_sku():
62+
uow = FakeUnitOfWork()
63+
messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow)
6064

65+
with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
66+
messagebus.handle(events.AllocationRequired("o1", "NONEXISTENTSKU", 10) , uow)
6167

62-
def test_allocate_commits():
63-
uow = FakeUnitOfWork()
64-
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
65-
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
66-
assert uow.committed
6768

69+
@staticmethod
70+
def test_commits():
71+
uow = FakeUnitOfWork()
72+
messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow)
73+
messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow)
74+
assert uow.committed
6875

69-
def test_sends_email_on_out_of_stock_error():
70-
uow = FakeUnitOfWork()
71-
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
76+
@staticmethod
77+
def test_sends_email_on_out_of_stock_error():
78+
uow = FakeUnitOfWork()
79+
messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow)
7280

73-
with mock.patch("allocation.email.send_mail") as mock_send_mail:
74-
services.allocate("o1", "POPULAR-CURTAINS", 10, uow)
75-
assert mock_send_mail.call_args == mock.call(
76-
"stock@made.com",
77-
f"Out of stock for POPULAR-CURTAINS",
78-
)
81+
with mock.patch("allocation.email.send") as mock_send_mail:
82+
messagebus.handle(events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow)
83+
assert mock_send_mail.call_args == mock.call(
84+
"stock@made.com",
85+
f"Out of stock for POPULAR-CURTAINS",
86+
)

0 commit comments

Comments
 (0)