+import itertools
+
+from sqlalchemy import ForeignKey
from .. import AssertsCompiledSQL
from .. import AssertsExecutionResults
from .. import config
)
+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
from sqlalchemy import between
from sqlalchemy import exc
from sqlalchemy import Integer
+from sqlalchemy import join
from sqlalchemy import LargeBinary
from sqlalchemy import literal_column
from sqlalchemy import not_
or_(false(), true()), "1 = 1", dialect=self._dialect(False)
)
+ def test_seven_a(self):
+ t1 = table("t1", column("a"))
+ t2 = table("t2", column("b"))
+ self.assert_compile(
+ join(t1, t2, onclause=true()),
+ "t1 JOIN t2 ON 1 = 1",
+ dialect=self._dialect(False),
+ )
+
+ def test_seven_b(self):
+ t1 = table("t1", column("a"))
+ t2 = table("t2", column("b"))
+ self.assert_compile(
+ join(t1, t2, onclause=false()),
+ "t1 JOIN t2 ON 0 = 1",
+ dialect=self._dialect(False),
+ )
+
+ def test_seven_c(self):
+ t1 = table("t1", column("a"))
+ t2 = table("t2", column("b"))
+ self.assert_compile(
+ join(t1, t2, onclause=true()),
+ "t1 JOIN t2 ON true",
+ dialect=self._dialect(True),
+ )
+
+ def test_seven_d(self):
+ t1 = table("t1", column("a"))
+ t2 = table("t2", column("b"))
+ self.assert_compile(
+ join(t1, t2, onclause=false()),
+ "t1 JOIN t2 ON false",
+ dialect=self._dialect(True),
+ )
+
def test_eight(self):
self.assert_compile(
and_(false(), true()), "false", dialect=self._dialect(True)