-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_sqlbuilder_dbspecific.py
More file actions
66 lines (46 loc) · 1.84 KB
/
Copy pathtest_sqlbuilder_dbspecific.py
File metadata and controls
66 lines (46 loc) · 1.84 KB
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)
checkCount(makeClause(), 1)
checkCount(makeSelect(), 1)
def testMiscOps():
setupClass(SBButton)
checkCount(AND(makeClause(), makeClause()), 2)
checkCount(AND(makeClause(), EXISTS(makeSelect())), 2)
def testAliased():
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)
def testTablesUsedSResults():
setupClass(SBButton)
checkCount(SBButton.select(makeClause()).queryForSelect(), 1)