Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax fix to fit sqlalchemy v2 #98

Open
wants to merge 1 commit into
base: chapter_02_repository
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions orm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy import Table, MetaData, Column, Integer, String, Date, ForeignKey
from sqlalchemy.orm import mapper, relationship
from sqlalchemy.orm import registry, relationship

import model

Expand Down Expand Up @@ -35,8 +35,9 @@


def start_mappers():
lines_mapper = mapper(model.OrderLine, order_lines)
mapper(
mapper_registry = registry()
lines_mapper = mapper_registry.map_imperatively(model.OrderLine, order_lines)
mapper_registry.map_imperatively(
model.Batch,
batches,
properties={
Expand Down
53 changes: 36 additions & 17 deletions test_orm.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import model
from datetime import date
from sqlalchemy import text


def test_orderline_mapper_can_load_lines(session):
session.execute(
"INSERT INTO order_lines (orderid, sku, qty) VALUES "
'("order1", "RED-CHAIR", 12),'
'("order1", "RED-TABLE", 13),'
'("order2", "BLUE-LIPSTICK", 14)'
text(
"INSERT INTO order_lines (orderid, sku, qty) VALUES "
'("order1", "RED-CHAIR", 12),'
'("order1", "RED-TABLE", 13),'
'("order2", "BLUE-LIPSTICK", 14)'
)
)
expected = [
model.OrderLine("order1", "RED-CHAIR", 12),
Expand All @@ -22,18 +25,22 @@ def test_orderline_mapper_can_save_lines(session):
session.add(new_line)
session.commit()

rows = list(session.execute('SELECT orderid, sku, qty FROM "order_lines"'))
rows = list(session.execute(text('SELECT orderid, sku, qty FROM "order_lines"')))
assert rows == [("order1", "DECORATIVE-WIDGET", 12)]


def test_retrieving_batches(session):
session.execute(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch1", "sku1", 100, null)'
text(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch1", "sku1", 100, null)'
)
)
session.execute(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch2", "sku2", 200, "2011-04-11")'
text(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch2", "sku2", 200, "2011-04-11")'
)
)
expected = [
model.Batch("batch1", "sku1", 100, eta=None),
Expand All @@ -48,7 +55,9 @@ def test_saving_batches(session):
session.add(batch)
session.commit()
rows = session.execute(
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
text(
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
)
)
assert list(rows) == [("batch1", "sku1", 100, None)]

Expand All @@ -59,28 +68,38 @@ def test_saving_allocations(session):
batch.allocate(line)
session.add(batch)
session.commit()
rows = list(session.execute('SELECT orderline_id, batch_id FROM "allocations"'))
rows = list(session.execute(text('SELECT orderline_id, batch_id FROM "allocations"')))
assert rows == [(line.id, batch.id)]


def test_retrieving_allocations(session):
session.execute(
'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)'
text(
'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)'
)
)
[[olid]] = session.execute(
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
text(
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
),
dict(orderid="order1", sku="sku1"),
)
session.execute(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch1", "sku1", 100, null)'
text(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES ("batch1", "sku1", 100, null)'
)
)
[[bid]] = session.execute(
"SELECT id FROM batches WHERE reference=:ref AND sku=:sku",
text(
"SELECT id FROM batches WHERE reference=:ref AND sku=:sku",
),
dict(ref="batch1", sku="sku1"),
)
session.execute(
"INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)",
text(
"INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)",
),
dict(olid=olid, bid=bid),
)

Expand Down
31 changes: 22 additions & 9 deletions test_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=protected-access
import model
import repository
from sqlalchemy import text


def test_repository_can_save_a_batch(session):
Expand All @@ -11,40 +12,52 @@ def test_repository_can_save_a_batch(session):
session.commit()

rows = session.execute(
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
text(
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
)
)
assert list(rows) == [("batch1", "RUSTY-SOAPDISH", 100, None)]


def insert_order_line(session):
session.execute(
"INSERT INTO order_lines (orderid, sku, qty)"
' VALUES ("order1", "GENERIC-SOFA", 12)'
text(
"INSERT INTO order_lines (orderid, sku, qty)"
' VALUES ("order1", "GENERIC-SOFA", 12)'
)
)
[[orderline_id]] = session.execute(
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
text(
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
),
dict(orderid="order1", sku="GENERIC-SOFA"),
)
return orderline_id


def insert_batch(session, batch_id):
session.execute(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES (:batch_id, "GENERIC-SOFA", 100, null)',
text(
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
' VALUES (:batch_id, "GENERIC-SOFA", 100, null)',
),
dict(batch_id=batch_id),
)
[[batch_id]] = session.execute(
'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"',
text(
'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"',
),
dict(batch_id=batch_id),
)
return batch_id


def insert_allocation(session, orderline_id, batch_id):
session.execute(
"INSERT INTO allocations (orderline_id, batch_id)"
" VALUES (:orderline_id, :batch_id)",
text(
"INSERT INTO allocations (orderline_id, batch_id)"
" VALUES (:orderline_id, :batch_id)",
),
dict(orderline_id=orderline_id, batch_id=batch_id),
)

Expand Down