From dd03cedc26f8ccd2717e31a3fcc8e6195caa0e52 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sun, 29 Dec 2019 18:57:21 +0100 Subject: [PATCH] update code and tests to avoid empty and_ or or_ or to catch the deprecation warning --- lib/sqlalchemy/orm/persistence.py | 30 +++++++++++----------------- test/engine/test_execute.py | 4 ++-- test/sql/test_compiler.py | 33 ++++++++++++++++++++++--------- test/sql/test_delete.py | 15 ++++++++------ test/sql/test_operators.py | 3 ++- test/sql/test_selectable.py | 3 ++- test/sql/test_update.py | 23 +++++++++++---------- 7 files changed, 64 insertions(+), 47 deletions(-) diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index 58c8bcc065..1ec25d43c1 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -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 diff --git a/test/engine/test_execute.py b/test/engine/test_execute.py index b71eb88378..acd309d621 100644 --- a/test/engine/test_execute.py +++ b/test/engine/test_execute.py @@ -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(), diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index b49fb455cd..852df04bf7 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -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( diff --git a/test/sql/test_delete.py b/test/sql/test_delete.py index 1f4c49c562..5dcc0d112b 100644 --- a/test/sql/test_delete.py +++ b/test/sql/test_delete.py @@ -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 diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 6650125663..df48d9a4db 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -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") diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index be7e28b5d3..6156fb870b 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -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")), diff --git a/test/sql/test_update.py b/test/sql/test_update.py index e625b7d9ce..bb155fd853 100644 --- a/test/sql/test_update.py +++ b/test/sql/test_update.py @@ -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 -- 2.47.3