|
1 | | -from domain_model import allocate |
| 1 | +from domain_model import Order, Stock, Shipment |
| 2 | +from datetime import date, timedelta |
| 3 | + |
| 4 | +today = date.today() |
| 5 | +tomorrow = today + timedelta(days=1) |
| 6 | +later = tomorrow + timedelta(days=10) |
2 | 7 |
|
3 | 8 |
|
4 | 9 | def test_can_allocate_to_stock(): |
5 | | - order = {'a-sku': 10} |
6 | | - stock = {'a-sku': 1000} |
| 10 | + order = Order({'a-sku': 10}) |
| 11 | + stock = Stock({'a-sku': 1000}) |
7 | 12 |
|
8 | | - allocations = allocate(order, stock, shipments=[]) |
| 13 | + order.allocate(stock, shipments=[]) |
9 | 14 |
|
10 | | - assert allocations['a-sku'] == stock |
| 15 | + assert order.allocation['a-sku'] == stock |
| 16 | + assert stock['a-sku'] == 990 |
11 | 17 |
|
12 | 18 |
|
13 | 19 | def test_can_allocate_to_shipment(): |
14 | | - order = {'a-sku': 10} |
15 | | - shipment = {'a-sku': 1000} |
| 20 | + order = Order({'a-sku': 10}) |
| 21 | + shipment = Shipment({'a-sku': 1000}, eta=tomorrow) |
16 | 22 |
|
17 | | - allocations = allocate(order, stock={}, shipments=[shipment]) |
| 23 | + order.allocate(stock=Stock({}), shipments=[shipment]) |
18 | 24 |
|
19 | | - assert allocations['a-sku'] == shipment |
| 25 | + assert order.allocation['a-sku'] == shipment |
| 26 | + assert shipment['a-sku'] == 990 |
20 | 27 |
|
21 | 28 |
|
22 | 29 | def test_ignores_irrelevant_stock(): |
23 | | - order = {'sku1': 10} |
24 | | - stock = {'sku2': 1000} |
25 | | - shipment = {'sku1': 1000} |
| 30 | + order = Order({'sku1': 10}) |
| 31 | + stock = Stock({'sku2': 1000}) |
| 32 | + shipment = Shipment({'sku1': 1000}, eta=tomorrow) |
| 33 | + |
| 34 | + order.allocate(stock=stock, shipments=[shipment]) |
26 | 35 |
|
27 | | - allocations = allocate(order, stock=stock, shipments=[shipment]) |
| 36 | + assert order.allocation['sku1'] == shipment |
28 | 37 |
|
29 | | - assert allocations['sku1'] == shipment |
30 | 38 |
|
31 | 39 |
|
32 | 40 | def test_can_allocate_to_correct_shipment(): |
33 | | - order = {'sku2': 10} |
34 | | - shipment1 = {'sku1': 1000} |
35 | | - shipment2 = {'sku2': 1000} |
| 41 | + order = Order({'sku2': 10}) |
| 42 | + shipment1 = Shipment({'sku1': 1000}, eta=tomorrow) |
| 43 | + shipment2 = Shipment({'sku2': 1000}, eta=tomorrow) |
36 | 44 |
|
37 | | - allocations = allocate(order, stock={}, shipments=[shipment1, shipment2]) |
| 45 | + order.allocate(stock=Stock({}), shipments=[shipment1, shipment2]) |
38 | 46 |
|
39 | | - assert allocations['sku2'] == shipment2 |
| 47 | + assert order.allocation['sku2'] == shipment2 |
40 | 48 |
|
41 | 49 |
|
42 | 50 | def test_allocates_to_stock_in_preference_to_shipment(): |
43 | | - order = {'sku1': 10} |
44 | | - stock = {'sku1': 1000} |
45 | | - shipment = {'sku1': 1000} |
| 51 | + order = Order({'sku1': 10}) |
| 52 | + stock = Stock({'sku1': 1000}) |
| 53 | + shipment = Shipment({'sku1': 1000}, eta=tomorrow) |
46 | 54 |
|
47 | | - allocations = allocate(order, stock, shipments=[shipment]) |
| 55 | + order.allocate(stock, shipments=[shipment]) |
48 | 56 |
|
49 | | - assert allocations['sku1'] == stock |
| 57 | + assert order.allocation['sku1'] == stock |
| 58 | + assert stock['sku1'] == 990 |
| 59 | + assert shipment['sku1'] == 1000 |
50 | 60 |
|
51 | 61 |
|
52 | 62 | def test_can_allocate_multiple_lines_to_wh(): |
53 | | - order = {'sku1': 5, 'sku2': 10} |
54 | | - stock = {'sku1': 1000, 'sku2': 1000} |
| 63 | + order = Order({'sku1': 5, 'sku2': 10}) |
| 64 | + stock = Stock({'sku1': 1000, 'sku2': 1000}) |
55 | 65 |
|
56 | | - allocations = allocate(order, stock, shipments=[]) |
57 | | - assert allocations['sku1'] == stock |
58 | | - assert allocations['sku2'] == stock |
| 66 | + 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 |
59 | 71 |
|
60 | 72 |
|
61 | 73 | def test_can_allocate_multiple_lines_to_shipment(): |
62 | | - order = {'sku1': 5, 'sku2': 10} |
63 | | - shipment = {'sku1': 1000, 'sku2': 1000} |
| 74 | + order = Order({'sku1': 5, 'sku2': 10}) |
| 75 | + shipment = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow) |
64 | 76 |
|
65 | | - allocations = allocate(order, stock={}, shipments=[shipment]) |
| 77 | + order.allocate(stock=Stock({}), shipments=[shipment]) |
66 | 78 |
|
67 | | - assert allocations['sku1'] == shipment |
68 | | - assert allocations['sku2'] == shipment |
| 79 | + assert order.allocation['sku1'] == shipment |
| 80 | + assert order.allocation['sku2'] == shipment |
| 81 | + assert shipment['sku1'] == 995 |
| 82 | + assert shipment['sku2'] == 990 |
69 | 83 |
|
70 | 84 |
|
71 | 85 | def test_can_allocate_to_both(): |
72 | | - order = {'sku1': 5, 'sku2': 10} |
73 | | - shipment = {'sku2': 1000} |
74 | | - stock = {'sku1': 1000} |
| 86 | + order = Order({'sku1': 5, 'sku2': 10}) |
| 87 | + shipment = Shipment({'sku2': 1000}, eta=tomorrow) |
| 88 | + stock = Stock({'sku1': 1000}) |
75 | 89 |
|
76 | | - allocations = allocate(order, stock, shipments=[shipment]) |
| 90 | + order.allocate(stock, shipments=[shipment]) |
77 | 91 |
|
78 | | - assert allocations['sku1'] == stock |
79 | | - assert allocations['sku2'] == shipment |
| 92 | + assert order.allocation['sku1'] == stock |
| 93 | + assert order.allocation['sku2'] == shipment |
| 94 | + assert stock['sku1'] == 995 |
| 95 | + assert shipment['sku2'] == 990 |
80 | 96 |
|
81 | 97 |
|
82 | 98 | def test_can_allocate_to_both_preferring_stock(): |
83 | | - order = {'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4} |
84 | | - shipment = {'sku1': 1000, 'sku2': 1000, 'sku3': 1000} |
85 | | - stock = {'sku3': 1000, 'sku4': 1000} |
| 99 | + order = Order({'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4}) |
| 100 | + shipment = Shipment({'sku1': 1000, 'sku2': 1000, 'sku3': 1000}, eta=tomorrow) |
| 101 | + stock = Stock({'sku3': 1000, 'sku4': 1000}) |
86 | 102 |
|
87 | | - allocations = allocate(order, stock, shipments=[shipment]) |
| 103 | + order.allocate(stock, shipments=[shipment]) |
88 | 104 |
|
89 | | - assert allocations['sku1'] == shipment |
90 | | - assert allocations['sku2'] == shipment |
91 | | - assert allocations['sku3'] == stock |
92 | | - assert allocations['sku4'] == stock |
| 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 |
93 | 114 |
|
94 | 115 |
|
95 | | -def test_allocated_to_first_suitable_shipment_in_list(): |
96 | | - order = {'sku1': 10, 'sku2': 10} |
97 | | - shipment1 = {'sku1': 1000, 'sku2': 1000} |
98 | | - shipment2 = {'sku1': 1000, 'sku2': 1000} |
99 | | - stock = {} |
| 116 | +def test_allocated_to_earliest_suitable_shipment_in_list(): |
| 117 | + order = Order({'sku1': 10, 'sku2': 10}) |
| 118 | + shipment1 = Shipment({'sku1': 1000, 'sku2': 1000}, eta=today) |
| 119 | + shipment2 = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow) |
| 120 | + stock = Stock({}) |
100 | 121 |
|
101 | | - allocations = allocate(order, stock, shipments=[shipment1, shipment2]) |
| 122 | + order.allocate(stock, shipments=[shipment1, shipment2]) |
102 | 123 |
|
103 | | - assert allocations['sku1'] == shipment1 |
104 | | - assert allocations['sku2'] == shipment1 |
| 124 | + assert order.allocation['sku1'] == shipment1 |
| 125 | + assert order.allocation['sku2'] == shipment1 |
105 | 126 |
|
106 | 127 |
|
107 | | -def test_still_preserves_ordering_if_split_across_shipments(): |
108 | | - order = {'sku1': 10, 'sku2': 10, 'sku3': 10} |
109 | | - shipment1 = {'sku1': 1000} |
110 | | - shipment2 = {'sku2': 1000, 'sku3': 1000} |
111 | | - shipment3 = {'sku2': 1000, 'sku3': 1000} |
112 | | - stock = {} |
| 128 | +def test_still_chooses_earliest_if_split_across_shipments(): |
| 129 | + order = Order({'sku1': 10, 'sku2': 10, 'sku3': 10}) |
| 130 | + shipment1 = Shipment({'sku1': 1000}, eta=today) |
| 131 | + shipment2 = Shipment({'sku2': 1000, 'sku3': 1000}, eta=tomorrow) |
| 132 | + shipment3 = Shipment({'sku2': 1000, 'sku3': 1000}, eta=later) |
| 133 | + stock = Stock({}) |
113 | 134 |
|
114 | | - allocations = allocate(order, stock, shipments=[shipment1, shipment2, shipment3]) |
| 135 | + order.allocate(stock, shipments=[shipment2, shipment3, shipment1]) |
115 | 136 |
|
116 | | - assert allocations['sku1'] == shipment1 |
117 | | - assert allocations['sku2'] == shipment2 |
118 | | - assert allocations['sku3'] == shipment2 |
| 137 | + assert order.allocation['sku1'] == shipment1 |
| 138 | + assert order.allocation['sku2'] == shipment2 |
| 139 | + assert order.allocation['sku3'] == shipment2 |
119 | 140 |
|
120 | 141 |
|
121 | 142 | def test_stock_not_quite_enough_means_we_use_shipment(): |
122 | | - order = {'sku1': 10, 'sku2': 10} |
123 | | - stock = {'sku1': 10, 'sku2': 5} |
124 | | - shipment = { |
| 143 | + order = Order({'sku1': 10, 'sku2': 10}) |
| 144 | + stock = Stock({'sku1': 10, 'sku2': 5}) |
| 145 | + shipment = Shipment({ |
125 | 146 | 'sku1': 1000, |
126 | 147 | 'sku2': 1000, |
127 | | - } |
| 148 | + }, eta=tomorrow) |
128 | 149 |
|
129 | | - allocations = allocate(order, stock, shipments=[shipment]) |
| 150 | + order.allocate(stock, shipments=[shipment]) |
130 | 151 |
|
131 | | - assert allocations['sku1'] == shipment |
132 | | - assert allocations['sku2'] == shipment |
| 152 | + assert order.allocation['sku1'] == shipment |
| 153 | + assert order.allocation['sku2'] == shipment |
133 | 154 |
|
134 | 155 |
|
135 | 156 | def test_cannot_allocate_if_insufficent_quantity_in_stock(): |
136 | | - order = {'a-sku': 10} |
137 | | - stock = {'a-sku': 5} |
| 157 | + order = Order({'a-sku': 10}) |
| 158 | + stock = Stock({'a-sku': 5}) |
138 | 159 |
|
139 | | - allocations = allocate(order, stock, shipments=[]) |
| 160 | + order.allocate(stock, shipments=[]) |
140 | 161 |
|
141 | | - assert 'a-sku' not in allocations |
| 162 | + assert 'a-sku' not in order.allocation |
142 | 163 |
|
143 | 164 |
|
144 | 165 | def test_cannot_allocate_if_insufficent_quantity_in_shipment(): |
145 | | - order = {'a-sku': 10} |
146 | | - shipment = { |
147 | | - 'a-sku': 5, |
148 | | - } |
| 166 | + order = Order({'a-sku': 10}) |
| 167 | + shipment = Shipment({'a-sku': 5}, eta=tomorrow) |
149 | 168 |
|
150 | | - allocations = allocate(order, stock={}, shipments=[shipment]) |
| 169 | + order.allocate(stock=Stock({}), shipments=[shipment]) |
151 | 170 |
|
152 | | - assert 'a-sku' not in allocations |
| 171 | + assert 'a-sku' not in order.allocation |
153 | 172 |
|
0 commit comments