]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
update code and tests to avoid empty and_ or or_ or to catch the deprecation warning
authorFederico Caselli <cfederico87@gmail.com>
Sun, 29 Dec 2019 17:57:21 +0000 (18:57 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Sun, 29 Dec 2019 18:21:05 +0000 (19:21 +0100)
lib/sqlalchemy/orm/persistence.py
test/engine/test_execute.py
test/sql/test_compiler.py
test/sql/test_delete.py
test/sql/test_operators.py
test/sql/test_selectable.py
test/sql/test_update.py

index 58c8bcc0656b9086f9ed580579bea712e49dcd2f..1ec25d43c174992316c9e381c94b193e4efc7f07 100644 (file)
@@ -864,15 +864,13 @@ def _emit_update_statements(
     )
 
     def update_stmt():
-        clause = sql.and_()
+        clauses = []
 
         for col in mapper._pks_by_table[table]:
-            clause.clauses.append(
-                col == sql.bindparam(col._label, type_=col.type)
-            )
+            clauses.append(col == sql.bindparam(col._label, type_=col.type))
 
         if needs_version_id:
-            clause.clauses.append(
+            clauses.append(
                 mapper.version_id_col
                 == sql.bindparam(
                     mapper.version_id_col._label,
@@ -880,7 +878,7 @@ def _emit_update_statements(
                 )
             )
 
-        stmt = table.update(clause)
+        stmt = table.update(sql.and_(*clauses) if len(clauses) > 0 else None)
         return stmt
 
     cached_stmt = base_mapper._memo(("update", table), update_stmt)
@@ -1180,15 +1178,13 @@ def _emit_post_update_statements(
     )
 
     def update_stmt():
-        clause = sql.and_()
+        clauses = []
 
         for col in mapper._pks_by_table[table]:
-            clause.clauses.append(
-                col == sql.bindparam(col._label, type_=col.type)
-            )
+            clauses.append(col == sql.bindparam(col._label, type_=col.type))
 
         if needs_version_id:
-            clause.clauses.append(
+            clauses.append(
                 mapper.version_id_col
                 == sql.bindparam(
                     mapper.version_id_col._label,
@@ -1196,7 +1192,7 @@ def _emit_post_update_statements(
                 )
             )
 
-        stmt = table.update(clause)
+        stmt = table.update(sql.and_(*clauses) if len(clauses) > 0 else None)
 
         if mapper.version_id_col is not None:
             stmt = stmt.return_defaults(mapper.version_id_col)
@@ -1295,21 +1291,19 @@ def _emit_delete_statements(
     )
 
     def delete_stmt():
-        clause = sql.and_()
+        clauses = []
         for col in mapper._pks_by_table[table]:
-            clause.clauses.append(
-                col == sql.bindparam(col.key, type_=col.type)
-            )
+            clauses.append(col == sql.bindparam(col.key, type_=col.type))
 
         if need_version_id:
-            clause.clauses.append(
+            clauses.append(
                 mapper.version_id_col
                 == sql.bindparam(
                     mapper.version_id_col.key, type_=mapper.version_id_col.type
                 )
             )
 
-        return table.delete(clause)
+        return table.delete(sql.and_(*clauses) if len(clauses) > 0 else None)
 
     statement = base_mapper._memo(("delete", table), delete_stmt)
     for connection, recs in groupby(delete, lambda rec: rec[1]):  # connection
index b71eb88378445f47f0243f64d8539993c8da3d28..acd309d621fde56f13c8d859b235b63da81db76b 100644 (file)
@@ -391,9 +391,9 @@ class ExecuteTest(fixtures.TestBase):
         for obj in (
             Table("foo", MetaData(), Column("x", Integer)),
             Column("x", Integer),
-            tsa.and_(),
+            tsa.and_(True),
             column("foo"),
-            tsa.and_().compile(),
+            tsa.and_(True).compile(),
             column("foo").compile(),
             MetaData(),
             Integer(),
index b49fb455cd4988bacb4030983f43e18311d90fae..852df04bf78a7f5e8a093c1effd85ff5e2926992 100644 (file)
@@ -78,6 +78,7 @@ from sqlalchemy.testing import assert_raises_message
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import eq_
 from sqlalchemy.testing import eq_ignore_whitespace
+from sqlalchemy.testing import expect_deprecated
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing import is_
 from sqlalchemy.util import u
@@ -1396,12 +1397,24 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
                 and_(
                     or_(
                         or_(t.c.x == 12),
-                        and_(or_(), or_(and_(t.c.x == 8)), and_()),
+                        and_(or_(and_(t.c.x == 8))),
                     )
                 )
             ),
             "SELECT t.x FROM t WHERE t.x = :x_1 OR t.x = :x_2",
         )
+        with expect_deprecated():
+            self.assert_compile(
+                select([t]).where(
+                    and_(
+                        or_(
+                            or_(t.c.x == 12),
+                            and_(or_(), or_(and_(t.c.x == 8)), and_()),
+                        )
+                    )
+                ),
+                "SELECT t.x FROM t WHERE t.x = :x_1 OR t.x = :x_2",
+            )
 
     def test_true_short_circuit(self):
         t = table("t", column("x"))
@@ -1450,14 +1463,16 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
         )
 
     def test_where_empty(self):
-        self.assert_compile(
-            select([table1.c.myid]).where(and_()),
-            "SELECT mytable.myid FROM mytable",
-        )
-        self.assert_compile(
-            select([table1.c.myid]).where(or_()),
-            "SELECT mytable.myid FROM mytable",
-        )
+        with expect_deprecated():
+            self.assert_compile(
+                select([table1.c.myid]).where(and_()),
+                "SELECT mytable.myid FROM mytable",
+            )
+        with expect_deprecated():
+            self.assert_compile(
+                select([table1.c.myid]).where(or_()),
+                "SELECT mytable.myid FROM mytable",
+            )
 
     def test_order_by_nulls(self):
         self.assert_compile(
index 1f4c49c562a8443c3526b2c5d02e5cfb8d31f431..5dcc0d112bdcd4c8c2ddaf1fa50948f38d42887c 100644 (file)
@@ -15,6 +15,7 @@ from sqlalchemy.engine import default
 from sqlalchemy.testing import assert_raises_message
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import eq_
+from sqlalchemy.testing import expect_deprecated
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing.schema import Column
 from sqlalchemy.testing.schema import Table
@@ -77,12 +78,14 @@ class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL):
     def test_where_empty(self):
         table1 = self.tables.mytable
 
-        self.assert_compile(
-            table1.delete().where(and_()), "DELETE FROM mytable"
-        )
-        self.assert_compile(
-            table1.delete().where(or_()), "DELETE FROM mytable"
-        )
+        with expect_deprecated():
+            self.assert_compile(
+                table1.delete().where(and_()), "DELETE FROM mytable"
+            )
+        with expect_deprecated():
+            self.assert_compile(
+                table1.delete().where(or_()), "DELETE FROM mytable"
+            )
 
     def test_prefix_with(self):
         table1 = self.tables.mytable
index 66501256638835b2ca4b646bf468edbf5733e96a..df48d9a4db8337ec8d5172dc2753e7ba9f5d790a 100644 (file)
@@ -1034,7 +1034,8 @@ class ConjunctionTest(fixtures.TestBase, testing.AssertsCompiledSQL):
         self.assert_compile(or_(~and_(true())), "false")
 
     def test_three(self):
-        self.assert_compile(or_(and_()), "")
+        with expect_deprecated():
+            self.assert_compile(or_(and_()), "")
 
     def test_four(self):
         x = column("x")
index be7e28b5d32f2a2779a667cc51054f6c05b1cd75..6156fb870b4e16e569609c31dc02ee9e65e3a036 100644 (file)
@@ -2678,7 +2678,8 @@ class ReprTest(fixtures.TestBase):
             elements.True_(),
             elements.False_(),
             elements.ClauseList(),
-            elements.BooleanClauseList.and_(),
+            elements.BooleanClauseList.and_(True),
+            elements.BooleanClauseList.or_(False),
             elements.Tuple(),
             elements.Case([]),
             elements.Extract("foo", column("x")),
index e625b7d9cee5d41a73ab98a8b2a0495dc0abc6ef..bb155fd8532abdf80c11dd38353af0e616dcb161 100644 (file)
@@ -22,6 +22,7 @@ from sqlalchemy.testing import assert_raises
 from sqlalchemy.testing import assert_raises_message
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import eq_
+from sqlalchemy.testing import expect_deprecated
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing.schema import Column
 from sqlalchemy.testing.schema import Table
@@ -633,16 +634,18 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL):
 
     def test_where_empty(self):
         table1 = self.tables.mytable
-        self.assert_compile(
-            table1.update().where(and_()),
-            "UPDATE mytable SET myid=:myid, name=:name, "
-            "description=:description",
-        )
-        self.assert_compile(
-            table1.update().where(or_()),
-            "UPDATE mytable SET myid=:myid, name=:name, "
-            "description=:description",
-        )
+        with expect_deprecated():
+            self.assert_compile(
+                table1.update().where(and_()),
+                "UPDATE mytable SET myid=:myid, name=:name, "
+                "description=:description",
+            )
+        with expect_deprecated():
+            self.assert_compile(
+                table1.update().where(or_()),
+                "UPDATE mytable SET myid=:myid, name=:name, "
+                "description=:description",
+            )
 
     def test_prefix_with(self):
         table1 = self.tables.mytable