forked from webpy/webpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
183 lines (147 loc) · 5.58 KB
/
db.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""DB test"""
import webtest
import web
class DBTest(webtest.TestCase):
dbname = 'postgres'
driver = None
def setUp(self):
self.db = webtest.setup_database(self.dbname, driver=self.driver)
self.db.query("CREATE TABLE person (name text, email text, active boolean)")
def tearDown(self):
# there might be some error with the current connection, delete from a new connection
self.db = webtest.setup_database(self.dbname, driver=self.driver)
self.db.query('DROP TABLE person')
def _testable(self):
try:
webtest.setup_database(self.dbname, driver=self.driver)
return True
except ImportError, e:
print >> web.debug, str(e), "(ignoring %s)" % self.__class__.__name__
return False
def testUnicode(self):
# Bug#177265: unicode queries throw errors
self.db.select('person', where='name=$name', vars={'name': u'\xf4'})
def assertRows(self, n):
result = self.db.select('person')
self.assertEquals(len(list(result)), n)
def testCommit(self):
t = self.db.transaction()
self.db.insert('person', False, name='user1')
t.commit()
t = self.db.transaction()
self.db.insert('person', False, name='user2')
self.db.insert('person', False, name='user3')
t.commit()
self.assertRows(3)
def testRollback(self):
t = self.db.transaction()
self.db.insert('person', False, name='user1')
self.db.insert('person', False, name='user2')
self.db.insert('person', False, name='user3')
t.rollback()
self.assertRows(0)
def testWrongQuery(self):
# It should be possible to run a correct query after getting an error from a wrong query.
try:
self.db.select('notthere')
except:
pass
self.db.select('person')
def testNestedTransactions(self):
t1 = self.db.transaction()
self.db.insert('person', False, name='user1')
self.assertRows(1)
t2 = self.db.transaction()
self.db.insert('person', False, name='user2')
self.assertRows(2)
t2.rollback()
self.assertRows(1)
t3 = self.db.transaction()
self.db.insert('person', False, name='user3')
self.assertRows(2)
t3.commit()
t1.commit()
self.assertRows(2)
def testPooling(self):
# can't test pooling if DBUtils is not installed
try:
import DBUtils
except ImportError:
return
db = webtest.setup_database(self.dbname, pooling=True)
self.assertEquals(db.ctx.db.__class__.__module__, 'DBUtils.PooledDB')
db.select('person', limit=1)
def test_multiple_insert(self):
db = webtest.setup_database(self.dbname)
db.multiple_insert('person', [dict(name='a'), dict(name='b')], seqname=False)
assert db.select("person", where="name='a'").list()
assert db.select("person", where="name='b'").list()
def test_result_is_unicode(self):
db = webtest.setup_database(self.dbname)
self.db.insert('person', False, name='user')
name = db.select('person')[0].name
self.assertEquals(type(name), unicode)
def test_result_is_true(self):
db = webtest.setup_database(self.dbname)
self.db.insert('person', False, name='user')
self.assertEquals(bool(db.select('person')), True)
def testBoolean(self):
def t(active):
name ='name-%s' % active
self.db.insert('person', False, name=name, active=active)
a = self.db.select('person', where='name=$name', vars=locals())[0].active
self.assertEquals(a, active)
t(False)
t(True)
def test_insert_default_values(self):
db = webtest.setup_database(self.dbname)
db.insert("person")
def test_where(self):
db = webtest.setup_database(self.dbname)
db.insert("person", False, name="Foo")
d = db.where("person", name="Foo").list()
assert len(d) == 1
d = db.where("person").list()
assert len(d) == 1
class PostgresTest(DBTest):
dbname = "postgres"
driver = "psycopg2"
class PostgresTest_psycopg(PostgresTest):
driver = "psycopg"
class PostgresTest_pgdb(PostgresTest):
driver = "pgdb"
class SqliteTest(DBTest):
dbname = "sqlite"
driver = "sqlite3"
def testNestedTransactions(self):
#nested transactions does not work with sqlite
pass
def testPooling(self):
# pooling is not support for sqlite
pass
class SqliteTest_pysqlite2(SqliteTest):
driver = "pysqlite2.dbapi2"
class MySQLTest(DBTest):
dbname = "mysql"
def setUp(self):
self.db = webtest.setup_database(self.dbname)
# In mysql, transactions are supported only with INNODB engine.
self.db.query("CREATE TABLE person (name text, email text) ENGINE=INNODB")
def testBoolean(self):
# boolean datatype is not suppoted in MySQL (at least until v5.0)
pass
del DBTest
def is_test(cls):
import inspect
return inspect.isclass(cls) and webtest.TestCase in inspect.getmro(cls)
# ignore db tests when the required db adapter is not found.
for t in globals().values():
if is_test(t) and not t('_testable')._testable():
del globals()[t.__name__]
del t
try:
import DBUtils
except ImportError, e:
print >> web.debug, str(e) + "(ignoring testPooling)"
if __name__ == '__main__':
webtest.main()