summaryrefslogtreecommitdiff
path: root/test/dialect/postgresql/test_compiler.py
diff options
context:
space:
mode:
authorLele Gaifax <lele@metapensiero.it>2022-11-27 11:28:51 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-11-29 17:11:38 -0500
commit0b239579f03c82f7669d77c238e4fda8638fb9c3 (patch)
treeebe836c6d9f60362c4824843478122c7f725c2bd /test/dialect/postgresql/test_compiler.py
parent61443aa62bbef158274ae393db399fec7f054c2d (diff)
downloadsqlalchemy-0b239579f03c82f7669d77c238e4fda8638fb9c3.tar.gz
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
Diffstat (limited to 'test/dialect/postgresql/test_compiler.py')
-rw-r--r--test/dialect/postgresql/test_compiler.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py
index 431cd7ded..ee3372c74 100644
--- a/test/dialect/postgresql/test_compiler.py
+++ b/test/dialect/postgresql/test_compiler.py
@@ -42,6 +42,7 @@ from sqlalchemy.dialects.postgresql import ExcludeConstraint
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import JSONPATH
+from sqlalchemy.dialects.postgresql import Range
from sqlalchemy.dialects.postgresql import TSRANGE
from sqlalchemy.dialects.postgresql.base import PGDialect
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
@@ -2397,6 +2398,26 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
"AS jsonb_path_exists_1 FROM data",
)
+ def test_custom_object_hook(self):
+ # See issue #8884
+ from datetime import date
+
+ usages = table(
+ "usages",
+ column("id", Integer),
+ column("date", Date),
+ column("amount", Integer),
+ )
+ period = Range(date(2022, 1, 1), (2023, 1, 1))
+ stmt = select(func.sum(usages.c.amount)).where(
+ usages.c.date.op("<@")(period)
+ )
+ self.assert_compile(
+ stmt,
+ "SELECT sum(usages.amount) AS sum_1 FROM usages "
+ "WHERE usages.date <@ %(date_1)s::DATERANGE",
+ )
+
class InsertOnConflictTest(fixtures.TablesTest, AssertsCompiledSQL):
__dialect__ = postgresql.dialect()