From 660e4d094aa14abda1fef137de587c47cac3a1d5 Mon Sep 17 00:00:00 2001 From: harryghgim Date: Sun, 22 Dec 2024 17:49:00 +0900 Subject: [PATCH] syntax fix to fit sqlalchemy v2 --- orm.py | 7 +++--- test_orm.py | 53 +++++++++++++++++++++++++++++++--------------- test_repository.py | 31 +++++++++++++++++++-------- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/orm.py b/orm.py index 3e6b4a70..12282c20 100644 --- a/orm.py +++ b/orm.py @@ -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 @@ -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={ diff --git a/test_orm.py b/test_orm.py index b1c6e3db..808eb723 100644 --- a/test_orm.py +++ b/test_orm.py @@ -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), @@ -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), @@ -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)] @@ -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), ) diff --git a/test_repository.py b/test_repository.py index 7c668a39..798908a8 100644 --- a/test_repository.py +++ b/test_repository.py @@ -1,6 +1,7 @@ # pylint: disable=protected-access import model import repository +from sqlalchemy import text def test_repository_can_save_a_batch(session): @@ -11,18 +12,24 @@ 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 @@ -30,12 +37,16 @@ def insert_order_line(session): 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 @@ -43,8 +54,10 @@ def insert_batch(session, 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), )