|
1 | 1 | # pylint: disable=no-self-use |
2 | 2 | from unittest import mock |
3 | 3 | import pytest |
| 4 | + |
4 | 5 | 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 |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class FakeRepository(repository.AbstractRepository): |
@@ -36,49 +38,64 @@ class TestAddBatch: |
36 | 38 |
|
37 | 39 | def test_for_new_product(self): |
38 | 40 | 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 | + ) |
40 | 44 | assert uow.products.get("CRUNCHY-ARMCHAIR") is not None |
41 | 45 | assert uow.committed |
42 | 46 |
|
43 | 47 |
|
44 | 48 | def test_for_existing_product(self): |
45 | 49 | 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) |
48 | 52 | assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] |
49 | 53 |
|
50 | 54 |
|
| 55 | + |
51 | 56 | class TestAllocate: |
52 | 57 |
|
53 | 58 | def test_returns_allocation(self): |
54 | 59 | 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 | + ) |
57 | 66 | assert result == "batch1" |
58 | 67 |
|
59 | 68 |
|
60 | 69 | def test_errors_for_invalid_sku(self): |
61 | 70 | 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) |
66 | 72 |
|
| 73 | + with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"): |
| 74 | + messagebus.handle( |
| 75 | + events.AllocationRequired("o1", "NONEXISTENTSKU", 10), uow |
| 76 | + ) |
67 | 77 |
|
68 | 78 | def test_commits(self): |
69 | 79 | 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 | + ) |
72 | 86 | assert uow.committed |
73 | 87 |
|
74 | 88 |
|
75 | 89 | def test_sends_email_on_out_of_stock_error(self): |
76 | 90 | 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 | + ) |
78 | 94 |
|
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 | + ) |
81 | 99 | 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" |
84 | 101 | ) |
0 commit comments