pass
value = kw.pop("value", None)
- if value is not None:
- whenlist = [
- (
- coercions.expect(
- roles.ExpressionElementRole,
- c,
- apply_propagate_attrs=self,
- ).self_group(),
- coercions.expect(roles.ExpressionElementRole, r),
- )
- for (c, r) in whens
- ]
- else:
- whenlist = [
- (
- coercions.expect(
- roles.ColumnArgumentRole, c, apply_propagate_attrs=self
- ).self_group(),
- coercions.expect(roles.ExpressionElementRole, r),
- )
- for (c, r) in whens
- ]
+
+ whenlist = [
+ (
+ coercions.expect(
+ roles.ExpressionElementRole,
+ c,
+ apply_propagate_attrs=self,
+ ).self_group(),
+ coercions.expect(roles.ExpressionElementRole, r),
+ )
+ for (c, r) in whens
+ ]
if whenlist:
type_ = list(whenlist[-1])[-1].type
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
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
],
)
- 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(
[("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(
from sqlalchemy import Column
from sqlalchemy import desc
from sqlalchemy import exc
+from sqlalchemy import extract
from sqlalchemy import Float
from sqlalchemy import func
from sqlalchemy import Integer
"(select f from bar where lala=heyhey) foo WHERE foo.f = t.id",
)
+ def test_expression_element_role(self):
+ """test #7287"""
+
+ self.assert_compile(
+ extract("year", text("some_date + :param")),
+ "EXTRACT(year FROM some_date + :param)",
+ )
+
@testing.combinations(
(
None,