Skip to content

Commit 0e8f68d

Browse files
committed
new test and put them into classes [test_change_batch_quantity_handler]
1 parent 160b081 commit 0e8f68d

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎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

@@ -32,7 +33,6 @@ def rollback(self):
3233
pass
3334

3435

35-
3636
class TestAddBatch:
3737

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

0 commit comments

Comments
 (0)