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