Skip to content

Commit f4b7851

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

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

tests/unit/test_handlers.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import date
12
from unittest import mock
23
import pytest
34
from allocation import events, exceptions, messagebus, repository, unit_of_work
@@ -29,7 +30,6 @@ def rollback(self):
2930
pass
3031

3132

32-
3333
class TestAddBatch:
3434

3535
@staticmethod
@@ -84,3 +84,35 @@ def test_sends_email_on_out_of_stock_error():
8484
"stock@made.com",
8585
f"Out of stock for POPULAR-CURTAINS",
8686
)
87+
88+
89+
class TestChangeBatchQuantity:
90+
91+
@staticmethod
92+
def test_changes_available_quantity():
93+
uow = FakeUnitOfWork()
94+
messagebus.handle(events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow)
95+
[batch] = uow.products.get(sku="ADORABLE-SETTEE").batches
96+
assert batch.available_quantity == 100
97+
98+
messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow)
99+
100+
assert batch.available_quantity == 50
101+
102+
103+
@staticmethod
104+
def test_reallocates_if_necessary():
105+
uow = FakeUnitOfWork()
106+
messagebus.handle(events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None), uow)
107+
messagebus.handle(events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()), uow)
108+
messagebus.handle(events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20), uow)
109+
messagebus.handle(events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20), uow)
110+
[batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches
111+
assert batch1.available_quantity == 10
112+
113+
messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow)
114+
115+
# order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1
116+
assert batch1.available_quantity == 5
117+
# and 20 will be reallocated to the next batch
118+
assert batch2.available_quantity == 30

0 commit comments

Comments
 (0)