|
| 1 | +from datetime import date |
1 | 2 | from unittest import mock |
2 | 3 | import pytest |
3 | 4 |
|
@@ -32,7 +33,6 @@ def rollback(self): |
32 | 33 | pass |
33 | 34 |
|
34 | 35 |
|
35 | | - |
36 | 36 | class TestAddBatch: |
37 | 37 |
|
38 | 38 | @staticmethod |
@@ -87,3 +87,35 @@ def test_sends_email_on_out_of_stock_error(): |
87 | 87 | "stock@made.com", |
88 | 88 | f"Out of stock for POPULAR-CURTAINS", |
89 | 89 | ) |
| 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