@@ -46,6 +46,23 @@ def test_can_allocate_to_shipment():
4646 assert allocations [0 ].shipment_id == shipment .id
4747 assert allocations [0 ].quantity == 10
4848
49+ def test_ignores_invalid_stock ():
50+ sku1 , sku2 = random_id (), random_id ()
51+ order = Order (id = random_id (), lines = [
52+ OrderLine (sku = sku1 , quantity = 10 ),
53+ ])
54+ stock = Stock (sku = sku2 , quantity = 1000 )
55+ shipment = Shipment (id = random_id (), eta = date .today (), lines = [
56+ OrderLine (sku = sku1 , quantity = 1000 ),
57+ ])
58+
59+ allocations = allocate (order , stock = [stock ], shipments = [shipment ])
60+
61+ assert allocations [0 ].order_id == order .id
62+ assert allocations [0 ].sku == sku1
63+ assert allocations [0 ].shipment_id == shipment .id
64+ assert allocations [0 ].quantity == 10
65+
4966
5067def test_can_allocate_to_correct_shipment ():
5168 sku1 , sku2 = random_id (), random_id ()
@@ -135,4 +152,30 @@ def test_can_allocate_to_both():
135152 assert Allocation (order .id , sku2 , 10 , shipment_id = shipment .id ) in allocations
136153
137154
155+ def test_can_allocate_to_both_preferring_stock ():
156+ sku1 , sku2 , sku3 , sku4 = [random_id () for _ in range (4 )]
157+ order = Order (id = random_id (), lines = [
158+ OrderLine (sku = sku1 , quantity = 10 ),
159+ OrderLine (sku = sku2 , quantity = 10 ),
160+ OrderLine (sku = sku3 , quantity = 10 ),
161+ OrderLine (sku = sku4 , quantity = 10 ),
162+ ])
163+ shipment = Shipment (id = random_id (), eta = date .today (), lines = [
164+ OrderLine (sku = sku1 , quantity = 1000 ),
165+ OrderLine (sku = sku2 , quantity = 1000 ),
166+ OrderLine (sku = sku3 , quantity = 1000 ),
167+ ])
168+ stock = [
169+ Stock (sku = sku3 , quantity = 1000 ),
170+ Stock (sku = sku4 , quantity = 1000 ),
171+ ]
172+
173+ allocations = allocate (order , stock , shipments = [shipment ])
174+ assert Allocation (order .id , sku1 , 10 , shipment_id = shipment .id ) in allocations
175+ assert Allocation (order .id , sku2 , 10 , shipment_id = shipment .id ) in allocations
176+ assert Allocation (order .id , sku3 , 10 , shipment_id = None ) in allocations
177+ assert Allocation (order .id , sku4 , 10 , shipment_id = None ) in allocations
178+ assert Allocation (order .id , sku3 , 10 , shipment_id = shipment .id ) not in allocations
179+
180+
138181
0 commit comments