MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_correlated_subquery

Method test_correlated_subquery

test/orm/test_froms.py:1799–1844  ·  view source on GitHub ↗

test that a subquery constructed from ORM attributes doesn't leak out those entities to the outermost query.

(self)

Source from the content-addressed store, hash-verified

1797 )
1798
1799 def test_correlated_subquery(self):
1800 """test that a subquery constructed from ORM attributes doesn't leak
1801 out those entities to the outermost query."""
1802
1803 Address, users, User = (
1804 self.classes.Address,
1805 self.tables.users,
1806 self.classes.User,
1807 )
1808
1809 sess = fixture_session()
1810
1811 subq = (
1812 select(func.count())
1813 .where(User.id == Address.user_id)
1814 .correlate(users)
1815 .label("count")
1816 )
1817
1818 # we don't want Address to be outside of the subquery here
1819 eq_(
1820 list(sess.query(User, subq)[0:3]),
1821 [
1822 (User(id=7, name="jack"), 1),
1823 (User(id=8, name="ed"), 3),
1824 (User(id=9, name="fred"), 1),
1825 ],
1826 )
1827
1828 # same thing without the correlate, as it should
1829 # not be needed
1830 subq = (
1831 select(func.count())
1832 .where(User.id == Address.user_id)
1833 .label("count")
1834 )
1835
1836 # we don't want Address to be outside of the subquery here
1837 eq_(
1838 list(sess.query(User, subq)[0:3]),
1839 [
1840 (User(id=7, name="jack"), 1),
1841 (User(id=8, name="ed"), 3),
1842 (User(id=9, name="fred"), 1),
1843 ],
1844 )
1845
1846 @testing.combinations((True,), (False,))
1847 def test_no_uniquing_cols_legacy(self, with_entities):

Callers

nothing calls this directly

Calls 9

fixture_sessionFunction · 0.90
selectFunction · 0.90
eq_Function · 0.90
UserClass · 0.70
labelMethod · 0.45
correlateMethod · 0.45
whereMethod · 0.45
countMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected