diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-11-03 21:26:44 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-11-03 21:34:32 -0400 |
commit | 77a17797ecc08736ea942e29f79df4f96bd74e0c (patch) | |
tree | 80c641bb03d4fdbd703c86b1ee8caf08397cb5fb /test/sql/test_case_statement.py | |
parent | 245a6ce87dbf0abf25d71074b8597bb2a0f8d5fe (diff) | |
download | sqlalchemy-77a17797ecc08736ea942e29f79df4f96bd74e0c.tar.gz |
use ExpressionElementRole for case targets in case()
Fixed regression where the :func:`_sql.text` construct would no longer be
accepted as a target case in the "whens" list within a :func:`_sql.case`
construct. The regression appears related to an attempt to guard against
some forms of literal values that were considered to be ambiguous when
passed here; however, there's no reason the target cases shouldn't be
interpreted as open-ended SQL expressions just like anywhere else, and a
literal string or tuple will be converted to a bound parameter as would be
the case elsewhere.
Fixes: #7287
Change-Id: I75478adfa115f3292cb1362cc5b2fdf152b0ed6f
Diffstat (limited to 'test/sql/test_case_statement.py')
-rw-r--r-- | test/sql/test_case_statement.py | 78 |
1 files changed, 63 insertions, 15 deletions
diff --git a/test/sql/test_case_statement.py b/test/sql/test_case_statement.py index 63491524c..db7f16194 100644 --- a/test/sql/test_case_statement.py +++ b/test/sql/test_case_statement.py @@ -2,7 +2,7 @@ from sqlalchemy import and_ from sqlalchemy import case from sqlalchemy import cast from sqlalchemy import Column -from sqlalchemy import exc +from sqlalchemy import func from sqlalchemy import Integer from sqlalchemy import literal_column from sqlalchemy import MetaData @@ -13,7 +13,6 @@ from sqlalchemy import testing from sqlalchemy import text from sqlalchemy.sql import column from sqlalchemy.sql import table -from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures @@ -125,23 +124,62 @@ class CaseTest(fixtures.TestBase, AssertsCompiledSQL): ], ) - def test_literal_interpretation_ambiguous(self): - assert_raises_message( - exc.ArgumentError, - r"Column expression expected, got 'x'", - case, - ("x", "y"), + def test_literal_interpretation_one(self): + """note this is modified as of #7287 to accept strings, tuples + and other literal values as input + where they are interpreted as bound values just like any other + expression. + + Previously, an exception would be raised that the literal was + ambiguous. + + + """ + self.assert_compile( + case(("x", "y")), + "CASE WHEN :param_1 THEN :param_2 END", + checkparams={"param_1": "x", "param_2": "y"}, ) - def test_literal_interpretation_ambiguous_tuple(self): - assert_raises_message( - exc.ArgumentError, - r"Column expression expected, got \('x', 'y'\)", - case, - (("x", "y"), "z"), + def test_literal_interpretation_two(self): + """note this is modified as of #7287 to accept strings, tuples + and other literal values as input + where they are interpreted as bound values just like any other + expression. + + Previously, an exception would be raised that the literal was + ambiguous. + + + """ + self.assert_compile( + case( + (("x", "y"), "z"), + ), + "CASE WHEN :param_1 THEN :param_2 END", + checkparams={"param_1": ("x", "y"), "param_2": "z"}, ) - def test_literal_interpretation(self): + def test_literal_interpretation_two_point_five(self): + """note this is modified as of #7287 to accept strings, tuples + and other literal values as input + where they are interpreted as bound values just like any other + expression. + + Previously, an exception would be raised that the literal was + ambiguous. + + + """ + self.assert_compile( + case( + (12, "z"), + ), + "CASE WHEN :param_1 THEN :param_2 END", + checkparams={"param_1": 12, "param_2": "z"}, + ) + + def test_literal_interpretation_three(self): t = table("test", column("col1")) self.assert_compile( @@ -220,6 +258,16 @@ class CaseTest(fixtures.TestBase, AssertsCompiledSQL): [("no",), ("no",), ("no",), ("yes",), ("no",), ("no",)], ) + def test_text_doenst_explode_even_in_whenlist(self): + """test #7287""" + self.assert_compile( + case( + (text(":case = 'upper'"), func.upper(literal_column("q"))), + else_=func.lower(literal_column("q")), + ), + "CASE WHEN :case = 'upper' THEN upper(q) ELSE lower(q) END", + ) + def testcase_with_dict(self): query = select( case( |