summaryrefslogtreecommitdiff
path: root/test/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-12-11 19:01:12 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-12-11 22:01:57 -0500
commited20e2f95f52a072d0c6b09af095b4cda0436d38 (patch)
tree340571f1d5bce61f5126a932a7739fee7efb3b77 /test/sql
parent8e9e473dcb76b57a7f0eaa476481cb66a258ea69 (diff)
downloadsqlalchemy-ed20e2f95f52a072d0c6b09af095b4cda0436d38.tar.gz
Fixes for lambda expressions and relationship loaders
Fixed bug in lambda SQL feature, used by ORM :meth:`_orm.with_loader_criteria` as well as available generally in the SQL expression language, where assigning a boolean value True/False to a variable would cause the query-time expression calculation to fail, as it would produce a SQL expression not compatible with a bound value. Fixed issue where the :attr:`_orm.ORMExecuteState.is_relationship_load` parameter would not be set correctly for many lazy loads, all selectinloads, etc. The flag is essential in order to test if options should be added to statements or if they would already have been propagated via relationship loads. Fixes: #5763 Fixes: #5764 Change-Id: I66aafbef193f892ff75ede0670698647b7475482
Diffstat (limited to 'test/sql')
-rw-r--r--test/sql/test_lambdas.py45
-rw-r--r--test/sql/test_utils.py29
2 files changed, 74 insertions, 0 deletions
diff --git a/test/sql/test_lambdas.py b/test/sql/test_lambdas.py
index c283e804e..a70dc0511 100644
--- a/test/sql/test_lambdas.py
+++ b/test/sql/test_lambdas.py
@@ -22,6 +22,7 @@ from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import ne_
from sqlalchemy.testing.assertsql import CompiledSQL
+from sqlalchemy.types import Boolean
from sqlalchemy.types import Integer
from sqlalchemy.types import String
@@ -77,6 +78,41 @@ class DeferredLambdaTest(
checkparams={"global_x_1": 10, "global_y_1": 9},
)
+ def test_boolean_constants(self):
+ t1 = table("t1", column("q"), column("p"))
+
+ def go():
+ xy = True
+ stmt = select(t1).where(lambda: t1.c.q == xy)
+ return stmt
+
+ self.assert_compile(
+ go(), "SELECT t1.q, t1.p FROM t1 WHERE t1.q = :xy_1"
+ )
+
+ def test_execute_boolean(self, boolean_table_fixture, connection):
+ boolean_data = boolean_table_fixture
+
+ connection.execute(
+ boolean_data.insert(),
+ [{"id": 1, "data": True}, {"id": 2, "data": False}],
+ )
+
+ xy = True
+
+ def go():
+ stmt = select(lambda: boolean_data.c.id).where(
+ lambda: boolean_data.c.data == xy
+ )
+ return connection.execute(stmt)
+
+ result = go()
+ eq_(result.all(), [(1,)])
+
+ xy = False
+ result = go()
+ eq_(result.all(), [(2,)])
+
def test_stale_checker_embedded(self):
def go(x):
@@ -761,6 +797,15 @@ class DeferredLambdaTest(
)
return users, addresses
+ @testing.metadata_fixture()
+ def boolean_table_fixture(self, metadata):
+ return Table(
+ "boolean_data",
+ metadata,
+ Column("id", Integer, primary_key=True),
+ Column("data", Boolean),
+ )
+
def test_adapt_select(self, user_address_fixture):
users, addresses = user_address_fixture
diff --git a/test/sql/test_utils.py b/test/sql/test_utils.py
index a4b76f35d..24a149ece 100644
--- a/test/sql/test_utils.py
+++ b/test/sql/test_utils.py
@@ -15,6 +15,7 @@ from sqlalchemy.sql import util as sql_util
from sqlalchemy.testing import assert_raises
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import eq_
+from sqlalchemy.testing import expect_raises_message
from sqlalchemy.testing import fixtures
@@ -57,6 +58,34 @@ class MiscTest(fixtures.TestBase):
{common, calias, subset_select},
)
+ def test_incompatible_options_add_clslevel(self):
+ class opt1(sql_base.CacheableOptions):
+ _cache_key_traversal = []
+ foo = "bar"
+
+ with expect_raises_message(
+ TypeError,
+ "dictionary contains attributes not covered by "
+ "Options class .*opt1.* .*'bar'.*",
+ ):
+ o1 = opt1
+
+ o1 += {"foo": "f", "bar": "b"}
+
+ def test_incompatible_options_add_instancelevel(self):
+ class opt1(sql_base.CacheableOptions):
+ _cache_key_traversal = []
+ foo = "bar"
+
+ o1 = opt1(foo="bat")
+
+ with expect_raises_message(
+ TypeError,
+ "dictionary contains attributes not covered by "
+ "Options class .*opt1.* .*'bar'.*",
+ ):
+ o1 += {"foo": "f", "bar": "b"}
+
def test_options_merge(self):
class opt1(sql_base.CacheableOptions):
_cache_key_traversal = []