Source code for sqlobject.tests.test_sqlbuilder_dbspecific

from__future__import print_function
fromsqlobjectimport BoolCol, SQLObject
fromsqlobject.sqlbuilderimport AND, Alias, EXISTS, JOIN, LEFTJOINOn, \
 Select, sqlrepr
fromsqlobject.tests.dbtestimport setupClass
''' Going to test that complex sqlbuilder constructions are never
 prematurely stringified. A straight-forward approach is to use
 Bools, since postgresql wants special formatting in queries.
 The test is whether a call to sqlrepr(x, 'postgres') includes
 the appropriate bool formatting throughout.
'''
[docs] classSBButton(SQLObject): activated = BoolCol()
[docs] defmakeClause(): # It's not a comparison, it's an SQLExpression return SBButton.q.activated == True # noqa
[docs] defmakeSelect(): return Select(SBButton.q.id, clause=makeClause())
[docs] defcheckCount(q, c, msg=''): print("STRING:", str(q)) print("POSTGR:", sqlrepr(q, 'postgres')) assert sqlrepr(q, 'postgres').count("'t'") == c and \ sqlrepr(q, 'postgres') != str(q), msg
[docs] deftestSimple(): setupClass(SBButton) checkCount(makeClause(), 1) checkCount(makeSelect(), 1)
[docs] deftestMiscOps(): setupClass(SBButton) checkCount(AND(makeClause(), makeClause()), 2) checkCount(AND(makeClause(), EXISTS(makeSelect())), 2)
[docs] deftestAliased(): setupClass(SBButton) b = Alias(makeSelect(), 'b') checkCount(b, 1) checkCount(Select(b.q.id), 1) # Table1 & Table2 are treated individually in joins checkCount(JOIN(None, b), 1) checkCount(JOIN(b, SBButton), 1) checkCount(JOIN(SBButton, b), 1) checkCount(LEFTJOINOn(None, b, SBButton.q.id == b.q.id), 1) checkCount(LEFTJOINOn(b, SBButton, SBButton.q.id == b.q.id), 1) checkCount(LEFTJOINOn(SBButton, b, SBButton.q.id == b.q.id), 1)
[docs] deftestTablesUsedSResults(): setupClass(SBButton) checkCount(SBButton.select(makeClause()).queryForSelect(), 1)