]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Modernize tests - dml_whereclause
authorGord Thompson <gord@gordthompson.com>
Sun, 25 Jul 2021 19:16:31 +0000 (13:16 -0600)
committerGord Thompson <gord@gordthompson.com>
Mon, 26 Jul 2021 16:08:30 +0000 (10:08 -0600)
Fixed issue where the unit of work would internally use a 2.0-deprecated
SQL expression form, emitting a deprecation warning when SQLALCHEMY_WARN_20
were enabled.

Fixes: #6812
Change-Id: I0a031e728527a1c3382848b6ddc793939362b128

12 files changed:
doc/build/changelog/unreleased_14/6812.rst [new file with mode: 0644]
lib/sqlalchemy/orm/dependency.py
lib/sqlalchemy/testing/suite/test_rowcount.py
lib/sqlalchemy/testing/warnings.py
test/dialect/mssql/test_compiler.py
test/dialect/mssql/test_deprecations.py
test/dialect/mssql/test_query.py
test/dialect/postgresql/test_types.py
test/orm/test_expire.py
test/orm/test_transaction.py
test/sql/test_cte.py
test/sql/test_returning.py

diff --git a/doc/build/changelog/unreleased_14/6812.rst b/doc/build/changelog/unreleased_14/6812.rst
new file mode 100644 (file)
index 0000000..d91db2e
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 6812
+
+    Fixed issue where the unit of work would internally use a 2.0-deprecated
+    SQL expression form, emitting a deprecation warning when SQLALCHEMY_WARN_20
+    were enabled.
+
index eae50a6dbed521622891ae28030656119a45ce22..27919050eceaaccdcf8643a168a55e587d5dc1a9 100644 (file)
@@ -1216,7 +1216,7 @@ class ManyToManyDP(DependencyProcessor):
 
         if secondary_update:
             associationrow = secondary_update[0]
