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