diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2021-07-21 21:23:37 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-07-21 21:23:37 +0000 |
| commit | 54ca40b8b3e4286183b64198573b55731b1ce363 (patch) | |
| tree | 453f951c8650999010bc2fd774abd5d5c21c1cee /test/sql | |
| parent | 79a27c9fcb1ba4909834a7e2d51c29afb0688cd5 (diff) | |
| parent | c2ea2b7308a376640cf051d33c7f2f06373487c9 (diff) | |
| download | sqlalchemy-54ca40b8b3e4286183b64198573b55731b1ce363.tar.gz | |
Merge "Modernize tests - legacy_select"
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_deprecations.py | 81 | ||||
| -rw-r--r-- | test/sql/test_lambdas.py | 2 | ||||
| -rw-r--r-- | test/sql/test_query.py | 100 | ||||
| -rw-r--r-- | test/sql/test_quote.py | 14 | ||||
| -rw-r--r-- | test/sql/test_resultset.py | 6 | ||||
| -rw-r--r-- | test/sql/test_select.py | 43 | ||||
| -rw-r--r-- | test/sql/test_selectable.py | 6 | ||||
| -rw-r--r-- | test/sql/test_text.py | 4 | ||||
| -rw-r--r-- | test/sql/test_types.py | 2 |
9 files changed, 148 insertions, 110 deletions
diff --git a/test/sql/test_deprecations.py b/test/sql/test_deprecations.py index 3256ebcad..44135e373 100644 --- a/test/sql/test_deprecations.py +++ b/test/sql/test_deprecations.py @@ -2805,3 +2805,84 @@ class DDLDeprecatedBindTest(fixtures.TestBase): c1 = const(m1, bind=testing.db) is_(c1.bind, testing.db) + + +class FutureSelectTest(fixtures.TestBase, AssertsCompiledSQL): + __dialect__ = "default" + + @testing.fixture + def table_fixture(self): + table1 = table( + "mytable", + column("myid", Integer), + column("name", String), + column("description", String), + ) + + table2 = table( + "myothertable", + column("otherid", Integer), + column("othername", String), + ) + return table1, table2 + + def test_legacy_calling_style_kw_only(self, table_fixture): + table1, table2 = table_fixture + with testing.expect_deprecated_20( + "The legacy calling style of select" + ): + stmt = select( + whereclause=table1.c.myid == table2.c.otherid + ).add_columns(table1.c.myid) + + self.assert_compile( + stmt, + "SELECT mytable.myid FROM mytable, myothertable " + "WHERE mytable.myid = myothertable.otherid", + ) + + def test_legacy_calling_style_col_seq_only(self, table_fixture): + table1, table2 = table_fixture + with testing.expect_deprecated_20( + "The legacy calling style of select" + ): + # keep [] here + stmt = select([table1.c.myid]).where( + table1.c.myid == table2.c.otherid + ) + + self.assert_compile( + stmt, + "SELECT mytable.myid FROM mytable, myothertable " + "WHERE mytable.myid = myothertable.otherid", + ) + + def test_new_calling_style_thing_ok_actually_use_iter(self, table_fixture): + table1, table2 = table_fixture + + class Thing(object): + def __iter__(self): + return iter([table1.c.name, table1.c.description]) + + with testing.expect_deprecated_20( + "The legacy calling style of select" + ): + stmt = select(Thing()) + self.assert_compile( + stmt, + "SELECT mytable.name, mytable.description FROM mytable", + ) + + def test_kw_triggers_old_style(self, table_fixture): + table1, table2 = table_fixture + with testing.expect_deprecated_20( + "The legacy calling style of select" + ): + assert_raises_message( + exc.ArgumentError, + r"select\(\) construct created in legacy mode, " + "i.e. with keyword arguments", + select, + table1.c.myid, + whereclause=table1.c.myid == table2.c.otherid, + ) diff --git a/test/sql/test_lambdas.py b/test/sql/test_lambdas.py index 2de969521..ea31c9aec 100644 --- a/test/sql/test_lambdas.py +++ b/test/sql/test_lambdas.py @@ -948,7 +948,7 @@ class LambdaElementTest( exc.ArgumentError, "Textual column expression 'f' should be explicitly declared", select, - [lambda: "foo"], + lambda: "foo", ) def test_coercion_where_clause(self): diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 005693402..0d8170113 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -403,16 +403,16 @@ class QueryTest(fixtures.TablesTest): return stmt a_eq( - users.select(order_by=[users.c.user_id]).set_label_style( - label_style - ), + users.select() + .order_by(users.c.user_id) + .set_label_style(label_style), [(1, "c"), (2, "b"), (3, "a")], ) a_eq( - users.select( - order_by=[users.c.user_name, users.c.user_id], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name, users.c.user_id) + .set_label_style(label_style), [(3, "a"), (2, "b"), (1, "c")], ) @@ -435,10 +435,10 @@ class QueryTest(fixtures.TablesTest): ) a_eq( - users.select( - distinct=True, - order_by=[users.c.user_id], - ).set_label_style(label_style), + users.select() + .distinct() + .order_by(users.c.user_id) + .set_label_style(label_style), [(1, "c"), (2, "b"), (3, "a")], ) @@ -463,10 +463,10 @@ class QueryTest(fixtures.TablesTest): ) a_eq( - users.select( - distinct=True, - order_by=[desc(users.c.user_id)], - ).set_label_style(label_style), + users.select() + .distinct() + .order_by(desc(users.c.user_id)) + .set_label_style(label_style), [(3, "a"), (2, "b"), (1, "c")], ) @@ -503,75 +503,75 @@ class QueryTest(fixtures.TablesTest): else LABEL_STYLE_TABLENAME_PLUS_COL ) a_eq( - users.select( - order_by=[users.c.user_name.nulls_first()], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name.nulls_first()) + .set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( - users.select( - order_by=[users.c.user_name.nulls_last()], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name.nulls_last()) + .set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) a_eq( - users.select( - order_by=[asc(users.c.user_name).nulls_first()], - ).set_label_style(label_style), + users.select() + .order_by(asc(users.c.user_name).nulls_first()) + .set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( - users.select( - order_by=[asc(users.c.user_name).nulls_last()], - ).set_label_style(label_style), + users.select() + .order_by(asc(users.c.user_name).nulls_last()) + .set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) a_eq( - users.select( - order_by=[users.c.user_name.desc().nulls_first()], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name.desc().nulls_first()) + .set_label_style(label_style), [(1, None), (2, "b"), (3, "a")], ) a_eq( - users.select( - order_by=[users.c.user_name.desc().nulls_last()], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name.desc().nulls_last()) + .set_label_style(label_style), [(2, "b"), (3, "a"), (1, None)], ) a_eq( - users.select( - order_by=[desc(users.c.user_name).nulls_first()], - ).set_label_style(label_style), + users.select() + .order_by(desc(users.c.user_name).nulls_first()) + .set_label_style(label_style), [(1, None), (2, "b"), (3, "a")], ) a_eq( - users.select( - order_by=[desc(users.c.user_name).nulls_last()], - ).set_label_style(label_style), + users.select() + .order_by(desc(users.c.user_name).nulls_last()) + .set_label_style(label_style), [(2, "b"), (3, "a"), (1, None)], ) a_eq( - users.select( - order_by=[ - users.c.user_name.nulls_first(), - users.c.user_id, - ], - ).set_label_style(label_style), + users.select() + .order_by( + users.c.user_name.nulls_first(), + users.c.user_id, + ) + .set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( - users.select( - order_by=[users.c.user_name.nulls_last(), users.c.user_id], - ).set_label_style(label_style), + users.select() + .order_by(users.c.user_name.nulls_last(), users.c.user_id) + .set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) @@ -1008,7 +1008,7 @@ class LimitTest(fixtures.TablesTest): def test_select_limit(self, connection): users, addresses = self.tables("users", "addresses") r = connection.execute( - users.select(limit=3, order_by=[users.c.user_id]) + users.select().limit(3).order_by(users.c.user_id) ).fetchall() self.assert_(r == [(1, "john"), (2, "jack"), (3, "ed")], repr(r)) @@ -1019,11 +1019,11 @@ class LimitTest(fixtures.TablesTest): users, addresses = self.tables("users", "addresses") r = connection.execute( - users.select(limit=3, offset=2, order_by=[users.c.user_id]) + users.select().limit(3).offset(2).order_by(users.c.user_id) ).fetchall() self.assert_(r == [(3, "ed"), (4, "wendy"), (5, "laura")]) r = connection.execute( - users.select(offset=5, order_by=[users.c.user_id]) + users.select().offset(5).order_by(users.c.user_id) ).fetchall() self.assert_(r == [(6, "ralph"), (7, "fido")]) diff --git a/test/sql/test_quote.py b/test/sql/test_quote.py index bf885a7f5..e51bdf5a0 100644 --- a/test/sql/test_quote.py +++ b/test/sql/test_quote.py @@ -145,11 +145,11 @@ class QuoteExecTest(fixtures.TablesTest): table1.c.MixedCase, table1.c.a123, ] - result = connection.execute(select(columns)).all() + result = connection.execute(select(*columns)).all() assert result == [(1, 2, 3, 4), (2, 2, 3, 4), (4, 3, 2, 1)] columns = [table2.c.d123, table2.c.u123, table2.c.MixedCase] - result = connection.execute(select(columns)).all() + result = connection.execute(select(*columns)).all() assert result == [(1, 2, 3), (2, 2, 3), (4, 3, 2)] def test_use_labels(self, connection): @@ -178,13 +178,13 @@ class QuoteExecTest(fixtures.TablesTest): table1.c.a123, ] result = connection.execute( - select(columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + select(*columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ).fetchall() assert result == [(1, 2, 3, 4), (2, 2, 3, 4), (4, 3, 2, 1)] columns = [table2.c.d123, table2.c.u123, table2.c.MixedCase] result = connection.execute( - select(columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + select(*columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ).all() assert result == [(1, 2, 3), (2, 2, 3), (4, 3, 2)] @@ -232,7 +232,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile( - table1.select(distinct=True).alias("LaLa").select(), + table1.select().distinct().alias("LaLa").select(), "SELECT " '"LaLa".lowercase, ' '"LaLa"."UPPERCASE", ' @@ -669,7 +669,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): # Note that 'col1' is already quoted (literal_column) columns = [sql.literal_column("'col1'").label("label1")] - x = select(columns, from_obj=[table]).alias("alias1") + x = select(*columns).select_from(table).alias("alias1") x = x.select() self.assert_compile( x, @@ -688,7 +688,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): # Note that 'Col1' is already quoted (literal_column) columns = [sql.literal_column("'Col1'").label("Label1")] - x = select(columns, from_obj=[table]).alias("Alias1") + x = select(*columns).select_from(table).alias("Alias1") x = x.select() self.assert_compile( x, diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index e75ddeede..936d0d9db 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -152,9 +152,7 @@ class CursorResultTest(fixtures.TablesTest): .where(users.c.user_name == "jack") .scalar_subquery() ) - for row in connection.execute( - select([sel + 1, sel + 3], bind=users.bind) - ): + for row in connection.execute(select(sel + 1, sel + 3)): eq_(row._mapping["anon_1"], 8) eq_(row._mapping["anon_2"], 10) @@ -2072,7 +2070,7 @@ class KeyTargetingTest(fixtures.TablesTest): def _adapt_result_columns_fixture_five(self): users, teams = self.tables("users", "teams") - return select([users.c.id, teams.c.id]).select_from( + return select(users.c.id, teams.c.id).select_from( users.outerjoin(teams) ) diff --git a/test/sql/test_select.py b/test/sql/test_select.py index 37d43f89f..17b47d96d 100644 --- a/test/sql/test_select.py +++ b/test/sql/test_select.py @@ -56,27 +56,6 @@ grandchild = Table( class FutureSelectTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "default" - def test_legacy_calling_style_kw_only(self): - stmt = select( - whereclause=table1.c.myid == table2.c.otherid - ).add_columns(table1.c.myid) - - self.assert_compile( - stmt, - "SELECT mytable.myid FROM mytable, myothertable " - "WHERE mytable.myid = myothertable.otherid", - ) - - def test_legacy_calling_style_col_seq_only(self): - # keep [] here - stmt = select([table1.c.myid]).where(table1.c.myid == table2.c.otherid) - - self.assert_compile( - stmt, - "SELECT mytable.myid FROM mytable, myothertable " - "WHERE mytable.myid = myothertable.otherid", - ) - def test_new_calling_style(self): stmt = select(table1.c.myid).where(table1.c.myid == table2.c.otherid) @@ -123,28 +102,6 @@ class FutureSelectTest(fixtures.TestBase, AssertsCompiledSQL): "mytable.description FROM mytable", ) - def test_new_calling_style_thing_ok_actually_use_iter(self): - class Thing(object): - def __iter__(self): - return iter([table1.c.name, table1.c.description]) - - stmt = select(Thing()) - self.assert_compile( - stmt, - "SELECT mytable.name, mytable.description FROM mytable", - ) - - def test_kw_triggers_old_style(self): - - assert_raises_message( - exc.ArgumentError, - r"select\(\) construct created in legacy mode, " - "i.e. with keyword arguments", - select, - table1.c.myid, - whereclause=table1.c.myid == table2.c.otherid, - ) - def test_join_nofrom_implicit_left_side_explicit_onclause(self): stmt = select(table1).join(table2, table1.c.myid == table2.c.otherid) diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index 61e25ddd7..5a94d4038 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -3444,7 +3444,7 @@ class ResultMapTest(fixtures.TestBase): ) def test_plain_exists(self): - expr = exists([1]) + expr = exists(text("1")) eq_(type(expr.type), Boolean) eq_( [ @@ -3455,7 +3455,7 @@ class ResultMapTest(fixtures.TestBase): ) def test_plain_exists_negate(self): - expr = ~exists([1]) + expr = ~exists(text("1")) eq_(type(expr.type), Boolean) eq_( [ @@ -3466,7 +3466,7 @@ class ResultMapTest(fixtures.TestBase): ) def test_plain_exists_double_negate(self): - expr = ~(~exists([1])) + expr = ~(~exists(text("1"))) eq_(type(expr.type), Boolean) eq_( [ diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 89cfa62c9..407e8aeb5 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -768,13 +768,15 @@ class TextErrorsTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "default" def _test(self, fn, arg, offending_clause): + arg = util.to_list(arg) + assert_raises_message( exc.ArgumentError, r"Textual (?:SQL|column|SQL FROM) expression %(stmt)r should be " r"explicitly declared (?:with|as) text\(%(stmt)r\)" % {"stmt": util.ellipses_string(offending_clause)}, fn, - arg, + *arg ) def test_where(self): diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 309baaabf..88a51c0c6 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -2636,7 +2636,7 @@ class BinaryTest(fixtures.TablesTest, AssertsExecutionResults): ) for stmt in ( - binary_table.select(order_by=binary_table.c.primary_id), + binary_table.select().order_by(binary_table.c.primary_id), text( "select * from binary_table order by binary_table.primary_id", ).columns( |
