|
1 | 1 | import uuid |
2 | | -from datetime import date |
| 2 | +from datetime import date, timedelta |
3 | 3 |
|
4 | 4 |
|
5 | 5 | from domain_model import ( |
@@ -172,3 +172,54 @@ def test_mixed_allocations_are_avoided_if_possible(): |
172 | 172 | assert order[0].allocation == shipment.id |
173 | 173 | assert order[1].allocation == shipment.id |
174 | 174 |
|
| 175 | + |
| 176 | +def test_prefer_allocating_to_earlier_shipment(): |
| 177 | + sku1, sku2 = random_id(), random_id() |
| 178 | + order = [ |
| 179 | + OrderLine(sku=sku1, quantity=10), |
| 180 | + OrderLine(sku=sku2, quantity=10), |
| 181 | + ] |
| 182 | + shipment1 = Shipment(id=random_id(), eta=date.today(), lines=[ |
| 183 | + Line(sku=sku1, quantity=1000), |
| 184 | + Line(sku=sku2, quantity=1000), |
| 185 | + ]) |
| 186 | + tomorrow = date.today() + timedelta(days=1) |
| 187 | + shipment2 = Shipment(id=random_id(), eta=tomorrow, lines=[ |
| 188 | + Line(sku=sku1, quantity=1000), |
| 189 | + Line(sku=sku2, quantity=1000), |
| 190 | + ]) |
| 191 | + stock = [] |
| 192 | + |
| 193 | + allocate(order, stock, shipments=[shipment2, shipment1]) |
| 194 | + |
| 195 | + assert order[0].allocation == shipment1.id |
| 196 | + assert order[1].allocation == shipment1.id |
| 197 | + |
| 198 | + |
| 199 | +def test_prefer_allocating_to_earlier_even_if_multiple_shipments(): |
| 200 | + sku1, sku2, sku3 = random_id(), random_id(), random_id() |
| 201 | + order = [ |
| 202 | + OrderLine(sku=sku1, quantity=10), |
| 203 | + OrderLine(sku=sku2, quantity=10), |
| 204 | + OrderLine(sku=sku3, quantity=10), |
| 205 | + ] |
| 206 | + shipment1 = Shipment(id=random_id(), eta=date.today(), lines=[ |
| 207 | + Line(sku=sku1, quantity=1000), |
| 208 | + ]) |
| 209 | + tomorrow = date.today() + timedelta(days=1) |
| 210 | + shipment2 = Shipment(id=random_id(), eta=tomorrow, lines=[ |
| 211 | + Line(sku=sku2, quantity=1000), |
| 212 | + Line(sku=sku3, quantity=1000), |
| 213 | + ]) |
| 214 | + later = tomorrow + timedelta(days=1) |
| 215 | + shipment3 = Shipment(id=random_id(), eta=later, lines=[ |
| 216 | + Line(sku=sku2, quantity=1000), |
| 217 | + Line(sku=sku3, quantity=1000), |
| 218 | + ]) |
| 219 | + stock = [] |
| 220 | + |
| 221 | + allocate(order, stock, shipments=[shipment3, shipment2, shipment1]) |
| 222 | + |
| 223 | + assert order[1].allocation == shipment2.id |
| 224 | + assert order[2].allocation == shipment2.id |
| 225 | + |
0 commit comments