33
44
55from domain_model import (
6- OrderLine ,
6+ Order ,
77 Shipment ,
88 allocate ,
99)
@@ -16,43 +16,43 @@ def random_id():
1616
1717
1818def test_can_allocate_to_stock ():
19- order = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
19+ order = Order ({ 'a-sku' : 10 })
2020 stock = {'a-sku' : 1000 }
2121
2222 allocate (order , stock , shipments = [])
2323
24- assert order [ 0 ]. allocation == 'STOCK'
24+ assert order . allocations [ 'a-sku' ] == 'STOCK'
2525 assert stock ['a-sku' ] == 990
2626
2727
2828def test_can_allocate_to_shipment ():
29- order = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
29+ order = Order ({ 'a-sku' : 10 })
3030 shipment = Shipment (id = 'shipment-id' , eta = date .today (), lines = {
3131 'a-sku' : 1000
3232 })
3333
3434 allocate (order , stock = [], shipments = [shipment ])
3535
36- assert order [ 0 ]. allocation == shipment .id
36+ assert order . allocations [ 'a-sku' ] == shipment .id
3737 assert shipment ['a-sku' ] == 990
3838
3939
4040def test_ignores_irrelevant_stock ():
41- order = [ OrderLine ( sku = 'sku1' , quantity = 10 ), ]
41+ order = Order ({ 'sku1' : 10 })
4242 stock = {'sku2' : 1000 }
4343 shipment = Shipment (id = 'shipment-id' , eta = date .today (), lines = {
4444 'sku1' : 1000 ,
4545 })
4646
4747 allocate (order , stock = stock , shipments = [shipment ])
4848
49- assert order [ 0 ]. allocation == shipment .id
49+ assert order . allocations [ 'sku1' ] == shipment .id
5050 assert stock ['sku2' ] == 1000
5151 assert shipment ['sku1' ] == 990
5252
5353
5454def test_can_allocate_to_correct_shipment ():
55- order = [ OrderLine ( sku = 'sku2' , quantity = 10 )]
55+ order = Order ({ 'sku2' : 10 })
5656 shipment1 = Shipment ('shipment1' , eta = date .today (), lines = {
5757 'sku1' : 1000 ,
5858 })
@@ -62,82 +62,68 @@ def test_can_allocate_to_correct_shipment():
6262
6363 allocate (order , stock = [], shipments = [shipment1 , shipment2 ])
6464
65- assert order [ 0 ]. allocation == shipment2 .id
65+ assert order . allocations [ 'sku2' ] == shipment2 .id
6666 assert shipment1 ['sku1' ] == 1000
6767 assert shipment2 ['sku2' ] == 990
6868
6969
7070def test_allocates_to_stock_in_preference_to_shipment ():
71- order = [ OrderLine ( sku = 'sku1' , quantity = 10 )]
71+ order = Order ({ 'sku1' : 10 })
7272 stock = {'sku1' : 1000 }
7373 shipment = Shipment ('shipment1' , eta = date .today (), lines = {
7474 'sku1' : 1000 ,
7575 })
7676
7777 allocate (order , stock , shipments = [shipment ])
7878
79- assert order [ 0 ]. allocation == 'STOCK'
79+ assert order . allocations [ 'sku1' ] == 'STOCK'
8080 assert stock ['sku1' ] == 990
8181 assert shipment ['sku1' ] == 1000
8282
8383
8484def test_can_allocate_multiple_lines_to_wh ():
85- order = [
86- OrderLine (sku = 'sku1' , quantity = 5 ),
87- OrderLine (sku = 'sku2' , quantity = 10 ),
88- ]
85+ order = Order ({'sku1' : 5 , 'sku2' : 10 })
8986 stock = {'sku1' : 1000 , 'sku2' : 1000 }
9087
9188 allocate (order , stock , shipments = [])
92- assert order [ 0 ]. allocation == 'STOCK'
93- assert order [ 1 ]. allocation == 'STOCK'
89+ assert order . allocations [ 'sku1' ] == 'STOCK'
90+ assert order . allocations [ 'sku2' ] == 'STOCK'
9491 assert stock ['sku1' ] == 995
9592 assert stock ['sku2' ] == 990
9693
9794
9895def test_can_allocate_multiple_lines_to_shipment ():
99- order = [
100- OrderLine (sku = 'sku1' , quantity = 5 ),
101- OrderLine (sku = 'sku2' , quantity = 10 ),
102- ]
96+ order = Order ({'sku1' : 5 , 'sku2' : 10 })
10397 shipment = Shipment ('shipment1' , eta = date .today (), lines = {
10498 'sku1' : 1000 ,
10599 'sku2' : 1000 ,
106100 })
107101
108102 allocate (order , [], shipments = [shipment ])
109103
110- assert order [ 0 ]. allocation == shipment .id
111- assert order [ 1 ]. allocation == shipment .id
104+ assert order . allocations [ 'sku1' ] == shipment .id
105+ assert order . allocations [ 'sku2' ] == shipment .id
112106 assert shipment ['sku1' ] == 995
113107 assert shipment ['sku2' ] == 990
114108
115109
116110def test_can_allocate_to_both ():
117- order = [
118- OrderLine (sku = 'sku1' , quantity = 5 ),
119- OrderLine (sku = 'sku2' , quantity = 10 ),
120- ]
111+ order = Order ({'sku1' : 5 , 'sku2' : 10 })
121112 shipment = Shipment ('shipment1' , eta = date .today (), lines = {
122113 'sku2' : 1000 ,
123114 })
124115 stock = {'sku1' : 1000 }
125116
126117 allocate (order , stock , shipments = [shipment ])
127118
128- assert order [ 0 ]. allocation == 'STOCK'
129- assert order [ 1 ]. allocation == shipment .id
119+ assert order . allocations [ 'sku1' ] == 'STOCK'
120+ assert order . allocations [ 'sku2' ] == shipment .id
130121 assert stock ['sku1' ] == 995
131122 assert shipment ['sku2' ] == 990
132123
133124
134125def test_can_allocate_to_both_preferring_stock ():
135- order = [
136- OrderLine (sku = 'sku1' , quantity = 1 ),
137- OrderLine (sku = 'sku2' , quantity = 2 ),
138- OrderLine (sku = 'sku3' , quantity = 3 ),
139- OrderLine (sku = 'sku4' , quantity = 4 ),
140- ]
126+ order = Order ({'sku1' : 1 , 'sku2' : 2 , 'sku3' : 3 , 'sku4' : 4 })
141127 shipment = Shipment ('shipment1' , eta = date .today (), lines = {
142128 'sku1' : 1000 ,
143129 'sku2' : 1000 ,
@@ -147,10 +133,10 @@ def test_can_allocate_to_both_preferring_stock():
147133
148134 allocate (order , stock , shipments = [shipment ])
149135
150- assert order [ 0 ]. allocation == shipment .id
151- assert order [ 1 ]. allocation == shipment .id
152- assert order [ 2 ]. allocation == 'STOCK'
153- assert order [ 3 ]. allocation == 'STOCK'
136+ assert order . allocations [ 'sku1' ] == shipment .id
137+ assert order . allocations [ 'sku2' ] == shipment .id
138+ assert order . allocations [ 'sku3' ] == 'STOCK'
139+ assert order . allocations [ 'sku4' ] == 'STOCK'
154140 assert shipment ['sku1' ] == 999
155141 assert shipment ['sku2' ] == 998
156142 assert shipment ['sku3' ] == 1000
@@ -159,10 +145,7 @@ def test_can_allocate_to_both_preferring_stock():
159145
160146
161147def test_mixed_allocations_are_avoided_if_possible ():
162- order = [
163- OrderLine (sku = 'sku1' , quantity = 10 ),
164- OrderLine (sku = 'sku2' , quantity = 10 ),
165- ]
148+ order = Order ({'sku1' : 10 , 'sku2' : 10 })
166149 shipment = Shipment ('shipment1' , eta = date .today (), lines = {
167150 'sku1' : 1000 ,
168151 'sku2' : 1000 ,
@@ -171,15 +154,12 @@ def test_mixed_allocations_are_avoided_if_possible():
171154
172155 allocate (order , stock , shipments = [shipment ])
173156
174- assert order [ 0 ]. allocation == shipment .id
175- assert order [ 1 ]. allocation == shipment .id
157+ assert order . allocations [ 'sku1' ] == shipment .id
158+ assert order . allocations [ 'sku2' ] == shipment .id
176159
177160
178161def test_prefer_allocating_to_earlier_shipment ():
179- order = [
180- OrderLine (sku = 'sku1' , quantity = 10 ),
181- OrderLine (sku = 'sku2' , quantity = 10 ),
182- ]
162+ order = Order ({'sku1' : 10 , 'sku2' : 10 })
183163 shipment1 = Shipment ('shipment1' , eta = date .today (), lines = {
184164 'sku1' : 1000 ,
185165 'sku2' : 1000 ,
@@ -193,16 +173,12 @@ def test_prefer_allocating_to_earlier_shipment():
193173
194174 allocate (order , stock , shipments = [shipment2 , shipment1 ])
195175
196- assert order [ 0 ]. allocation == shipment1 .id
197- assert order [ 1 ]. allocation == shipment1 .id
176+ assert order . allocations [ 'sku1' ] == shipment1 .id
177+ assert order . allocations [ 'sku2' ] == shipment1 .id
198178
199179
200180def test_prefer_allocating_to_earlier_even_if_multiple_shipments ():
201- order = [
202- OrderLine (sku = 'sku1' , quantity = 10 ),
203- OrderLine (sku = 'sku2' , quantity = 10 ),
204- OrderLine (sku = 'sku3' , quantity = 10 ),
205- ]
181+ order = Order ({'sku1' : 10 , 'sku2' : 10 , 'sku3' : 10 })
206182 shipment1 = Shipment (id = 'shipment1' , eta = date .today (), lines = {
207183 'sku1' : 1000 ,
208184 })
@@ -220,37 +196,38 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
220196
221197 allocate (order , stock , shipments = [shipment3 , shipment2 , shipment1 ])
222198
223- assert order [1 ].allocation == shipment2 .id
224- assert order [2 ].allocation == shipment2 .id
199+ assert order .allocations ['sku1' ] == shipment1 .id
200+ assert order .allocations ['sku2' ] == shipment2 .id
201+ assert order .allocations ['sku3' ] == shipment2 .id
225202
226203
227204def test_cannot_allocate_if_insufficent_quantity_in_stock ():
228- order = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
205+ order = Order ({ 'a-sku' : 10 })
229206 stock = {'a-sku' : 5 }
230207
231208 allocate (order , stock , shipments = [])
232209
233- assert order [ 0 ]. allocation is None
210+ assert 'a-sku' not in order . allocations
234211
235212
236213def test_cannot_allocate_if_insufficent_quantity_in_shipment ():
237- order = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
214+ order = Order ({ 'a-sku' : 10 })
238215 shipment = Shipment (id = 'shipment-id' , eta = date .today (), lines = {
239216 'a-sku' : 5 ,
240217 })
241218
242219 allocate (order , stock = [], shipments = [shipment ])
243220
244- assert order [ 0 ]. allocation is None
221+ assert 'a-sku' not in order . allocations
245222
246223
247224def test_cannot_allocate_more_orders_than_we_have_stock_for ():
248- order1 = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
249- order2 = [ OrderLine ( sku = 'a-sku' , quantity = 10 )]
225+ order1 = Order ({ 'a-sku' : 10 })
226+ order2 = Order ({ 'a-sku' : 10 })
250227 stock = {'a-sku' : 15 }
251228
252229 allocate (order1 , stock , shipments = [])
253230 allocate (order2 , stock , shipments = [])
254231
255- assert order2 [ 0 ]. allocation is None
232+ assert 'a-sku' not in order2 . allocations
256233
0 commit comments