|
| 1 | +from datetime import date |
1 | 2 | from unittest import mock |
2 | 3 | import pytest |
3 | 4 | from allocation import events, exceptions, messagebus, repository, unit_of_work |
@@ -29,7 +30,6 @@ def rollback(self): |
29 | 30 | pass |
30 | 31 |
|
31 | 32 |
|
32 | | - |
33 | 33 | class TestAddBatch: |
34 | 34 |
|
35 | 35 | @staticmethod |
@@ -84,3 +84,35 @@ def test_sends_email_on_out_of_stock_error(): |
84 | 84 | "stock@made.com", |
85 | 85 | f"Out of stock for POPULAR-CURTAINS", |
86 | 86 | ) |
| 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