diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2020-01-18 00:13:37 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2020-01-18 00:13:37 +0000 |
| commit | a54527fb0ef71aca0be9614617f143da27e03f22 (patch) | |
| tree | e80f6f1158f30deaa5e99b5376f013cc8602adb6 /lib/sqlalchemy | |
| parent | 9e31fc74089cf565df5f275d22eb8ae5414d6e45 (diff) | |
| parent | bdbe164d392d41991b64ced0f097930a04a2c420 (diff) | |
| download | sqlalchemy-a54527fb0ef71aca0be9614617f143da27e03f22.tar.gz | |
Merge "apply asbool reduction to the onclause in join()"
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 2 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/suite/test_select.py | 102 |
2 files changed, 103 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 136c9f868..db743f408 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -698,7 +698,7 @@ class Join(FromClause): if onclause is None: self.onclause = self._match_primaries(self.left, self.right) else: - self.onclause = onclause + self.onclause = onclause.self_group(against=operators._asbool) self.isouter = isouter self.full = full diff --git a/lib/sqlalchemy/testing/suite/test_select.py b/lib/sqlalchemy/testing/suite/test_select.py index 9db2daf7a..3b64b0f29 100644 --- a/lib/sqlalchemy/testing/suite/test_select.py +++ b/lib/sqlalchemy/testing/suite/test_select.py @@ -1,3 +1,6 @@ +import itertools + +from sqlalchemy import ForeignKey from .. import AssertsCompiledSQL from .. import AssertsExecutionResults from .. import config @@ -292,6 +295,105 @@ class LimitOffsetTest(fixtures.TablesTest): ) +class JoinTest(fixtures.TablesTest): + __backend__ = True + + def _assert_result(self, select, result, params=()): + eq_(config.db.execute(select, params).fetchall(), result) + + @classmethod + def define_tables(cls, metadata): + Table("a", metadata, Column("id", Integer, primary_key=True)) + Table( + "b", + metadata, + Column("id", Integer, primary_key=True), + Column("a_id", ForeignKey("a.id"), nullable=False), + ) + + @classmethod + def insert_data(cls): + config.db.execute( + cls.tables.a.insert(), + [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}], + ) + + config.db.execute( + cls.tables.b.insert(), + [ + {"id": 1, "a_id": 1}, + {"id": 2, "a_id": 1}, + {"id": 4, "a_id": 2}, + {"id": 5, "a_id": 3}, + ], + ) + + def test_inner_join_fk(self): + a, b = self.tables("a", "b") + + stmt = select([a, b]).select_from(a.join(b)).order_by(a.c.id, b.c.id) + + self._assert_result(stmt, [(1, 1, 1), (1, 2, 1), (2, 4, 2), (3, 5, 3)]) + + def test_inner_join_true(self): + a, b = self.tables("a", "b") + + stmt = ( + select([a, b]) + .select_from(a.join(b, true())) + .order_by(a.c.id, b.c.id) + ) + + self._assert_result( + stmt, + [ + (a, b, c) + for (a,), (b, c) in itertools.product( + [(1,), (2,), (3,), (4,), (5,)], + [(1, 1), (2, 1), (4, 2), (5, 3)], + ) + ], + ) + + def test_inner_join_false(self): + a, b = self.tables("a", "b") + + stmt = ( + select([a, b]) + .select_from(a.join(b, false())) + .order_by(a.c.id, b.c.id) + ) + + self._assert_result(stmt, []) + + def test_outer_join_false(self): + a, b = self.tables("a", "b") + + stmt = ( + select([a, b]) + .select_from(a.outerjoin(b, false())) + .order_by(a.c.id, b.c.id) + ) + + self._assert_result( + stmt, + [ + (1, None, None), + (2, None, None), + (3, None, None), + (4, None, None), + (5, None, None), + ], + ) + + def test_outer_join_fk(self): + a, b = self.tables("a", "b") + + stmt = select([a, b]).select_from(a.join(b)).order_by(a.c.id, b.c.id) + + self._assert_result(stmt, [(1, 1, 1), (1, 2, 1), (2, 4, 2), (3, 5, 3)]) + + class CompoundSelectTest(fixtures.TablesTest): __backend__ = True |
