|
| 1 | +from __future__ import annotations |
1 | 2 | from datetime import date |
2 | 3 | from unittest import mock |
3 | 4 | import pytest |
4 | 5 | from allocation.adapters import repository |
5 | | -from allocation.domain import commands, events |
| 6 | +from allocation.domain import commands |
6 | 7 | from allocation.service_layer import handlers, messagebus, unit_of_work |
7 | 8 |
|
8 | 9 |
|
@@ -39,93 +40,95 @@ def rollback(self): |
39 | 40 |
|
40 | 41 |
|
41 | 42 |
|
| 43 | +class FakeBus(messagebus.MessageBus): |
| 44 | + def __init__(self): |
| 45 | + uow = FakeUnitOfWork() |
| 46 | + super().__init__( |
| 47 | + uow=uow, |
| 48 | + send_mail=mock.Mock(), |
| 49 | + publish=mock.Mock(), |
| 50 | + ) |
| 51 | + uow.bus = self |
| 52 | + |
| 53 | + |
| 54 | + |
42 | 55 | class TestAddBatch: |
43 | 56 |
|
44 | 57 | @staticmethod |
45 | 58 | def test_for_new_product(): |
46 | | - uow = FakeUnitOfWork() |
47 | | - messagebus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow) |
48 | | - assert uow.products.get("CRUNCHY-ARMCHAIR") is not None |
49 | | - assert uow.committed |
| 59 | + bus = FakeBus() |
| 60 | + bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None)) |
| 61 | + assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None |
| 62 | + assert bus.uow.committed |
50 | 63 |
|
51 | 64 | @staticmethod |
52 | 65 | def test_for_existing_product(): |
53 | | - uow = FakeUnitOfWork() |
54 | | - messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow) |
55 | | - messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow) |
56 | | - assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] |
57 | | - |
58 | | - |
59 | | - |
60 | | -@pytest.fixture(autouse=True) |
61 | | -def fake_redis_publish(): |
62 | | - with mock.patch("allocation.adapters.redis_eventpublisher.publish"): |
63 | | - yield |
| 66 | + bus = FakeBus() |
| 67 | + bus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None)) |
| 68 | + bus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None)) |
| 69 | + assert "b2" in [b.reference for b in bus.uow.products.get("GARISH-RUG").batches] |
64 | 70 |
|
65 | 71 |
|
66 | 72 | class TestAllocate: |
67 | 73 |
|
68 | 74 | @staticmethod |
69 | 75 | def test_allocates(): |
70 | | - uow = FakeUnitOfWork() |
71 | | - messagebus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None), uow) |
72 | | - messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow) |
73 | | - [batch] = uow.products.get("COMPLICATED-LAMP").batches |
| 76 | + bus = FakeBus() |
| 77 | + bus.handle(commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None)) |
| 78 | + bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10)) |
| 79 | + [batch] = bus.uow.products.get("COMPLICATED-LAMP").batches |
74 | 80 | assert batch.available_quantity == 90 |
75 | 81 |
|
76 | 82 | @staticmethod |
77 | 83 | def test_errors_for_invalid_sku(): |
78 | | - uow = FakeUnitOfWork() |
79 | | - messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow) |
| 84 | + bus = FakeBus() |
| 85 | + bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None)) |
80 | 86 |
|
81 | 87 | with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"): |
82 | | - messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow) |
| 88 | + bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10)) |
83 | 89 |
|
84 | 90 | @staticmethod |
85 | 91 | def test_commits(): |
86 | | - uow = FakeUnitOfWork() |
87 | | - messagebus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow) |
88 | | - messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow) |
89 | | - assert uow.committed |
| 92 | + bus = FakeBus() |
| 93 | + bus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None)) |
| 94 | + bus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10)) |
| 95 | + assert bus.uow.committed |
90 | 96 |
|
91 | 97 | @staticmethod |
92 | 98 | def test_sends_email_on_out_of_stock_error(): |
93 | | - uow = FakeUnitOfWork() |
94 | | - messagebus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow) |
95 | | - |
96 | | - with mock.patch("allocation.adapters.email.send") as mock_send_mail: |
97 | | - messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow) |
98 | | - assert mock_send_mail.call_args == mock.call( |
99 | | - "stock@made.com", |
100 | | - f"Out of stock for POPULAR-CURTAINS", |
101 | | - ) |
| 99 | + bus = FakeBus() |
| 100 | + bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None)) |
| 101 | + bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10)) |
| 102 | + assert bus.dependencies["send_mail"].call_args == mock.call( |
| 103 | + "stock@made.com", |
| 104 | + f"Out of stock for POPULAR-CURTAINS", |
| 105 | + ) |
102 | 106 |
|
103 | 107 |
|
104 | 108 | class TestChangeBatchQuantity: |
105 | 109 |
|
106 | 110 | @staticmethod |
107 | 111 | def test_changes_available_quantity(): |
108 | | - uow = FakeUnitOfWork() |
109 | | - messagebus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow) |
110 | | - [batch] = uow.products.get(sku="ADORABLE-SETTEE").batches |
| 112 | + bus = FakeBus() |
| 113 | + bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None)) |
| 114 | + [batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches |
111 | 115 | assert batch.available_quantity == 100 |
112 | 116 |
|
113 | | - messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow) |
114 | | - |
| 117 | + bus.handle(commands.ChangeBatchQuantity("batch1", 50)) |
115 | 118 | assert batch.available_quantity == 50 |
116 | 119 |
|
117 | 120 |
|
118 | 121 | @staticmethod |
119 | 122 | def test_reallocates_if_necessary(): |
120 | | - uow = FakeUnitOfWork() |
121 | | - messagebus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), uow) |
122 | | - messagebus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow) |
123 | | - messagebus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20), uow) |
124 | | - messagebus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20), uow) |
125 | | - [batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches |
| 123 | + bus = FakeBus() |
| 124 | + bus.handle(commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None)) |
| 125 | + bus.handle(commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today())) |
| 126 | + bus.handle(commands.Allocate("order1", "INDIFFERENT-TABLE", 20)) |
| 127 | + bus.handle(commands.Allocate("order2", "INDIFFERENT-TABLE", 20)) |
| 128 | + [batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches |
126 | 129 | assert batch1.available_quantity == 10 |
127 | 130 |
|
128 | | - messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow) |
| 131 | + bus.handle(commands.ChangeBatchQuantity("batch1", 25)) |
129 | 132 |
|
130 | 133 | # order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1 |
131 | 134 | assert batch1.available_quantity == 5 |
|
0 commit comments