def test_join(self): t1 = Table('t1') t2 = Table('t2') join = Join(t1, t2) with AliasManager(): self.assertEqual(str(join), '"t1" AS "a" INNER JOIN "t2" AS "b"') self.assertEqual(join.params, ()) join.condition = t1.c == t2.c with AliasManager(): self.assertEqual(str(join), '"t1" AS "a" INNER JOIN "t2" AS "b" ON ("a"."c" = "b"."c")')
def func1(self): try: with AliasManager(): a1 = AliasManager.get(self.t1) a2 = AliasManager.get(self.t2) self.synchro.wait() self.assertEqual(a1, AliasManager.get(self.t1)) self.assertEqual(a2, AliasManager.get(self.t2)) self.succeed1.set() return except Exception: pass finally: self.finish1.set()
def func2(self): try: with AliasManager(exclude=[self.t2]): a2 = AliasManager.get(self.t2) a1 = AliasManager.get(self.t1) self.synchro.set() self.assertEqual(a1, AliasManager.get(self.t1)) self.assertEqual(a2, AliasManager.get(self.t2)) self.succeed2.set() return except Exception: pass finally: self.synchro.set() self.finish2.set()
def test_window(self): t = Table('t') function = Rank(t.c, window=Window([])) with AliasManager(): self.assertEqual(str(function), 'RANK("a"."c") OVER "b"') self.assertEqual(function.params, ())
def test_column(self): column = Column(Table('t'), 'c') self.assertEqual(str(column), '"c"') self.assertEqual(column.name, 'c') with AliasManager(): self.assertEqual(str(column), '"a"."c"')
def _test_column_with_quote(self, quote): column = Column(Table('t'), 'c') self.assertEqual(str(column), quote + 'c' + quote) self.assertEqual(column.name, 'c') with AliasManager(): self.assertEqual(str(column), '%sa%s.%sc%s' % (quote, quote, quote, quote))
def test_filter(self): t = Table('t') function = Rank(t.c, filter_=t.c > 0, window=Window([])) with AliasManager(): self.assertEqual( str(function), 'RANK("a"."c") FILTER (WHERE ("a"."c" > %s)) OVER "b"') self.assertEqual(function.params, (0, ))
def test_with_columns(self): with AliasManager(): second = With('a', query=self.table.select(self.table.a)) self.assertEqual( second.statement(), '"a" ("a") AS (' 'SELECT "b"."a" FROM "t" AS "b"' ')') self.assertEqual(second.statement_params(), ())
def test_with(self): with AliasManager(): simple = With(query=self.table.select(self.table.id, where=self.table.id == 1)) self.assertEqual( simple.statement(), '"a" AS (' 'SELECT "b"."id" FROM "t" AS "b" WHERE ("b"."id" = %s)' ')') self.assertEqual(simple.statement_params(), (1, ))
def test_join_subselect(self): t1 = Table('t1') t2 = Table('t2') select = t2.select() join = Join(t1, select) join.condition = t1.c == select.c with AliasManager(): self.assertEqual(str(join), '"t1" AS "a" INNER JOIN (SELECT * FROM "t2" AS "c") AS "b" ' 'ON ("a"."c" = "b"."c")') self.assertEqual(join.params, ())
def test_with_query(self): with AliasManager(): simple = With() simple.query = self.table.select(self.table.id, where=self.table.id == 1) second = With() second.query = simple.select() wq = WithQuery(with_=[simple, second]) self.assertEqual( wq._with_str(), 'WITH "a" AS (' 'SELECT "b"."id" FROM "t" AS "b" WHERE ("b"."id" = %s)' '), "c" AS (' 'SELECT * FROM "a" AS "a"' ') ') self.assertEqual(wq._with_params(), (1, ))
def test_window(self): avg = Avg(self.table.c, window=Window([])) with AliasManager(): self.assertEqual(str(avg), 'AVG("a"."c") OVER "b"') self.assertEqual(avg.params, ())
def test_join_function(self): t1 = Table('t1') join = Join(t1, Now()) with AliasManager(): self.assertEqual(str(join), '"t1" AS "a" INNER JOIN NOW() AS "b"') self.assertEqual(join.params, ())