1from sqlobject import *
2from sqlobject.sqlbuilder import *
3from sqlobject.tests.dbtest import *
4
5''' Going to test that complex sqlbuilder constructions are never
6    prematurely stringified. A straight-forward approach is to use
7    Bools, since postgresql wants special formatting in queries.
8    The test is whether a call to sqlrepr(x, 'postgres') includes
9    the appropriate bool formatting throughout.
10'''
11
12class SBButton(SQLObject):
13    activated = BoolCol()
14
15def makeClause():
16    return SBButton.q.activated==True
17
18def makeSelect():
19    return Select(SBButton.q.id, clause=makeClause())
20
21def checkCount(q, c, msg=''):
22    print "STRING:", str(q)
23    print "POSTGR:", sqlrepr(q, 'postgres')
24    assert sqlrepr(q, 'postgres').count("'t'") == c and sqlrepr(q, 'postgres') != str(q), msg
25
26def testSimple():
27    setupClass(SBButton)
28    yield checkCount, makeClause(), 1
29    yield checkCount, makeSelect(), 1
30
31def testMiscOps():
32    setupClass(SBButton)
33    yield checkCount, AND(makeClause(), makeClause()), 2
34    yield checkCount, AND(makeClause(), EXISTS(makeSelect())), 2
35
36def testAliased():
37    setupClass(SBButton)
38    b = Alias(makeSelect(), 'b')
39    yield checkCount, b, 1
40    yield checkCount, Select(b.q.id), 1
41
42    # Table1 & Table2 are treated individually in joins
43    yield checkCount, JOIN(None, b), 1
44    yield checkCount, JOIN(b, SBButton), 1
45    yield checkCount, JOIN(SBButton, b), 1
46    yield checkCount, LEFTJOINOn(None, b, SBButton.q.id==b.q.id), 1
47    yield checkCount, LEFTJOINOn(b, SBButton, SBButton.q.id==b.q.id), 1
48    yield checkCount, LEFTJOINOn(SBButton, b, SBButton.q.id==b.q.id), 1
49
50def testTablesUsedSResults():
51    setupClass(SBButton)
52
53    yield checkCount, SBButton.select(makeClause()).queryForSelect(), 1
54