Skip to content

Commit fff68bd

Browse files
committed
ORM for _allocations set on Batch
1 parent 26b997e commit fff68bd

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

orm.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from sqlalchemy import Table, MetaData, Column, Integer, String, Date
2-
from sqlalchemy.orm import mapper
1+
from sqlalchemy import Table, MetaData, Column, Integer, String, Date, ForeignKey
2+
from sqlalchemy.orm import mapper, relationship
33

44
import model
55

@@ -9,21 +9,39 @@
99
order_lines = Table(
1010
"order_lines",
1111
metadata,
12-
Column("orderid", String(255), primary_key=True),
13-
Column("sku", String(255), primary_key=True),
14-
Column("qty", Integer),
12+
Column("id", Integer, primary_key=True, autoincrement=True),
13+
Column("sku", String(255)),
14+
Column("qty", Integer, nullable=False),
15+
Column("orderid", String(255)),
1516
)
1617

1718
batches = Table(
1819
"batches",
1920
metadata,
20-
Column("reference", String(255), primary_key=True),
21-
Column("sku", String(255), primary_key=True),
22-
Column("_purchased_qty", Integer),
23-
Column("eta", Date),
21+
Column("id", Integer, primary_key=True, autoincrement=True),
22+
Column("reference", String(255)),
23+
Column("sku", String(255)),
24+
Column("_purchased_quantity", Integer, nullable=False),
25+
Column("eta", Date, nullable=True),
26+
)
27+
28+
allocations = Table(
29+
"allocations",
30+
metadata,
31+
Column("id", Integer, primary_key=True, autoincrement=True),
32+
Column("orderline_id", ForeignKey("order_lines.id")),
33+
Column("batch_id", ForeignKey("batches.id")),
2434
)
2535

2636

2737
def start_mappers():
28-
mapper(model.OrderLine, order_lines)
29-
mapper(model.Batch, batches)
38+
lines_mapper = mapper(model.OrderLine, order_lines)
39+
mapper(
40+
model.Batch,
41+
batches,
42+
properties={
43+
"_allocations": relationship(
44+
lines_mapper, secondary=allocations, collection_class=set,
45+
)
46+
},
47+
)

test_orm.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,66 @@ def test_orderline_mapper_can_save_lines(session):
2626
assert rows == [("order1", "DECORATIVE-WIDGET", 12)]
2727

2828

29-
def test_batches(session):
30-
session.execute('INSERT INTO "batches" VALUES ("batch1", "sku1", 100, null)')
29+
def test_retrieving_batches(session):
3130
session.execute(
32-
'INSERT INTO "batches" VALUES ("batch2", "sku2", 200, "2011-04-11")'
31+
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
32+
' VALUES ("batch1", "sku1", 100, null)'
33+
)
34+
session.execute(
35+
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
36+
' VALUES ("batch2", "sku2", 200, "2011-04-11")'
3337
)
3438
expected = [
3539
model.Batch("batch1", "sku1", 100, eta=None),
3640
model.Batch("batch2", "sku2", 200, eta=date(2011, 4, 11)),
3741
]
3842

3943
assert session.query(model.Batch).all() == expected
44+
45+
46+
def test_saving_batches(session):
47+
batch = model.Batch("batch1", "sku1", 100, eta=None)
48+
session.add(batch)
49+
session.commit()
50+
rows = list(
51+
session.execute(
52+
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
53+
)
54+
)
55+
assert rows == [("batch1", "sku1", 100, None)]
56+
57+
58+
def test_saving_allocations(session):
59+
batch = model.Batch("batch1", "sku1", 100, eta=None)
60+
line = model.OrderLine("order1", "sku1", 10)
61+
batch.allocate(line)
62+
session.add(batch)
63+
session.commit()
64+
rows = list(session.execute('SELECT orderline_id, batch_id FROM "allocations"'))
65+
assert rows == [(batch.id, line.id)]
66+
67+
68+
def test_retrieving_allocations(session):
69+
session.execute(
70+
'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)'
71+
)
72+
[[olid]] = session.execute(
73+
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
74+
dict(orderid="order1", sku="sku1"),
75+
)
76+
session.execute(
77+
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
78+
' VALUES ("batch1", "sku1", 100, null)'
79+
)
80+
[[bid]] = session.execute(
81+
"SELECT id FROM batches WHERE reference=:ref AND sku=:sku",
82+
dict(ref="batch1", sku="sku1"),
83+
)
84+
session.execute(
85+
"INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)",
86+
dict(olid=olid, bid=bid),
87+
)
88+
89+
batch = session.query(model.Batch).one()
90+
91+
assert batch._allocations == {model.OrderLine("order1", "sku1", 12)}

0 commit comments

Comments
 (0)