11from domain_model import Order , Stock
2+ from datetime import date , timedelta
3+
4+ today = date .today ()
5+ tomorrow = today + timedelta (days = 1 )
6+ later = tomorrow + timedelta (days = 10 )
27
38
49def test_can_allocate_to_stock ():
@@ -12,7 +17,7 @@ def test_can_allocate_to_stock():
1217
1318def test_can_allocate_to_shipment ():
1419 order = Order ({'a-sku' : 10 })
15- shipment = Stock ({'a-sku' : 1000 })
20+ shipment = Stock ({'a-sku' : 1000 }, eta = tomorrow )
1621
1722 order .allocate (stock = Stock ({}), shipments = [shipment ])
1823
@@ -22,7 +27,7 @@ def test_can_allocate_to_shipment():
2227def test_ignores_irrelevant_stock ():
2328 order = Order ({'sku1' : 10 })
2429 stock = Stock ({'sku2' : 1000 })
25- shipment = Stock ({'sku1' : 1000 })
30+ shipment = Stock ({'sku1' : 1000 }, eta = tomorrow )
2631
2732 order .allocate (stock = stock , shipments = [shipment ])
2833
@@ -32,8 +37,8 @@ def test_ignores_irrelevant_stock():
3237
3338def test_can_allocate_to_correct_shipment ():
3439 order = Order ({'sku2' : 10 })
35- shipment1 = Stock ({'sku1' : 1000 })
36- shipment2 = Stock ({'sku2' : 1000 })
40+ shipment1 = Stock ({'sku1' : 1000 }, eta = tomorrow )
41+ shipment2 = Stock ({'sku2' : 1000 }, eta = tomorrow )
3742
3843 order .allocate (stock = Stock ({}), shipments = [shipment1 , shipment2 ])
3944
@@ -43,7 +48,7 @@ def test_can_allocate_to_correct_shipment():
4348def test_allocates_to_stock_in_preference_to_shipment ():
4449 order = Order ({'sku1' : 10 })
4550 stock = Stock ({'sku1' : 1000 })
46- shipment = Stock ({'sku1' : 1000 })
51+ shipment = Stock ({'sku1' : 1000 }, eta = tomorrow )
4752
4853 order .allocate (stock , shipments = [shipment ])
4954
@@ -61,7 +66,7 @@ def test_can_allocate_multiple_lines_to_wh():
6166
6267def test_can_allocate_multiple_lines_to_shipment ():
6368 order = Order ({'sku1' : 5 , 'sku2' : 10 })
64- shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 })
69+ shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 }, eta = tomorrow )
6570
6671 order .allocate (stock = Stock ({}), shipments = [shipment ])
6772
@@ -71,7 +76,7 @@ def test_can_allocate_multiple_lines_to_shipment():
7176
7277def test_can_allocate_to_both ():
7378 order = Order ({'sku1' : 5 , 'sku2' : 10 })
74- shipment = Stock ({'sku2' : 1000 })
79+ shipment = Stock ({'sku2' : 1000 }, eta = tomorrow )
7580 stock = Stock ({'sku1' : 1000 })
7681
7782 order .allocate (stock , shipments = [shipment ])
@@ -82,7 +87,7 @@ def test_can_allocate_to_both():
8287
8388def test_can_allocate_to_both_preferring_stock ():
8489 order = Order ({'sku1' : 1 , 'sku2' : 2 , 'sku3' : 3 , 'sku4' : 4 })
85- shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 , 'sku3' : 1000 })
90+ shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 , 'sku3' : 1000 }, eta = tomorrow )
8691 stock = Stock ({'sku3' : 1000 , 'sku4' : 1000 })
8792
8893 order .allocate (stock , shipments = [shipment ])
@@ -93,10 +98,9 @@ def test_can_allocate_to_both_preferring_stock():
9398 assert order .allocation ['sku4' ] == stock
9499
95100
96-
97101def test_mixed_allocation_are_avoided_if_possible ():
98102 order = Order ({'sku1' : 10 , 'sku2' : 10 })
99- shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 })
103+ shipment = Stock ({'sku1' : 1000 , 'sku2' : 1000 }, eta = tomorrow )
100104 stock = Stock ({'sku1' : 1000 })
101105
102106 order .allocate (stock , shipments = [shipment ])
@@ -105,26 +109,26 @@ def test_mixed_allocation_are_avoided_if_possible():
105109 assert order .allocation ['sku2' ] == shipment
106110
107111
108- def test_allocated_to_first_suitable_shipment_in_list ():
112+ def test_allocated_to_earliest_suitable_shipment_in_list ():
109113 order = Order ({'sku1' : 10 , 'sku2' : 10 })
110- shipment1 = Stock ({'sku1' : 1000 , 'sku2' : 1000 })
111- shipment2 = Stock ({'sku1' : 1000 , 'sku2' : 1000 })
114+ shipment1 = Stock ({'sku1' : 1000 , 'sku2' : 1000 }, eta = today )
115+ shipment2 = Stock ({'sku1' : 1000 , 'sku2' : 1000 }, eta = tomorrow )
112116 stock = Stock ({})
113117
114- order .allocate (stock , shipments = [shipment1 , shipment2 ])
118+ order .allocate (stock , shipments = [shipment2 , shipment1 ])
115119
116120 assert order .allocation ['sku1' ] == shipment1
117121 assert order .allocation ['sku2' ] == shipment1
118122
119123
120- def test_still_preserves_ordering_if_split_across_shipments ():
124+ def test_still_chooses_earliest_if_split_across_shipments ():
121125 order = Order ({'sku1' : 10 , 'sku2' : 10 , 'sku3' : 10 })
122- shipment1 = Stock ({'sku1' : 1000 })
123- shipment2 = Stock ({'sku2' : 1000 , 'sku3' : 1000 })
124- shipment3 = Stock ({'sku2' : 1000 , 'sku3' : 1000 })
126+ shipment1 = Stock ({'sku1' : 1000 }, eta = today )
127+ shipment2 = Stock ({'sku2' : 1000 , 'sku3' : 1000 }, eta = tomorrow )
128+ shipment3 = Stock ({'sku2' : 1000 , 'sku3' : 1000 }, eta = later )
125129 stock = Stock ({})
126130
127- order .allocate (stock , shipments = [shipment1 , shipment2 , shipment3 ])
131+ order .allocate (stock , shipments = [shipment3 , shipment2 , shipment1 ])
128132
129133 assert order .allocation ['sku1' ] == shipment1
130134 assert order .allocation ['sku2' ] == shipment2
@@ -156,9 +160,7 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
156160
157161def test_cannot_allocate_if_insufficent_quantity_in_shipment ():
158162 order = Order ({'a-sku' : 10 })
159- shipment = Stock ({
160- 'a-sku' : 5 ,
161- })
163+ shipment = Stock ({'a-sku' : 5 }, eta = tomorrow )
162164
163165 order .allocate (stock = Stock ({}), shipments = [shipment ])
164166
0 commit comments