From 0b239579f03c82f7669d77c238e4fda8638fb9c3 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sun, 27 Nov 2022 11:28:51 -0500 Subject: Add value-level hooks for SQL type detection; apply to Range Added additional type-detection for the new PostgreSQL :class:`_postgresql.Range` type, where previous cases that allowed the psycopg2-native range objects to be received directly by the DBAPI without SQLAlchemy intercepting them stopped working, as we now have our own value object. The :class:`_postgresql.Range` object has been enhanced such that SQLAlchemy Core detects it in otherwise ambiguous situations (such as comparison to dates) and applies appropriate bind handlers. Pull request courtesy Lele Gaifax. Fixes: #8884 Closes: #8886 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8886 Pull-request-sha: 6e95e08a30597d3735ab38f2f1a2ccabd968852c Change-Id: I3ca277c826dcf4b5644f44eb251345b439a84ee4 --- test/sql/test_types.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test/sql') diff --git a/test/sql/test_types.py b/test/sql/test_types.py index d1b32186e..91413ff35 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -3293,6 +3293,43 @@ class ExpressionTest( ], ) + @testing.variation("secondary_adapt", [True, False]) + @testing.variation("expression_type", ["literal", "right_side"]) + def test_value_level_bind_hooks( + self, connection, metadata, secondary_adapt, expression_type + ): + """test new feature added in #8884, allowing custom value objects + to indicate the SQL type they should resolve towards. + + """ + + class MyFoobarType(types.UserDefinedType): + if secondary_adapt: + + def _resolve_for_literal(self, value): + return String(value.length) + + class Widget: + def __init__(self, length): + self.length = length + + @property + def __sa_type_engine__(self): + return MyFoobarType() + + if expression_type.literal: + expr = literal(Widget(52)) + elif expression_type.right_side: + expr = (column("x", Integer) == Widget(52)).right + else: + assert False + + if secondary_adapt: + is_(expr.type._type_affinity, String) + eq_(expr.type.length, 52) + else: + is_(expr.type._type_affinity, MyFoobarType) + def test_grouped_bind_adapt(self): test_table = self.tables.test -- cgit v1.2.1