From a7a8804a836ce9edcfdee7d7406cd3c050b34f05 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 6 Dec 2019 10:24:25 -0500 Subject: Include DISTINCT in error message for label reference Needed to add tests to ensure this label reference is handled correctly, so also modified the exception message to be more clear if someone has this error within distinct(). Change-Id: I6e685e46ae336596272d14366445ac224c18d92c --- test/sql/test_compiler.py | 3 ++- test/sql/test_text.py | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'test/sql') diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index ca73f6c18..486b54682 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -2073,7 +2073,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # to check the special thing CompoundSelect does with labels assert_raises_message( exc.CompileError, - "Can't resolve label reference for ORDER BY / GROUP BY. Textual " + "Can't resolve label reference for ORDER BY / GROUP BY / " + "DISTINCT etc. Textual " "SQL expression 'noname'", union( select([table1.c.myid, table1.c.name]), diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 3f12d06a9..3386d0aae 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -678,14 +678,16 @@ class TextErrorsTest(fixtures.TestBase, AssertsCompiledSQL): class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "default" - def _test_exception(self, stmt, offending_clause): + def _test_exception(self, stmt, offending_clause, dialect=None): assert_raises_message( exc.CompileError, - r"Can't resolve label reference for ORDER BY / GROUP BY. " + r"Can't resolve label reference for ORDER BY / GROUP BY / " + "DISTINCT etc. " "Textual SQL " "expression %r should be explicitly " r"declared as text\(%r\)" % (offending_clause, offending_clause), stmt.compile, + dialect=dialect, ) def test_order_by_label(self): @@ -736,6 +738,21 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): 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") + self.assert_compile( + stmt, + "SELECT DISTINCT ON (foo) mytable.myid AS foo FROM mytable", + dialect="postgresql", + ) + + def test_unresolvable_distinct_label(self): + from sqlalchemy.dialects import postgresql + + 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") self.assert_compile( @@ -890,7 +907,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): assert_raises_message( exc.CompileError, - r"Can't resolve label reference for ORDER BY / GROUP BY. " + r"Can't resolve label reference for ORDER BY / GROUP BY / " + "DISTINCT etc. " "Textual SQL " "expression 't1name' should be explicitly " r"declared as text\('t1name'\)", -- cgit v1.2.1