diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-07-05 21:05:18 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-07-06 09:58:11 -0400 |
| commit | 370d73b4e4b3009ea5feed1341ead965f6aa98bb (patch) | |
| tree | d86d23568f0edc9b2c67210073c83bca99eaca7e /test | |
| parent | 6e8c390c1b937d842893646fb59785eaa48b243b (diff) | |
| download | sqlalchemy-370d73b4e4b3009ea5feed1341ead965f6aa98bb.tar.gz | |
generalize sql server check for id col to accommodate ORM cases
Fixed issues that prevented the new usage patterns for using DML with ORM
objects presented at :ref:`orm_dml_returning_objects` from working
correctly with the SQL Server pyodbc dialect.
Here we add a step to look in compile_state._dict_values more thoroughly
for the keys we need to determine "identity insert" or not, and also
add a new compiler variable dml_compile_state so that we can skip the
ORM's compile_state if present.
Fixes: #8210
Change-Id: Idbd76bb3eb075c647dc6c1cb78f7315c821e15f7
Diffstat (limited to 'test')
| -rw-r--r-- | test/orm/test_update_delete.py | 44 | ||||
| -rw-r--r-- | test/sql/test_insert_exec.py | 28 |
2 files changed, 59 insertions, 13 deletions
diff --git a/test/orm/test_update_delete.py b/test/orm/test_update_delete.py index 22d827be9..933d2bb1f 100644 --- a/test/orm/test_update_delete.py +++ b/test/orm/test_update_delete.py @@ -2253,21 +2253,45 @@ class LoadFromReturningTest(fixtures.MappedTest): [User(name="jack", age=52), User(name="jill", age=34)], ) - def test_load_from_insert(self, connection): + @testing.combinations( + ("single",), + ("multiple", testing.requires.multivalues_inserts), + argnames="params", + ) + def test_load_from_insert(self, connection, params): User = self.classes.User - stmt = ( - insert(User) - .values({User.id: 5, User.age: 25, User.name: "spongebob"}) - .returning(User) - ) + if params == "multiple": + values = [ + {User.id: 5, User.age: 25, User.name: "spongebob"}, + {User.id: 6, User.age: 30, User.name: "patrick"}, + {User.id: 7, User.age: 35, User.name: "squidward"}, + ] + elif params == "single": + values = {User.id: 5, User.age: 25, User.name: "spongebob"} + else: + assert False + + stmt = insert(User).values(values).returning(User) stmt = select(User).from_statement(stmt) with Session(connection) as sess: rows = sess.execute(stmt).scalars().all() - eq_( - rows, - [User(name="spongebob", age=25)], - ) + if params == "multiple": + eq_( + rows, + [ + User(name="spongebob", age=25), + User(name="patrick", age=30), + User(name="squidward", age=35), + ], + ) + elif params == "single": + eq_( + rows, + [User(name="spongebob", age=25)], + ) + else: + assert False diff --git a/test/sql/test_insert_exec.py b/test/sql/test_insert_exec.py index 3e51e9450..b6945813e 100644 --- a/test/sql/test_insert_exec.py +++ b/test/sql/test_insert_exec.py @@ -19,6 +19,14 @@ from sqlalchemy.testing.schema import Column from sqlalchemy.testing.schema import Table +class ExpectExpr: + def __init__(self, element): + self.element = element + + def __clause_element__(self): + return self.element + + class InsertExecTest(fixtures.TablesTest): __backend__ = True @@ -35,13 +43,27 @@ class InsertExecTest(fixtures.TablesTest): ) @testing.requires.multivalues_inserts - def test_multivalues_insert(self, connection): + @testing.combinations("string", "column", "expect", argnames="keytype") + def test_multivalues_insert(self, connection, keytype): + users = self.tables.users + + if keytype == "string": + user_id, user_name = "user_id", "user_name" + elif keytype == "column": + user_id, user_name = users.c.user_id, users.c.user_name + elif keytype == "expect": + user_id, user_name = ExpectExpr(users.c.user_id), ExpectExpr( + users.c.user_name + ) + else: + assert False + connection.execute( users.insert().values( [ - {"user_id": 7, "user_name": "jack"}, - {"user_id": 8, "user_name": "ed"}, + {user_id: 7, user_name: "jack"}, + {user_id: 8, user_name: "ed"}, ] ) ) |
