Skip to content

Commit eb08e7b

Browse files
committed
ORM for _allocations set on Batch
1 parent 2a29db0 commit eb08e7b

2 files changed

Lines changed: 84 additions & 14 deletions

File tree

‎orm.py‎

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

47
import model
58

@@ -8,20 +11,35 @@
811

912
order_lines = Table(
1013
'order_lines', metadata,
11-
Column('orderid', String(255), primary_key=True),
12-
Column('sku', String(255), primary_key=True),
13-
Column('qty', Integer),
14+
Column('id', Integer, primary_key=True, autoincrement=True),
15+
Column('sku', String(255)),
16+
Column('qty', Integer, nullable=False),
17+
Column('orderid', String(255)),
1418
)
1519

1620
batches = Table(
1721
'batches', metadata,
18-
Column('reference', String(255), primary_key=True),
19-
Column('sku', String(255), primary_key=True),
20-
Column('_purchased_qty', Integer),
21-
Column('eta', Date),
22+
Column('id', Integer, primary_key=True, autoincrement=True),
23+
Column('reference', String(255)),
24+
Column('sku', String(255)),
25+
Column('_purchased_quantity', Integer, nullable=False),
26+
Column('eta', Date, nullable=True),
27+
)
28+
29+
allocations = Table(
30+
'allocations', 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')),
2234
)
2335

2436

2537
def start_mappers():
26-
mapper(model.OrderLine, order_lines)
27-
mapper(model.Batch, batches)
38+
lines_mapper = mapper(model.OrderLine, order_lines)
39+
mapper(model.Batch, batches, properties={
40+
'_allocations': relationship(
41+
lines_mapper,
42+
secondary=allocations,
43+
collection_class=set,
44+
)
45+
})

‎test_orm.py‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,65 @@ def test_orderline_mapper_can_save_lines(session):
2626

2727

2828

29-
def test_batches(session):
30-
session.execute('INSERT INTO "batches" VALUES ("batch1", "sku1", 100, null)')
31-
session.execute('INSERT INTO "batches" VALUES ("batch2", "sku2", 200, "2011-04-11")')
29+
def test_retrieving_batches(session):
30+
session.execute(
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")'
37+
)
3238
expected = [
3339
model.Batch("batch1", "sku1", 100, eta=None),
3440
model.Batch("batch2", "sku2", 200, eta=date(2011, 4, 11)),
3541
]
3642

3743
assert session.query(model.Batch).all() == expected
3844

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

0 commit comments

Comments
 (0)