1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
from __future__ import print_function
from sqlobject import BoolCol, SQLObject
from sqlobject.sqlbuilder import AND, Alias, EXISTS, JOIN, LEFTJOINOn, \
Select, sqlrepr
from sqlobject.tests.dbtest import 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.
'''
class SBButton(SQLObject):
activated = BoolCol()
def makeClause():
# It's not a comparison, it's an SQLExpression
return SBButton.q.activated == True # noqa
def makeSelect():
return Select(SBButton.q.id, clause=makeClause())
def checkCount(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
def testSimple():
setupClass(SBButton)
yield checkCount, makeClause(), 1
yield checkCount, makeSelect(), 1
def testMiscOps():
setupClass(SBButton)
yield checkCount, AND(makeClause(), makeClause()), 2
yield checkCount, AND(makeClause(), EXISTS(makeSelect())), 2
def testAliased():
setupClass(SBButton)
b = Alias(makeSelect(), 'b')
yield checkCount, b, 1
yield checkCount, Select(b.q.id), 1
# Table1 & Table2 are treated individually in joins
yield checkCount, JOIN(None, b), 1
yield checkCount, JOIN(b, SBButton), 1
yield checkCount, JOIN(SBButton, b), 1
yield checkCount, LEFTJOINOn(None, b, SBButton.q.id == b.q.id), 1
yield checkCount, LEFTJOINOn(b, SBButton, SBButton.q.id == b.q.id), 1
yield checkCount, LEFTJOINOn(SBButton, b, SBButton.q.id == b.q.id), 1
def testTablesUsedSResults():
setupClass(SBButton)
yield checkCount, SBButton.select(makeClause()).queryForSelect(), 1
|