diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2023-04-21 15:28:43 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2023-04-21 15:28:43 +0000 |
| commit | 95628d9707cdfbfdd229b2acee02fbadfbe7ced0 (patch) | |
| tree | c8b17287a05a074f2c0081772b829b2f6c046140 /test | |
| parent | 98b77c36ed90894a4f7d4b9b43b8903675f36717 (diff) | |
| parent | 63f51491c5f0cb22883c800a065d7c4b4c54774e (diff) | |
| download | sqlalchemy-95628d9707cdfbfdd229b2acee02fbadfbe7ced0.tar.gz | |
Merge "try to omit unnecessary cols for ORM bulk insert + returning" into main
Diffstat (limited to 'test')
| -rw-r--r-- | test/orm/dml/test_bulk_statements.py | 80 |
1 files changed, 59 insertions, 21 deletions
diff --git a/test/orm/dml/test_bulk_statements.py b/test/orm/dml/test_bulk_statements.py index b4628af67..7a9f3324f 100644 --- a/test/orm/dml/test_bulk_statements.py +++ b/test/orm/dml/test_bulk_statements.py @@ -33,7 +33,7 @@ from sqlalchemy.testing.entities import ComparableEntity from sqlalchemy.testing.fixtures import fixture_session -class InsertStmtTest(fixtures.TestBase): +class InsertStmtTest(testing.AssertsExecutionResults, fixtures.TestBase): def test_no_returning_error(self, decl_base): class A(fixtures.ComparableEntity, decl_base): __tablename__ = "a" @@ -90,6 +90,48 @@ class InsertStmtTest(fixtures.TestBase): ) @testing.requires.insert_returning + def test_insert_returning_cols_dont_give_me_defaults(self, decl_base): + """test #9685""" + + class User(decl_base): + __tablename__ = "users" + + id: Mapped[int] = mapped_column(Identity(), primary_key=True) + + name: Mapped[str] = mapped_column() + other_thing: Mapped[Optional[str]] + server_thing: Mapped[str] = mapped_column(server_default="thing") + + decl_base.metadata.create_all(testing.db) + insert_stmt = insert(User).returning(User.id) + + s = fixture_session() + + with self.sql_execution_asserter() as asserter: + result = s.execute( + insert_stmt, + [ + {"name": "some name 1"}, + {"name": "some name 2"}, + {"name": "some name 3"}, + ], + ) + + eq_(result.all(), [(1,), (2,), (3,)]) + + asserter.assert_( + CompiledSQL( + "INSERT INTO users (name) VALUES (:name) " + "RETURNING users.id", + [ + {"name": "some name 1"}, + {"name": "some name 2"}, + {"name": "some name 3"}, + ], + ), + ) + + @testing.requires.insert_returning def test_insert_from_select_col_property(self, decl_base): """test #9273""" @@ -191,17 +233,12 @@ class BulkDMLReturningInhTest: with self.sql_execution_asserter() as asserter: result = s.execute(stmt, values) - if inspect(B).single: - single_inh = ", a.bd, a.zcol, a.q" - else: - single_inh = "" - if use_returning: asserter.assert_( CompiledSQL( "INSERT INTO a (type, data, xcol) VALUES " "(:type, :data, :xcol) " - f"RETURNING a.id, a.type, a.data, a.xcol, a.y{single_inh}", + "RETURNING a.id, a.type, a.data, a.xcol, a.y", [ {"type": "a", "data": "d3", "xcol": 5}, {"type": "a", "data": "d4", "xcol": 6}, @@ -209,13 +246,13 @@ class BulkDMLReturningInhTest: ), CompiledSQL( "INSERT INTO a (type, data) VALUES (:type, :data) " - f"RETURNING a.id, a.type, a.data, a.xcol, a.y{single_inh}", + "RETURNING a.id, a.type, a.data, a.xcol, a.y", [{"type": "a", "data": "d5"}], ), CompiledSQL( "INSERT INTO a (type, data, xcol, y) " "VALUES (:type, :data, :xcol, :y) " - f"RETURNING a.id, a.type, a.data, a.xcol, a.y{single_inh}", + "RETURNING a.id, a.type, a.data, a.xcol, a.y", [ {"type": "a", "data": "d6", "xcol": 8, "y": 9}, {"type": "a", "data": "d7", "xcol": 12, "y": 12}, @@ -224,7 +261,7 @@ class BulkDMLReturningInhTest: CompiledSQL( "INSERT INTO a (type, data, xcol) " "VALUES (:type, :data, :xcol) " - f"RETURNING a.id, a.type, a.data, a.xcol, a.y{single_inh}", + "RETURNING a.id, a.type, a.data, a.xcol, a.y", [{"type": "a", "data": "d8", "xcol": 7}], ), ) @@ -258,17 +295,18 @@ class BulkDMLReturningInhTest: ) if use_returning: - eq_( - result.scalars().all(), - [ - A(data="d3", id=mock.ANY, type="a", x=5, y=None), - A(data="d4", id=mock.ANY, type="a", x=6, y=None), - A(data="d5", id=mock.ANY, type="a", x=None, y=None), - A(data="d6", id=mock.ANY, type="a", x=8, y=9), - A(data="d7", id=mock.ANY, type="a", x=12, y=12), - A(data="d8", id=mock.ANY, type="a", x=7, y=None), - ], - ) + with self.assert_statement_count(testing.db, 0): + eq_( + result.scalars().all(), + [ + A(data="d3", id=mock.ANY, type="a", x=5, y=None), + A(data="d4", id=mock.ANY, type="a", x=6, y=None), + A(data="d5", id=mock.ANY, type="a", x=None, y=None), + A(data="d6", id=mock.ANY, type="a", x=8, y=9), + A(data="d7", id=mock.ANY, type="a", x=12, y=12), + A(data="d8", id=mock.ANY, type="a", x=7, y=None), + ], + ) @testing.combinations( "strings", |
