Skip to content

Commit d13475b

Browse files
committed
new test and put them into classes [test_change_batch_quantity_handler]
1 parent e4e48bd commit d13475b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/unit/test_handlers.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=no-self-use
2+
from datetime import date
23
from unittest import mock
34
import pytest
45

@@ -84,3 +85,38 @@ def test_sends_email_on_out_of_stock_error(self):
8485
assert mock_send_mail.call_args == mock.call(
8586
"stock@made.com", f"Out of stock for POPULAR-CURTAINS"
8687
)
88+
89+
90+
class TestChangeBatchQuantity:
91+
def test_changes_available_quantity(self):
92+
uow = FakeUnitOfWork()
93+
messagebus.handle(
94+
events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow
95+
)
96+
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
97+
assert batch.available_quantity == 100
98+
99+
messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow)
100+
101+
assert batch.available_quantity == 50
102+
103+
def test_reallocates_if_necessary(self):
104+
uow = FakeUnitOfWork()
105+
event_history = [
106+
events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None),
107+
events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()),
108+
events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20),
109+
events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20),
110+
]
111+
for e in event_history:
112+
messagebus.handle(e, uow)
113+
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
114+
assert batch1.available_quantity == 10
115+
assert batch2.available_quantity == 50
116+
117+
messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow)
118+
119+
# order1 or order2 will be deallocated, so we'll have 25 - 20
120+
assert batch1.available_quantity == 5
121+
# and 20 will be reallocated to the next batch
122+
assert batch2.available_quantity == 30

0 commit comments

Comments
 (0)