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