|
1 | 1 | # pylint: disable=no-self-use |
| 2 | +from datetime import date |
2 | 3 | from unittest import mock |
3 | 4 | import pytest |
4 | 5 |
|
@@ -84,3 +85,38 @@ def test_sends_email_on_out_of_stock_error(self): |
84 | 85 | assert mock_send_mail.call_args == mock.call( |
85 | 86 | "stock@made.com", f"Out of stock for POPULAR-CURTAINS" |
86 | 87 | ) |
| 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