Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8dae126

Browse files
committed
repository tests
1 parent fff68bd commit 8dae126

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

test_orm.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ def test_saving_batches(session):
4747
batch = model.Batch("batch1", "sku1", 100, eta=None)
4848
session.add(batch)
4949
session.commit()
50-
rows = list(
51-
session.execute(
52-
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
53-
)
50+
rows = session.execute(
51+
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
5452
)
55-
assert rows == [("batch1", "sku1", 100, None)]
53+
assert list(rows) == [("batch1", "sku1", 100, None)]
5654

5755

5856
def test_saving_allocations(session):

test_repository.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# pylint: disable=protected-access
2+
import model
3+
import repository
4+
5+
6+
def test_repository_can_save_a_batch(session):
7+
batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None)
8+
9+
repo = repository.SqlAlchemyRepository(session)
10+
repo.add(batch)
11+
session.commit()
12+
13+
rows = session.execute(
14+
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
15+
)
16+
assert list(rows) == [("batch1", "RUSTY-SOAPDISH", 100, None)]
17+
18+
19+
def insert_order_line(session):
20+
session.execute(
21+
"INSERT INTO order_lines (orderid, sku, qty)"
22+
' VALUES ("order1", "GENERIC-SOFA", 12)'
23+
)
24+
[[orderline_id]] = session.execute(
25+
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
26+
dict(orderid="order1", sku="GENERIC-SOFA"),
27+
)
28+
return orderline_id
29+
30+
31+
def insert_batch(session, batch_id):
32+
session.execute(
33+
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
34+
' VALUES (:batch_id, "GENERIC-SOFA", 100, null)',
35+
dict(batch_id=batch_id),
36+
)
37+
[[batch_id]] = session.execute(
38+
'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"',
39+
dict(batch_id=batch_id),
40+
)
41+
return batch_id
42+
43+
44+
def insert_allocation(session, orderline_id, batch_id):
45+
session.execute(
46+
"INSERT INTO allocations (orderline_id, batch_id)"
47+
" VALUES (:orderline_id, :batch_id)",
48+
dict(orderline_id=orderline_id, batch_id=batch_id),
49+
)
50+
51+
52+
def test_repository_can_retrieve_a_batch_with_allocations(session):
53+
orderline_id = insert_order_line(session)
54+
batch1_id = insert_batch(session, "batch1")
55+
insert_batch(session, "batch2")
56+
insert_allocation(session, orderline_id, batch1_id)
57+
58+
repo = repository.SqlAlchemyRepository(session)
59+
retrieved = repo.get("batch1")
60+
61+
expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)
62+
assert retrieved == expected # Batch.__eq__ only compares reference
63+
assert retrieved.sku == expected.sku
64+
assert retrieved._purchased_quantity == expected._purchased_quantity
65+
assert retrieved._allocations == {
66+
model.OrderLine("order1", "GENERIC-SOFA", 12),
67+
}

0 commit comments

Comments
 (0)