-            statement = self.secondary.update(
+            statement = self.secondary.update().where(
                 sql.and_(
                     *[
                         c == sql.bindparam("old_" + c.key, type_=c.type)
index 504ac13a5a22b28a0e5761f457013e5c1fa16c80..82e831f496697aa7639b2c3806625c1387f4e0cc 100644 (file)
@@ -69,7 +69,8 @@ class RowCountTest(fixtures.TablesTest):
         # WHERE matches 3, 3 rows changed
         department = employees_table.c.department
         r = connection.execute(
-            employees_table.update(department == "C"), {"department": "Z"}
+            employees_table.update().where(department == "C"),
+            {"department": "Z"},
         )
         assert r.rowcount == 3
 
@@ -80,7 +81,8 @@ class RowCountTest(fixtures.TablesTest):
         department = employees_table.c.department
 
         r = connection.execute(
-            employees_table.update(department == "C"), {"department": "C"}
+            employees_table.update().where(department == "C"),
+            {"department": "C"},
         )
         eq_(r.rowcount, 3)
 
@@ -90,7 +92,8 @@ class RowCountTest(fixtures.TablesTest):
 
         department = employees_table.c.department
         stmt = (
-            employees_table.update(department == "C")
+            employees_table.update()
+            .where(department == "C")
             .values(name=employees_table.c.department + "Z")
             .return_defaults()
         )
@@ -117,7 +120,9 @@ class RowCountTest(fixtures.TablesTest):
 
         # WHERE matches 3, 3 rows deleted
         department = employees_table.c.department
-        r = connection.execute(employees_table.delete(department == "C"))
+        r = connection.execute(
+            employees_table.delete().where(department == "C")
+        )
         eq_(r.rowcount, 3)
 
     @testing.requires.sane_multi_rowcount
index 54cf7589f2d050456e3d2243b6d5635a83256d9c..39dc7da58db44f745cd5db87e608566d37ec562d 100644 (file)
@@ -76,8 +76,6 @@ def setup_filters():
         #
         # DML
         #
-        r"The (?:update|delete).whereclause parameter will be removed in "
-        "SQLAlchemy 2.0.",
         r"The (?:insert|update).values parameter will be removed in "
         "SQLAlchemy 2.0.",
         r"The update.preserve_parameter_order parameter will be removed in "
index b0434115d68c8718b8b4fd9e523835b43ae9924d..c512ae44182b541c936ae1cd984f0bdf0d053dbe 100644 (file)
@@ -137,7 +137,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
     def test_update(self):
         t = table("sometable", column("somecolumn"))
         self.assert_compile(
-            t.update(t.c.somecolumn == 7),
+            t.update().where(t.c.somecolumn == 7),
             "UPDATE sometable SET somecolumn=:somecolum"
             "n WHERE sometable.somecolumn = "
             ":somecolumn_1",
@@ -703,7 +703,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             schema="paj",
         )
         self.assert_compile(
-            tbl.delete(tbl.c.id == 1),
+            tbl.delete().where(tbl.c.id == 1),
             "DELETE FROM paj.test WHERE paj.test.id = " ":id_1",
         )
         s = select(tbl.c.id).where(tbl.c.id == 1)
@@ -723,7 +723,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             schema="banana.paj",
         )
         self.assert_compile(
-            tbl.delete(tbl.c.id == 1),
+            tbl.delete().where(tbl.c.id == 1),
             "DELETE FROM banana.paj.test WHERE " "banana.paj.test.id = :id_1",
         )
         s = select(tbl.c.id).where(tbl.c.id == 1)
@@ -744,7 +744,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             schema="banana split.paj",
         )
         self.assert_compile(
-            tbl.delete(tbl.c.id == 1),
+            tbl.delete().where(tbl.c.id == 1),
             "DELETE FROM [banana split].paj.test WHERE "
             "[banana split].paj.test.id = :id_1",
         )
@@ -767,7 +767,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             schema="banana split.paj with a space",
         )
         self.assert_compile(
-            tbl.delete(tbl.c.id == 1),
+            tbl.delete().where(tbl.c.id == 1),
             "DELETE FROM [banana split].[paj with a "
             "space].test WHERE [banana split].[paj "
             "with a space].test.id = :id_1",
index 354107e811f1908f8593cf304cdeeaf2e83fcdf7..aecb813fa167d68a8a011c344a29f820fbe64a05 100644 (file)
@@ -212,24 +212,6 @@ class LegacySchemaAliasingBackendTest(
             conn.execute(tbl.insert(), {"id": 1})
             eq_(conn.scalar(tbl.select()), 1)
 
-    @testing.provide_metadata
-    def test_delete_schema(self, connection):
-        meta = self.metadata
-
-        is_(connection.dialect.legacy_schema_aliasing, False)
-
-        tbl = Table(
-            "test",
-            meta,
-            Column("id", Integer, primary_key=True),
-            schema=testing.config.test_schema,
-        )
-        tbl.create(connection)
-        connection.execute(tbl.insert(), {"id": 1})
-        eq_(connection.scalar(tbl.select()), 1)
-        connection.execute(tbl.delete(tbl.c.id == 1))
-        eq_(connection.scalar(tbl.select()), None)
-
     @testing.provide_metadata
     def test_delete_schema_legacy(self):
         meta = self.metadata
@@ -249,5 +231,5 @@ class LegacySchemaAliasingBackendTest(
             tbl.create(conn)
             conn.execute(tbl.insert(), {"id": 1})
             eq_(conn.scalar(tbl.select()), 1)
-            conn.execute(tbl.delete(tbl.c.id == 1))
+            conn.execute(tbl.delete().where(tbl.c.id == 1))
             eq_(conn.scalar(tbl.select()), None)
index 6952b26a9070658db9fc39b9e8248ef108a2b9f4..e5e3cd3ad292ed66915d9536d4fa67fbd3426207 100644 (file)
@@ -380,7 +380,7 @@ class QueryTest(testing.AssertsExecutionResults, fixtures.TestBase):
         tbl.create(connection)
         connection.execute(tbl.insert(), {"id": 1})
         eq_(connection.scalar(tbl.select()), 1)
-        connection.execute(tbl.delete(tbl.c.id == 1))
+        connection.execute(tbl.delete().where(tbl.c.id == 1))
         eq_(connection.scalar(tbl.select()), None)
 
     @testing.provide_metadata
index f667a0223e5f033b950ff8cc83a9fd09c0843f66..4f87603f3e9302d79c67adb30124c5483782fd73 100644 (file)
@@ -1019,7 +1019,9 @@ class TimezoneTest(fixtures.TablesTest):
             row[0].tzinfo.utcoffset(row[0]),
         )
         result = connection.execute(
-            tztable.update(tztable.c.id == 1).returning(tztable.c.date),
+            tztable.update()
+            .where(tztable.c.id == 1)
+            .returning(tztable.c.date),
             dict(
                 name="newname",
             ),
@@ -1043,7 +1045,9 @@ class TimezoneTest(fixtures.TablesTest):
         eq_(row[0], somedate)
         eq_(row[0].tzinfo, None)
         result = connection.execute(
-            notztable.update(notztable.c.id == 1).returning(notztable.c.date),
+            notztable.update()
+            .where(notztable.c.id == 1)
+            .returning(notztable.c.date),
             dict(
                 name="newname",
             ),
index 0e356fe783dcc3def18a5d7c7721f5056325be7e..497dbdb0377f8d987a64fbfb0956bbeb95080d90 100644 (file)
@@ -68,7 +68,9 @@ class ExpireTest(_fixtures.FixtureTest):
         u.name = "foo"
         sess.flush()
         # change the value in the DB
-        sess.execute(users.update(users.c.id == 7, values=dict(name="jack")))
+        sess.execute(
+            users.update(values=dict(name="jack")).where(users.c.id == 7)
+        )
         sess.expire(u)
         # object isn't refreshed yet, using dict to bypass trigger
         assert u.__dict__.get("name") != "jack"
@@ -936,7 +938,7 @@ class ExpireTest(_fixtures.FixtureTest):
         sess.expire(u)
         assert "name" not in u.__dict__
 
-        sess.execute(users.update(users.c.id == 7), dict(name="jack2"))
+        sess.execute(users.update().where(users.c.id == 7), dict(name="jack2"))
         assert u.name == "jack2"
         assert u.uname == "jack2"
         assert "name" in u.__dict__
@@ -961,7 +963,7 @@ class ExpireTest(_fixtures.FixtureTest):
         assert attributes.instance_state(o).dict["isopen"] == 1
 
         sess.execute(
-            orders.update(orders.c.id == 3),
+            orders.update().where(orders.c.id == 3),
             dict(description="order 3 modified"),
         )
 
index 25f5d22ce878b90ce5c8f3748b74a0176046d209..ad57a8031623269ba0fa298cd58cb0d487ef4a5c 100644 (file)
@@ -1945,7 +1945,9 @@ class AccountingFlagsTest(_LocalFixture):
         sess.add(u1)
         sess.commit()
 
-        sess.execute(users.update(users.c.name == "ed").values(name="edward"))
+        sess.execute(
+            users.update().where(users.c.name == "ed").values(name="edward")
+        )
 
         assert u1.name == "ed"
         sess.expire_all()
index f1d27aa8f174c6d73d7cad8bd2144ae5fc4b1030..77905cd896dbfd040c7f44fe7991e2983174c20a 100644 (file)
@@ -1504,7 +1504,7 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL):
             )
         )
         cte = q.cte("deldup")
-        stmt = delete(ctetext("RN > 1"))
+        stmt = delete(cte).where(text("RN > 1"))
         eq_(stmt.compile().execution_options["autocommit"], True)
 
         self.assert_compile(
index a0d69e782c16df25d7914eb603cb5ecc83210f84..10bf3beb6fe9b44576c79bd338d09076148529e1 100644 (file)
@@ -189,9 +189,10 @@ class ReturningTest(fixtures.TablesTest, AssertsExecutionResults):
         )
 
         result = connection.execute(
-            table.update(table.c.persons > 4, dict(full=True)).returning(
-                table.c.id
-            )
+            table.update()
+            .values(dict(full=True))
+            .where(table.c.persons > 4)
+            .returning(table.c.id)
         )
         eq_(result.fetchall(), [(1,)])
 
@@ -291,7 +292,8 @@ class ReturningTest(fixtures.TablesTest, AssertsExecutionResults):
         )
 
         result = connection.execute(
-            table.update(table.c.persons > 2)
+            table.update()
+            .where(table.c.persons > 2)
             .values(full=True)
             .returning(table.c.id, table.c.full)
         )
@@ -369,7 +371,7 @@ class ReturningTest(fixtures.TablesTest, AssertsExecutionResults):
         )
 
         result = connection.execute(
-            table.delete(table.c.persons > 4).returning(table.c.id)
+            table.delete().where(table.c.persons > 4).returning(table.c.id)
         )
         eq_(result.fetchall(), [(1,)])