1- from domain_model import Order , Stock , Shipment
1+ import domain_model
22from datetime import date , timedelta
33
44today = date .today ()
55tomorrow = today + timedelta (days = 1 )
66later = tomorrow + timedelta (days = 10 )
77
8+ Order = lambda d : domain_model .Order (lines = [domain_model .OrderLine (sku , qty ) for sku , qty in d .items ()])
9+ Stock = lambda d : domain_model .Stock (lines = [domain_model .StockLine (sku , qty ) for sku , qty in d .items ()])
10+ Shipment = lambda d , eta : domain_model .Shipment (
11+ lines = [domain_model .StockLine (sku , qty ) for sku , qty in d .items ()],
12+ eta = eta ,
13+ )
14+
815
916def test_can_allocate_to_stock ():
1017 order = Order ({'a-sku' : 10 })
1118 stock = Stock ({'a-sku' : 1000 })
1219
1320 order .allocate (stock , shipments = [])
1421
15- assert order .allocation ['a-sku' ] == stock
16- assert stock ['a-sku' ] == 990
22+ assert order .allocation . sources ['a-sku' ] == stock
23+ assert stock . quantities ['a-sku' ] == 990
1724
1825
1926def test_can_allocate_to_shipment ():
@@ -22,8 +29,8 @@ def test_can_allocate_to_shipment():
2229
2330 order .allocate (stock = Stock ({}), shipments = [shipment ])
2431
25- assert order .allocation ['a-sku' ] == shipment
26- assert shipment ['a-sku' ] == 990
32+ assert order .allocation . sources ['a-sku' ] == shipment
33+ assert shipment . quantities ['a-sku' ] == 990
2734
2835
2936def test_ignores_irrelevant_stock ():
@@ -33,7 +40,7 @@ def test_ignores_irrelevant_stock():
3340
3441 order .allocate (stock = stock , shipments = [shipment ])
3542
36- assert order .allocation ['sku1' ] == shipment
43+ assert order .allocation . sources ['sku1' ] == shipment
3744
3845
3946
@@ -44,7 +51,7 @@ def test_can_allocate_to_correct_shipment():
4451
4552 order .allocate (stock = Stock ({}), shipments = [shipment1 , shipment2 ])
4653
47- assert order .allocation ['sku2' ] == shipment2
54+ assert order .allocation . sources ['sku2' ] == shipment2
4855
4956
5057def test_allocates_to_stock_in_preference_to_shipment ():
@@ -54,20 +61,20 @@ def test_allocates_to_stock_in_preference_to_shipment():
5461
5562 order .allocate (stock , shipments = [shipment ])
5663
57- assert order .allocation ['sku1' ] == stock
58- assert stock ['sku1' ] == 990
59- assert shipment ['sku1' ] == 1000
64+ assert order .allocation . sources ['sku1' ] == stock
65+ assert stock . quantities ['sku1' ] == 990
66+ assert shipment . quantities ['sku1' ] == 1000
6067
6168
6269def test_can_allocate_multiple_lines_to_wh ():
6370 order = Order ({'sku1' : 5 , 'sku2' : 10 })
6471 stock = Stock ({'sku1' : 1000 , 'sku2' : 1000 })
6572
6673 order .allocate (stock , shipments = [])
67- assert order .allocation ['sku1' ] == stock
68- assert order .allocation ['sku2' ] == stock
69- assert stock ['sku1' ] == 995
70- assert stock ['sku2' ] == 990
74+ assert order .allocation . sources ['sku1' ] == stock
75+ assert order .allocation . sources ['sku2' ] == stock
76+ assert stock . quantities ['sku1' ] == 995
77+ assert stock . quantities ['sku2' ] == 990
7178
7279
7380def test_can_allocate_multiple_lines_to_shipment ():
@@ -76,10 +83,10 @@ def test_can_allocate_multiple_lines_to_shipment():
7683
7784 order .allocate (stock = Stock ({}), shipments = [shipment ])
7885
79- assert order .allocation ['sku1' ] == shipment
80- assert order .allocation ['sku2' ] == shipment
81- assert shipment ['sku1' ] == 995
82- assert shipment ['sku2' ] == 990
86+ assert order .allocation . sources ['sku1' ] == shipment
87+ assert order .allocation . sources ['sku2' ] == shipment
88+ assert shipment . quantities ['sku1' ] == 995
89+ assert shipment . quantities ['sku2' ] == 990
8390
8491
8592def test_can_allocate_to_both ():
@@ -89,10 +96,10 @@ def test_can_allocate_to_both():
8996
9097 order .allocate (stock , shipments = [shipment ])
9198
92- assert order .allocation ['sku1' ] == stock
93- assert order .allocation ['sku2' ] == shipment
94- assert stock ['sku1' ] == 995
95- assert shipment ['sku2' ] == 990
99+ assert order .allocation . sources ['sku1' ] == stock
100+ assert order .allocation . sources ['sku2' ] == shipment
101+ assert stock . quantities ['sku1' ] == 995
102+ assert shipment . quantities ['sku2' ] == 990
96103
97104
98105def test_can_allocate_to_both_preferring_stock ():
@@ -102,15 +109,15 @@ def test_can_allocate_to_both_preferring_stock():
102109
103110 order .allocate (stock , shipments = [shipment ])
104111
105- assert order .allocation ['sku1' ] == shipment
106- assert order .allocation ['sku2' ] == shipment
107- assert order .allocation ['sku3' ] == stock
108- assert order .allocation ['sku4' ] == stock
109- assert shipment ['sku1' ] == 999
110- assert shipment ['sku2' ] == 998
111- assert shipment ['sku3' ] == 1000
112- assert stock ['sku3' ] == 997
113- assert stock ['sku4' ] == 996
112+ assert order .allocation . sources ['sku1' ] == shipment
113+ assert order .allocation . sources ['sku2' ] == shipment
114+ assert order .allocation . sources ['sku3' ] == stock
115+ assert order .allocation . sources ['sku4' ] == stock
116+ assert shipment . quantities ['sku1' ] == 999
117+ assert shipment . quantities ['sku2' ] == 998
118+ assert shipment . quantities ['sku3' ] == 1000
119+ assert stock . quantities ['sku3' ] == 997
120+ assert stock . quantities ['sku4' ] == 996
114121
115122
116123def test_mixed_allocation_are_avoided_if_possible ():
@@ -120,8 +127,8 @@ def test_mixed_allocation_are_avoided_if_possible():
120127
121128 order .allocate (stock , shipments = [shipment ])
122129
123- assert order .allocation ['sku1' ] == shipment
124- assert order .allocation ['sku2' ] == shipment
130+ assert order .allocation . sources ['sku1' ] == shipment
131+ assert order .allocation . sources ['sku2' ] == shipment
125132
126133
127134def test_allocated_to_earliest_suitable_shipment_in_list ():
@@ -132,8 +139,8 @@ def test_allocated_to_earliest_suitable_shipment_in_list():
132139
133140 order .allocate (stock , shipments = [shipment2 , shipment1 ])
134141
135- assert order .allocation ['sku1' ] == shipment1
136- assert order .allocation ['sku2' ] == shipment1
142+ assert order .allocation . sources ['sku1' ] == shipment1
143+ assert order .allocation . sources ['sku2' ] == shipment1
137144
138145
139146def test_still_chooses_earliest_if_split_across_shipments ():
@@ -145,9 +152,9 @@ def test_still_chooses_earliest_if_split_across_shipments():
145152
146153 order .allocate (stock , shipments = [shipment3 , shipment2 , shipment1 ])
147154
148- assert order .allocation ['sku1' ] == shipment1
149- assert order .allocation ['sku2' ] == shipment2
150- assert order .allocation ['sku3' ] == shipment2
155+ assert order .allocation . sources ['sku1' ] == shipment1
156+ assert order .allocation . sources ['sku2' ] == shipment2
157+ assert order .allocation . sources ['sku3' ] == shipment2
151158
152159
153160def test_stock_not_quite_enough_means_we_use_shipment ():
@@ -160,8 +167,8 @@ def test_stock_not_quite_enough_means_we_use_shipment():
160167
161168 order .allocate (stock , shipments = [shipment ])
162169
163- assert order .allocation ['sku1' ] == shipment
164- assert order .allocation ['sku2' ] == shipment
170+ assert order .allocation . sources ['sku1' ] == shipment
171+ assert order .allocation . sources ['sku2' ] == shipment
165172
166173
167174def test_cannot_allocate_if_insufficent_quantity_in_stock ():
@@ -170,7 +177,7 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
170177
171178 order .allocate (stock , shipments = [])
172179
173- assert 'a-sku' not in order .allocation
180+ assert 'a-sku' not in order .allocation . skus
174181
175182
176183def test_cannot_allocate_if_insufficent_quantity_in_shipment ():
@@ -179,5 +186,5 @@ def test_cannot_allocate_if_insufficent_quantity_in_shipment():
179186
180187 order .allocate (stock = Stock ({}), shipments = [shipment ])
181188
182- assert 'a-sku' not in order .allocation
189+ assert 'a-sku' not in order .allocation . skus
183190
0 commit comments