Skip to content

Commit e6f7384

Browse files
committed
get rid of order and make order lines different from shipment lines
1 parent 7446e17 commit e6f7384

2 files changed

Lines changed: 50 additions & 52 deletions

File tree

‎domain_model.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
from datetime import date
33

44
@dataclass
5-
class Order:
6-
id: str
7-
lines: list
5+
class Line:
6+
sku: str
7+
quantity: int
88

99

1010
@dataclass
11-
class OrderLine:
12-
sku: str
13-
quantity: int
11+
class OrderLine(Line):
1412
allocation: str = None
1513

1614

@@ -39,7 +37,7 @@ def allocate_to_shipments(line, shipments):
3937
line.allocation = shipment.id
4038

4139
def allocate(order, stock, shipments):
42-
for line in order.lines:
40+
for line in order:
4341
allocate_to_stock(line, stock)
4442
if not line.allocation:
4543
allocate_to_shipments(line, shipments)

‎test_allocation.py‎

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
from domain_model import (
6-
Order,
6+
Line,
77
OrderLine,
88
Shipment,
99
Stock,
@@ -19,141 +19,141 @@ def random_id():
1919

2020
def test_can_allocate_to_warehouse_stock():
2121
sku = random_id()
22-
order = Order(id=random_id(), lines=[
22+
order = [
2323
OrderLine(sku=sku, quantity=10),
24-
])
24+
]
2525
stock = Stock(sku=sku, quantity=1000)
2626

2727
allocate(order, [stock], shipments=[])
2828

29-
assert order.lines[0].allocation == 'warehouse'
29+
assert order[0].allocation == 'warehouse'
3030

3131

3232
def test_can_allocate_to_shipment():
3333
sku = random_id()
34-
order = Order(id=random_id(), lines=[
34+
order = [
3535
OrderLine(sku=sku, quantity=10),
36-
])
36+
]
3737
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
38-
OrderLine(sku=sku, quantity=1000),
38+
Line(sku=sku, quantity=1000),
3939
])
4040

4141
allocate(order, stock=[], shipments=[shipment])
4242

43-
assert order.lines[0].allocation == shipment.id
43+
assert order[0].allocation == shipment.id
4444

4545

4646
def test_ignores_invalid_stock():
4747
sku1, sku2 = random_id(), random_id()
48-
order = Order(id=random_id(), lines=[
48+
order = [
4949
OrderLine(sku=sku1, quantity=10),
50-
])
50+
]
5151
stock = Stock(sku=sku2, quantity=1000)
5252
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
53-
OrderLine(sku=sku1, quantity=1000),
53+
Line(sku=sku1, quantity=1000),
5454
])
5555

5656
allocate(order, stock=[stock], shipments=[shipment])
5757

58-
assert order.lines[0].allocation == shipment.id
58+
assert order[0].allocation == shipment.id
5959

6060

6161
def test_can_allocate_to_correct_shipment():
6262
sku1, sku2 = random_id(), random_id()
63-
order = Order(id=random_id(), lines=[
63+
order = [
6464
OrderLine(sku=sku2, quantity=10),
65-
])
65+
]
6666
shipment1 = Shipment(id=random_id(), eta=date.today(), lines=[
67-
OrderLine(sku=sku1, quantity=1000),
67+
Line(sku=sku1, quantity=1000),
6868
])
6969
shipment2 = Shipment(id=random_id(), eta=date.today(), lines=[
70-
OrderLine(sku=sku2, quantity=1000),
70+
Line(sku=sku2, quantity=1000),
7171
])
7272

7373
allocate(order, stock=[], shipments=[shipment1, shipment2])
7474

75-
assert order.lines[0].allocation == shipment2.id
75+
assert order[0].allocation == shipment2.id
7676

7777

7878
def test_allocates_to_warehouse_stock_in_preference_to_shipment():
7979
sku = random_id()
80-
order = Order(id=random_id(), lines=[
80+
order = [
8181
OrderLine(sku=sku, quantity=10),
82-
])
82+
]
8383
stock = Stock(sku=sku, quantity=1000)
8484
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
85-
OrderLine(sku=sku, quantity=1000),
85+
Line(sku=sku, quantity=1000),
8686
])
8787

