|
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):
|
@@ -32,44 +34,53 @@ def rollback(self):
|
32 | 34 | class TestAddBatch:
|
33 | 35 | def test_for_new_product(self):
|
34 | 36 | 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 | + ) |
36 | 40 | assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
|
37 | 41 | assert uow.committed
|
38 | 42 |
|
39 | 43 | def test_for_existing_product(self):
|
40 | 44 | 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) |
43 | 47 | assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
|
44 | 48 |
|
45 | 49 |
|
46 | 50 | class TestAllocate:
|
47 | 51 | def test_returns_allocation(self):
|
48 | 52 | 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 | + ) |
51 | 59 | assert result == "batch1"
|
52 | 60 |
|
53 | 61 | def test_errors_for_invalid_sku(self):
|
54 | 62 | uow = FakeUnitOfWork()
|
55 |
| - services.add_batch("b1", "AREALSKU", 100, None, uow) |
| 63 | + messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow) |
56 | 64 |
|
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 | + ) |
59 | 69 |
|
60 | 70 | def test_commits(self):
|
61 | 71 | 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) |
64 | 74 | assert uow.committed
|
65 | 75 |
|
66 | 76 | def test_sends_email_on_out_of_stock_error(self):
|
67 | 77 | uow = FakeUnitOfWork()
|
68 |
| - services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow) |
| 78 | + messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow) |
69 | 79 |
|
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 | + ) |
72 | 84 | 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" |
75 | 86 | )
|
0 commit comments