summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2017-04-10 14:59:14 -0400
committerGerrit Code Review <gerrit@awstats.zzzcomputing.com>2017-04-10 14:59:14 -0400
commit5b81dbcfa3888de65fc33b247353b38488199b00 (patch)
treead184ab166e1b41d400cb809e164d583f0eaf493 /test
parent7d9f241d63b76cf3d4a5f1c146554cd9dc140656 (diff)
parent86c43d2e0f0c7c28b933f470678571077c5d5035 (diff)
downloadsqlalchemy-5b81dbcfa3888de65fc33b247353b38488199b00.tar.gz
Merge "Compare entities also on chop_path"
Diffstat (limited to 'test')
-rw-r--r--test/orm/test_eager_relations.py159
-rw-r--r--test/orm/test_options.py48
2 files changed, 207 insertions, 0 deletions
diff --git a/test/orm/test_eager_relations.py b/test/orm/test_eager_relations.py
index 0d324b3ff..3c669d90d 100644
--- a/test/orm/test_eager_relations.py
+++ b/test/orm/test_eager_relations.py
@@ -4362,6 +4362,165 @@ class EntityViaMultiplePathTestTwo(fixtures.DeclarativeMappedTest):
)
+class LazyLoadOptSpecificityTest(fixtures.DeclarativeMappedTest):
+ """test for [ticket:3963]"""
+
+ @classmethod
+ def setup_classes(cls):
+ Base = cls.DeclarativeBasic
+
+ class A(Base):
+ __tablename__ = 'a'
+ id = Column(Integer, primary_key=True)
+ bs = relationship("B")
+
+ class B(Base):
+ __tablename__ = 'b'
+ id = Column(Integer, primary_key=True)
+ a_id = Column(ForeignKey('a.id'))
+ cs = relationship("C")
+
+ class C(Base):
+ __tablename__ = 'c'
+ id = Column(Integer, primary_key=True)
+ b_id = Column(ForeignKey('b.id'))
+
+ @classmethod
+ def insert_data(cls):
+ A, B, C = cls.classes("A", "B", "C")
+ s = Session()
+ s.add(A(id=1, bs=[B(cs=[C()])]))
+ s.add(A(id=2))
+ s.commit()
+
+ def _run_tests(self, query, expected):
+ def go():
+ for a, _ in query:
+ for b in a.bs:
+ b.cs
+ self.assert_sql_count(testing.db, go, expected)
+
+ def test_string_options_aliased_whatever(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(
+ joinedload("bs").joinedload("cs"))
+ self._run_tests(q, 1)
+
+ def test_string_options_unaliased_whatever(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(A, aa).filter(
+ aa.id == 2).filter(A.id == 1).options(
+ joinedload("bs").joinedload("cs"))
+ self._run_tests(q, 1)
+
+ def test_lazyload_aliased_abs_bcs_one(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(
+ joinedload(A.bs).joinedload(B.cs))
+ self._run_tests(q, 3)
+
+ def test_lazyload_aliased_abs_bcs_two(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(
+ defaultload(A.bs).joinedload(B.cs))
+ self._run_tests(q, 3)
+
+ def test_pathed_lazyload_aliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ opt = Load(A).joinedload(A.bs).joinedload(B.cs)
+
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(opt)
+ self._run_tests(q, 3)
+
+ def test_pathed_lazyload_plus_joined_aliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ opt = Load(aa).defaultload(aa.bs).joinedload(B.cs)
+
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(opt)
+ self._run_tests(q, 2)
+
+ def test_pathed_joinedload_aliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ opt = Load(aa).joinedload(aa.bs).joinedload(B.cs)
+
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(opt)
+ self._run_tests(q, 1)
+
+ def test_lazyload_plus_joined_aliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(
+ defaultload(aa.bs).joinedload(B.cs))
+ self._run_tests(q, 2)
+
+ def test_joinedload_aliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(aa, A).filter(
+ aa.id == 1).filter(A.id == 2).options(
+ joinedload(aa.bs).joinedload(B.cs))
+ self._run_tests(q, 1)
+
+ def test_lazyload_unaliased_abs_bcs_one(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(A, aa).filter(
+ aa.id == 2).filter(A.id == 1).options(
+ joinedload(aa.bs).joinedload(B.cs))
+ self._run_tests(q, 3)
+
+ def test_lazyload_unaliased_abs_bcs_two(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(A, aa).filter(
+ aa.id == 2).filter(A.id == 1).options(
+ defaultload(aa.bs).joinedload(B.cs))
+ self._run_tests(q, 3)
+
+ def test_lazyload_plus_joined_unaliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(A, aa).filter(
+ aa.id == 2).filter(A.id == 1).options(
+ defaultload(A.bs).joinedload(B.cs))
+ self._run_tests(q, 2)
+
+ def test_joinedload_unaliased_abs_bcs(self):
+ A, B, C = self.classes("A", "B", "C")
+ s = Session()
+ aa = aliased(A)
+ q = s.query(A, aa).filter(
+ aa.id == 2).filter(A.id == 1).options(
+ joinedload(A.bs).joinedload(B.cs))
+ self._run_tests(q, 1)
+
+
class EntityViaMultiplePathTestThree(fixtures.DeclarativeMappedTest):
"""test for [ticket:3811] continuing on [ticket:3431]"""
diff --git a/test/orm/test_options.py b/test/orm/test_options.py
index 556f73c4b..b7c574e2a 100644
--- a/test/orm/test_options.py
+++ b/test/orm/test_options.py
@@ -306,6 +306,54 @@ class OptionsTest(PathTest, QueryTest):
opt = self._option_fixture(Order.items, Item.keywords)
self._assert_path_result(opt, q, [])
+ def test_with_current_nonmatching_entity(self):
+ Item, User, Order = (self.classes.Item,
+ self.classes.User,
+ self.classes.Order)
+
+ sess = Session()
+ q = sess.query(Item)._with_current_path(
+ self._make_path_registry(
+ [inspect(aliased(User)), 'orders', Order, 'items'])
+ )
+
+ opt = self._option_fixture(User.orders)
+ self._assert_path_result(opt, q, [])
+
+ opt = self._option_fixture(User.orders, Order.items, Item.keywords)
+ self._assert_path_result(opt, q, [])
+
+ q = sess.query(Item)._with_current_path(
+ self._make_path_registry(
+ [User, 'orders', Order, 'items'])
+ )
+
+ ac = aliased(User)
+
+ opt = self._option_fixture(ac.orders)
+ self._assert_path_result(opt, q, [])
+
+ opt = self._option_fixture(ac.orders, Order.items, Item.keywords)
+ self._assert_path_result(opt, q, [])
+
+ def test_with_current_match_aliased_classes(self):
+ Item, User, Order = (self.classes.Item,
+ self.classes.User,
+ self.classes.Order)
+
+ ac = aliased(User)
+ sess = Session()
+ q = sess.query(Item)._with_current_path(
+ self._make_path_registry(
+ [inspect(ac), 'orders', Order, 'items'])
+ )
+
+ opt = self._option_fixture(ac.orders, Order.items, Item.keywords)
+ self._assert_path_result(opt, q, [(Item, "keywords")])
+
+ opt = self._option_fixture(ac.orders, Order.items)
+ self._assert_path_result(opt, q, [])
+
def test_from_base_to_subclass_attr(self):
Dingaling, Address = self.classes.Dingaling, self.classes.Address