8888
allocate(order, [stock], shipments=[shipment])
8989

90-
assert order.lines[0].allocation == 'warehouse'
90+
assert order[0].allocation == 'warehouse'
9191

9292

9393
def test_can_allocate_multiple_lines_to_wh():
9494
sku1, sku2 = random_id(), random_id()
95-
order = Order(id=random_id(), lines=[
95+
order = [
9696
OrderLine(sku=sku1, quantity=10),
9797
OrderLine(sku=sku2, quantity=10),
98-
])
98+
]
9999
stock = [
100100
Stock(sku=sku1, quantity=1000),
101101
Stock(sku=sku2, quantity=1000),
102102
]
103103

104104
allocate(order, stock, shipments=[])
105-
assert order.lines[0].allocation == 'warehouse'
106-
assert order.lines[1].allocation == 'warehouse'
105+
assert order[0].allocation == 'warehouse'
106+
assert order[1].allocation == 'warehouse'
107107

108108

109109
def test_can_allocate_multiple_lines_to_shipment():
110110
sku1, sku2 = random_id(), random_id()
111-
order = Order(id=random_id(), lines=[
111+
order = [
112112
OrderLine(sku=sku1, quantity=10),
113113
OrderLine(sku=sku2, quantity=10),
114-
])
114+
]
115115
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
116-
OrderLine(sku=sku1, quantity=1000),
117-
OrderLine(sku=sku2, quantity=1000),
116+
Line(sku=sku1, quantity=1000),
117+
Line(sku=sku2, quantity=1000),
118118
])
119119

120120
allocate(order, [], shipments=[shipment])
121121

122-
assert order.lines[0].allocation == shipment.id
123-
assert order.lines[1].allocation == shipment.id
122+
assert order[0].allocation == shipment.id
123+
assert order[1].allocation == shipment.id
124124

125125

126126
def test_can_allocate_to_both():
127127
sku1, sku2 = random_id(), random_id()
128-
order = Order(id=random_id(), lines=[
128+
order = [
129129
OrderLine(sku=sku1, quantity=10),
130130
OrderLine(sku=sku2, quantity=10),
131-
])
131+
]
132132
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
133-
OrderLine(sku=sku2, quantity=1000),
133+
Line(sku=sku2, quantity=1000),
134134
])
135135
stock = [
136136
Stock(sku=sku1, quantity=1000),
137137
]
138138

139139
allocate(order, stock, shipments=[shipment])
140140

141-
assert order.lines[0].allocation == 'warehouse'
142-
assert order.lines[1].allocation == shipment.id
141+
assert order[0].allocation == 'warehouse'
142+
assert order[1].allocation == shipment.id
143143

144144

145145
def test_can_allocate_to_both_preferring_stock():
146146
sku1, sku2, sku3, sku4 = [random_id() for _ in range(4)]
147-
order = Order(id=random_id(), lines=[
147+
order = [
148148
OrderLine(sku=sku1, quantity=10),
149149
OrderLine(sku=sku2, quantity=10),
150150
OrderLine(sku=sku3, quantity=10),
151151
OrderLine(sku=sku4, quantity=10),
152-
])
152+
]
153153
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
154-
OrderLine(sku=sku1, quantity=1000),
155-
OrderLine(sku=sku2, quantity=1000),
156-
OrderLine(sku=sku3, quantity=1000),
154+
Line(sku=sku1, quantity=1000),
155+
Line(sku=sku2, quantity=1000),
156+
Line(sku=sku3, quantity=1000),
157157
])
158158
stock = [
159159
Stock(sku=sku3, quantity=1000),
@@ -162,8 +162,8 @@ def test_can_allocate_to_both_preferring_stock():
162162

163163
allocate(order, stock, shipments=[shipment])
164164

165-
assert order.lines[0].allocation == shipment.id
166-
assert order.lines[1].allocation == shipment.id
167-
assert order.lines[2].allocation == 'warehouse'
168-
assert order.lines[3].allocation == 'warehouse'
165+
assert order[0].allocation == shipment.id
166+
assert order[1].allocation == shipment.id
167+
assert order[2].allocation == 'warehouse'
168+
assert order[3].allocation == 'warehouse'
169169

0 commit comments

Comments
 (0)