diff options
Diffstat (limited to 'test/sql/test_text.py')
-rw-r--r-- | test/sql/test_text.py | 173 |
1 files changed, 79 insertions, 94 deletions
diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 9d5ab65ed..0cd6f8cf3 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -54,7 +54,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): def test_text_adds_to_result_map(self): t1, t2 = text("t1"), text("t2") - stmt = select([t1, t2]) + stmt = select(t1, t2) compiled = stmt.compile() eq_( compiled._result_columns, @@ -75,16 +75,12 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): def test_select_composition_one(self): self.assert_compile( select( - [ - literal_column("foobar(a)"), - literal_column("pk_foo_bar(syslaal)"), - ], - text("a = 12"), - from_obj=[ - text( - "foobar left outer join lala on foobar.foo = lala.foo" - ) - ], + literal_column("foobar(a)"), + literal_column("pk_foo_bar(syslaal)"), + ) + .where(text("a = 12")) + .select_from( + text("foobar left outer join lala on foobar.foo = lala.foo") ), "SELECT foobar(a), pk_foo_bar(syslaal) FROM foobar " "left outer join lala on foobar.foo = lala.foo WHERE a = 12", @@ -105,7 +101,8 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): def test_select_composition_three(self): self.assert_compile( - select([column("column1"), column("column2")], from_obj=table1) + select(column("column1"), column("column2")) + .select_from(table1) .alias("somealias") .select(), "SELECT somealias.column1, somealias.column2 FROM " @@ -116,15 +113,13 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): # test that use_labels doesn't interfere with literal columns self.assert_compile( select( - [ - text("column1"), - column("column2"), - column("column3").label("bar"), - table1.c.myid, - ], - from_obj=table1, - use_labels=True, - ), + text("column1"), + column("column2"), + column("column3").label("bar"), + table1.c.myid, + ) + .select_from(table1) + .apply_labels(), "SELECT column1, column2, column3 AS bar, " "mytable.myid AS mytable_myid " "FROM mytable", @@ -135,14 +130,12 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): # with literal columns that have textual labels self.assert_compile( select( - [ - text("column1 AS foobar"), - text("column2 AS hoho"), - table1.c.myid, - ], - from_obj=table1, - use_labels=True, - ), + text("column1 AS foobar"), + text("column2 AS hoho"), + table1.c.myid, + ) + .select_from(table1) + .apply_labels(), "SELECT column1 AS foobar, column2 AS hoho, " "mytable.myid AS mytable_myid FROM mytable", ) @@ -155,13 +148,11 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): # no columns is being maintained. self.assert_compile( select( - [ - literal_column("column1 AS foobar"), - literal_column("column2 AS hoho"), - table1.c.myid, - ], - from_obj=[table1], + literal_column("column1 AS foobar"), + literal_column("column2 AS hoho"), + table1.c.myid, ) + .select_from(table1) .subquery() .select(), "SELECT anon_1.column1 AS foobar, anon_1.column2 AS hoho, " @@ -172,20 +163,17 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): def test_select_composition_seven(self): self.assert_compile( - select( - [literal_column("col1"), literal_column("col2")], - from_obj=table("tablename"), - ).alias("myalias"), + select(literal_column("col1"), literal_column("col2")) + .select_from(table("tablename")) + .alias("myalias"), "SELECT col1, col2 FROM tablename", ) def test_select_composition_eight(self): self.assert_compile( - select( - [table1.alias("t"), text("foo.f")], - text("foo.f = t.id"), - from_obj=[text("(select f from bar where lala=heyhey) foo")], - ), + select(table1.alias("t"), text("foo.f")) + .where(text("foo.f = t.id")) + .select_from(text("(select f from bar where lala=heyhey) foo")), "SELECT t.myid, t.name, t.description, foo.f FROM mytable AS t, " "(select f from bar where lala=heyhey) foo WHERE foo.f = t.id", ) @@ -193,12 +181,11 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): def test_select_bundle_columns(self): self.assert_compile( select( - [ - table1, - table2.c.otherid, - text("sysdate()"), - text("foo, bar, lala"), - ], + table1, + table2.c.otherid, + text("sysdate()"), + text("foo, bar, lala"), + ).where( and_( text("foo.id = foofoo(lala)"), text("datetime(foo) = Today"), @@ -302,7 +289,7 @@ class BindParamTest(fixtures.TestBase, AssertsCompiledSQL): t1 = text("select :foo").bindparams(bindparam("foo", 5, unique=True)) t2 = text("select :foo").bindparams(bindparam("foo", 10, unique=True)) - stmt = select([t1, t2]) + stmt = select(t1, t2) self.assert_compile( stmt, "SELECT select :foo_1, select :foo_2", @@ -383,7 +370,7 @@ class BindParamTest(fixtures.TestBase, AssertsCompiledSQL): ).bindparams(x=None, y=None, z=None) s = select( - [(func.current_date() + literal_column("s.a")).label("dates")] + (func.current_date() + literal_column("s.a")).label("dates") ).select_from(generate_series) self.assert_compile( @@ -411,7 +398,7 @@ class BindParamTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_percent_signs_literal_binds(self): - stmt = select([literal("percent % signs %%")]) + stmt = select(literal("percent % signs %%")) self.assert_compile( stmt, "SELECT 'percent % signs %%' AS anon_1", @@ -487,7 +474,7 @@ class AsFromTest(fixtures.TestBase, AssertsCompiledSQL): .subquery() ) - stmt = select([table1.c.myid]).select_from( + stmt = select(table1.c.myid).select_from( table1.join(t, table1.c.myid == t.c.id) ) compiled = stmt.compile() @@ -592,7 +579,7 @@ class AsFromTest(fixtures.TestBase, AssertsCompiledSQL): .cte("t") ) - s = select([table1]).where(table1.c.myid == t.c.id) + s = select(table1).where(table1.c.myid == t.c.id) self.assert_compile( s, "WITH t AS (select id, name from user) " @@ -608,7 +595,7 @@ class AsFromTest(fixtures.TestBase, AssertsCompiledSQL): ) stmt = ( - select([table1.c.myid]) + select(table1.c.myid) .select_from(table1.join(t, table1.c.myid == t.c.id)) .order_by(t.c.name) ) @@ -627,7 +614,7 @@ class AsFromTest(fixtures.TestBase, AssertsCompiledSQL): .alias("t") ) - s = select([table1]).where(table1.c.myid == t.c.id) + s = select(table1).where(table1.c.myid == t.c.id) self.assert_compile( s, "SELECT mytable.myid, mytable.name, mytable.description " @@ -641,7 +628,7 @@ class AsFromTest(fixtures.TestBase, AssertsCompiledSQL): assert subq.type._type_affinity is Integer()._type_affinity - s = select([table1.c.myid, subq]).where(table1.c.myid == subq) + s = select(table1.c.myid, subq).where(table1.c.myid == subq) self.assert_compile( s, "SELECT mytable.myid, (select id from user) AS anon_1 " @@ -672,16 +659,16 @@ class TextErrorsTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_where(self): - self._test(select([table1.c.myid]).where, "myid == 5", "myid == 5") + self._test(select(table1.c.myid).where, "myid == 5", "myid == 5") def test_column(self): self._test(select, ["myid"], "myid") def test_having(self): - self._test(select([table1.c.myid]).having, "myid == 5", "myid == 5") + self._test(select(table1.c.myid).having, "myid == 5", "myid == 5") def test_from(self): - self._test(select([table1.c.myid]).select_from, "mytable", "mytable") + self._test(select(table1.c.myid).select_from, "mytable", "mytable") class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): @@ -700,20 +687,20 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_order_by_label(self): - stmt = select([table1.c.myid.label("foo")]).order_by("foo") + stmt = select(table1.c.myid.label("foo")).order_by("foo") self.assert_compile( stmt, "SELECT mytable.myid AS foo FROM mytable ORDER BY foo" ) def test_order_by_colname(self): - stmt = select([table1.c.myid]).order_by("name") + stmt = select(table1.c.myid).order_by("name") self.assert_compile( stmt, "SELECT mytable.myid FROM mytable ORDER BY mytable.name" ) def test_order_by_alias_colname(self): t1 = table1.alias() - stmt = select([t1.c.myid]).apply_labels().order_by("name") + stmt = select(t1.c.myid).apply_labels().order_by("name") self.assert_compile( stmt, "SELECT mytable_1.myid AS mytable_1_myid " @@ -732,7 +719,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): for mod in modifiers: order_by = mod(order_by) - stmt = select([case]).order_by(order_by) + stmt = select(case).order_by(order_by) col_expr = str(case) self.assert_compile( @@ -740,7 +727,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_order_by_named_label_from_anon_label(self): - s1 = select([table1.c.myid.label(None).label("foo"), table1.c.name]) + s1 = select(table1.c.myid.label(None).label("foo"), table1.c.name) stmt = s1.order_by("foo") self.assert_compile( stmt, @@ -752,8 +739,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): # test [ticket:3335], assure that order_by("foo") # catches the label named "foo" in the columns clause only, # and not the label named "foo" in the FROM clause - s1 = select([table1.c.myid.label("foo"), table1.c.name]).alias() - stmt = select([s1.c.name, func.bar().label("foo")]).order_by("foo") + s1 = select(table1.c.myid.label("foo"), table1.c.name).alias() + stmt = select(s1.c.name, func.bar().label("foo")).order_by("foo") self.assert_compile( stmt, @@ -763,12 +750,12 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_unresolvable_warning_order_by(self): - stmt = select([table1.c.myid]).order_by("foobar") + stmt = select(table1.c.myid).order_by("foobar") self._test_exception(stmt, "foobar") def test_distinct_label(self): - stmt = select([table1.c.myid.label("foo")]).distinct("foo") + stmt = select(table1.c.myid.label("foo")).distinct("foo") self.assert_compile( stmt, "SELECT DISTINCT ON (foo) mytable.myid AS foo FROM mytable", @@ -777,7 +764,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_distinct_label_keyword(self): - stmt = select([table1.c.myid.label("foo")], distinct="foo") + stmt = select(table1.c.myid.label("foo")).distinct("foo") self.assert_compile( stmt, "SELECT DISTINCT ON (foo) mytable.myid AS foo FROM mytable", @@ -787,27 +774,27 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_unresolvable_distinct_label(self): from sqlalchemy.dialects import postgresql - stmt = select([table1.c.myid.label("foo")]).distinct("not a label") + stmt = select(table1.c.myid.label("foo")).distinct("not a label") self._test_exception(stmt, "not a label", dialect=postgresql.dialect()) def test_group_by_label(self): - stmt = select([table1.c.myid.label("foo")]).group_by("foo") + stmt = select(table1.c.myid.label("foo")).group_by("foo") self.assert_compile( stmt, "SELECT mytable.myid AS foo FROM mytable GROUP BY foo" ) def test_group_by_colname(self): - stmt = select([table1.c.myid]).group_by("name") + stmt = select(table1.c.myid).group_by("name") self.assert_compile( stmt, "SELECT mytable.myid FROM mytable GROUP BY mytable.name" ) def test_unresolvable_warning_group_by(self): - stmt = select([table1.c.myid]).group_by("foobar") + stmt = select(table1.c.myid).group_by("foobar") self._test_exception(stmt, "foobar") def test_asc(self): - stmt = select([table1.c.myid]).order_by(asc("name"), "description") + stmt = select(table1.c.myid).order_by(asc("name"), "description") self.assert_compile( stmt, "SELECT mytable.myid FROM mytable " @@ -815,8 +802,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_group_by_subquery(self): - stmt = select([table1]).alias() - stmt = select([stmt]).apply_labels().group_by("myid") + stmt = select(table1).alias() + stmt = select(stmt).apply_labels().group_by("myid") self.assert_compile( stmt, "SELECT anon_1.myid AS anon_1_myid, anon_1.name AS anon_1_name, " @@ -829,7 +816,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_order_by_literal_col_quoting_one(self): col = literal_column("SUM(ABC)").label("SUM(ABC)") tbl = table("my_table") - query = select([col]).select_from(tbl).order_by(col) + query = select(col).select_from(tbl).order_by(col) self.assert_compile( query, 'SELECT SUM(ABC) AS "SUM(ABC)" FROM my_table ORDER BY "SUM(ABC)"', @@ -838,7 +825,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_order_by_literal_col_quoting_two(self): col = literal_column("SUM(ABC)").label("SUM(ABC)_") tbl = table("my_table") - query = select([col]).select_from(tbl).order_by(col) + query = select(col).select_from(tbl).order_by(col) self.assert_compile( query, 'SELECT SUM(ABC) AS "SUM(ABC)_" FROM my_table ORDER BY ' @@ -848,7 +835,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_order_by_literal_col_quoting_one_explict_quote(self): col = literal_column("SUM(ABC)").label(quoted_name("SUM(ABC)", True)) tbl = table("my_table") - query = select([col]).select_from(tbl).order_by(col) + query = select(col).select_from(tbl).order_by(col) self.assert_compile( query, 'SELECT SUM(ABC) AS "SUM(ABC)" FROM my_table ORDER BY "SUM(ABC)"', @@ -857,7 +844,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_order_by_literal_col_quoting_two_explicit_quote(self): col = literal_column("SUM(ABC)").label(quoted_name("SUM(ABC)_", True)) tbl = table("my_table") - query = select([col]).select_from(tbl).order_by(col) + query = select(col).select_from(tbl).order_by(col) self.assert_compile( query, 'SELECT SUM(ABC) AS "SUM(ABC)_" FROM my_table ORDER BY ' @@ -865,9 +852,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_order_by_func_label_desc(self): - stmt = select([func.foo("bar").label("fb"), table1]).order_by( - desc("fb") - ) + stmt = select(func.foo("bar").label("fb"), table1).order_by(desc("fb")) self.assert_compile( stmt, @@ -876,7 +861,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_pg_distinct(self): - stmt = select([table1]).distinct("name") + stmt = select(table1).distinct("name") self.assert_compile( stmt, "SELECT DISTINCT ON (mytable.name) mytable.myid, " @@ -885,9 +870,9 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_over(self): - stmt = select([column("foo"), column("bar")]).subquery() + stmt = select(column("foo"), column("bar")).subquery() stmt = select( - [func.row_number().over(order_by="foo", partition_by="bar")] + func.row_number().over(order_by="foo", partition_by="bar") ).select_from(stmt) self.assert_compile( @@ -898,8 +883,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_union_column(self): - s1 = select([table1]) - s2 = select([table1]) + s1 = select(table1) + s2 = select(table1) stmt = union(s1, s2).order_by("name") self.assert_compile( stmt, @@ -909,8 +894,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_union_label(self): - s1 = select([func.foo("hoho").label("x")]) - s2 = select([func.foo("Bar").label("y")]) + s1 = select(func.foo("hoho").label("x")) + s2 = select(func.foo("Bar").label("y")) stmt = union(s1, s2).order_by("x") self.assert_compile( stmt, @@ -937,7 +922,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): adapter = sql_util.ColumnAdapter(ta, anonymize_labels=True) s1 = ( - select([adapter.columns[expr] for expr in exprs]) + select(*[adapter.columns[expr] for expr in exprs]) .apply_labels() .order_by("myid", "t1name", "x") ) @@ -969,7 +954,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): adapter = sql_util.ColumnAdapter(ta) s1 = ( - select([adapter.columns[expr] for expr in exprs]) + select(*[adapter.columns[expr] for expr in exprs]) .apply_labels() .order_by("myid", "t1name", "x") ) |