Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 184 lines (147 sloc) 5.71 kB
dd6e1af @anandology web.py test suite.
anandology authored
1 """DB test"""
2 import webtest
3 import web
4
5 class DBTest(webtest.TestCase):
6 dbname = 'postgres'
88c4669 @anandology test DB classes with each driver separately.
anandology authored
7 driver = None
dd6e1af @anandology web.py test suite.
anandology authored
8
f54e168 @anandology Database transactions
anandology authored
9 def setUp(self):
88c4669 @anandology test DB classes with each driver separately.
anandology authored
10 self.db = webtest.setup_database(self.dbname, driver=self.driver)
2e6671c @anandology add testBoolean
anandology authored
11 self.db.query("CREATE TABLE person (name text, email text, active boolean)")
dd6e1af @anandology web.py test suite.
anandology authored
12
f54e168 @anandology Database transactions
anandology authored
13 def tearDown(self):
dd6e1af @anandology web.py test suite.
anandology authored
14 # there might be some error with the current connection, delete from a new connection
88c4669 @anandology test DB classes with each driver separately.
anandology authored
15 self.db = webtest.setup_database(self.dbname, driver=self.driver)
dd6e1af @anandology web.py test suite.
anandology authored
16 self.db.query('DROP TABLE person')
3ba0911 @anandology unicodify web.input
anandology authored
17
18 def _testable(self):
19 try:
88c4669 @anandology test DB classes with each driver separately.
anandology authored
20 webtest.setup_database(self.dbname, driver=self.driver)
3ba0911 @anandology unicodify web.input
anandology authored
21 return True
22 except ImportError, e:
88c4669 @anandology test DB classes with each driver separately.
anandology authored
23 print >> web.debug, str(e), "(ignoring %s)" % self.__class__.__name__
3ba0911 @anandology unicodify web.input
anandology authored
24 return False
dd6e1af @anandology web.py test suite.
anandology authored
25
26 def testUnicode(self):
88c4669 @anandology test DB classes with each driver separately.
anandology authored
27 # Bug#177265: unicode queries throw errors
dd6e1af @anandology web.py test suite.
anandology authored
28 self.db.select('person', where='name=$name', vars={'name': u'\xf4'})
f54e168 @anandology Database transactions
anandology authored
29
30 def assertRows(self, n):
31 result = self.db.select('person')
32 self.assertEquals(len(list(result)), n)
33
34 def testCommit(self):
35 t = self.db.transaction()
36 self.db.insert('person', False, name='user1')
37 t.commit()
38
39 t = self.db.transaction()
40 self.db.insert('person', False, name='user2')
41 self.db.insert('person', False, name='user3')
42 t.commit()
43
44 self.assertRows(3)
45
46 def testRollback(self):
47 t = self.db.transaction()
48 self.db.insert('person', False, name='user1')
49 self.db.insert('person', False, name='user2')
50 self.db.insert('person', False, name='user3')
51 t.rollback()
52 self.assertRows(0)
53
54 def testWrongQuery(self):
55 # It should be possible to run a correct query after getting an error from a wrong query.
56 try:
57 self.db.select('notthere')
58 except:
59 pass
60 self.db.select('person')
61
62 def testNestedTransactions(self):
63 t1 = self.db.transaction()
64 self.db.insert('person', False, name='user1')
65 self.assertRows(1)
66
67 t2 = self.db.transaction()
68 self.db.insert('person', False, name='user2')
69 self.assertRows(2)
70 t2.rollback()
71 self.assertRows(1)
72 t3 = self.db.transaction()
73 self.db.insert('person', False, name='user3')
74 self.assertRows(2)
75 t3.commit()
76 t1.commit()
77 self.assertRows(2)
757601d db pooling (Bug#212430)
Anand authored
78
79 def testPooling(self):
b0df4ed @anandology ignore testPooling when DBUtils module is not available.
anandology authored
80 # can't test pooling if DBUtils is not installed
81 try:
82 import DBUtils
83 except ImportError:
84 return
d2a1c0b enable pooling by default
Anand authored
85 db = webtest.setup_database(self.dbname, pooling=True)
479ee3d fixed DB Pooling. changed db.hasPooling to db.has_pooling
Anand authored
86 self.assertEquals(db.ctx.db.__class__.__module__, 'DBUtils.PooledDB')
87 db.select('person', limit=1)
780f52f new: db.multiple_insert
Anand authored
88
89 def test_multiple_insert(self):
90 db = webtest.setup_database(self.dbname)
91 db.multiple_insert('person', [dict(name='a'), dict(name='b')], seqname=False)
92
e608612 @anandology fix bad assert in db test
anandology authored
93 assert db.select("person", where="name='a'").list()
94 assert db.select("person", where="name='b'").list()
780f52f new: db.multiple_insert
Anand authored
95
88c4669 @anandology test DB classes with each driver separately.
anandology authored
96 def test_result_is_unicode(self):
18b6b3f testcase to test unicode in db result
Anand authored
97 db = webtest.setup_database(self.dbname)
98 self.db.insert('person', False, name='user')
99 name = db.select('person')[0].name
100 self.assertEquals(type(name), unicode)
101
9e5e404 @anandology with better iterbetter, bool test can be done on sqlite results too
anandology authored
102 def test_result_is_true(self):
103 db = webtest.setup_database(self.dbname)
104 self.db.insert('person', False, name='user')
105 self.assertEquals(bool(db.select('person')), True)
106
2e6671c @anandology add testBoolean
anandology authored
107 def testBoolean(self):
108 def t(active):
109 name ='name-%s' % active
110 self.db.insert('person', False, name=name, active=active)
111 a = self.db.select('person', where='name=$name', vars=locals())[0].active
112 self.assertEquals(a, active)
113 t(False)
114 t(True)
b1417f5 @anandology Fix inserting default values into MySQL. (fixes #49)
anandology authored
115
116 def test_insert_default_values(self):
117 db = webtest.setup_database(self.dbname)
118 db.insert("person")
cfedd35 @anandology Added a test for db.where.
anandology authored
119
120 def test_where(self):
121 db = webtest.setup_database(self.dbname)
122 db.insert("person", False, name="Foo")
123 d = db.where("person", name="Foo").list()
124 assert len(d) == 1
125 d = db.where("person").list()
126 assert len(d) == 1
2e6671c @anandology add testBoolean
anandology authored
127
88c4669 @anandology test DB classes with each driver separately.
anandology authored
128 class PostgresTest(DBTest):
129 dbname = "postgres"
130 driver = "psycopg2"
131
132 class PostgresTest_psycopg(PostgresTest):
133 driver = "psycopg"
134
135 class PostgresTest_pgdb(PostgresTest):
136 driver = "pgdb"
137
dd6e1af @anandology web.py test suite.
anandology authored
138 class SqliteTest(DBTest):
139 dbname = "sqlite"
88c4669 @anandology test DB classes with each driver separately.
anandology authored
140 driver = "sqlite3"
dd6e1af @anandology web.py test suite.
anandology authored
141
f54e168 @anandology Database transactions
anandology authored
142 def testNestedTransactions(self):
143 #nested transactions does not work with sqlite
144 pass
88c4669 @anandology test DB classes with each driver separately.
anandology authored
145
719a78b @anandology Disabled testPooling for sqlite.
anandology authored
146 def testPooling(self):
147 # pooling is not support for sqlite
148 pass
149
88c4669 @anandology test DB classes with each driver separately.
anandology authored
150 class SqliteTest_pysqlite2(SqliteTest):
151 driver = "pysqlite2.dbapi2"
152
dd6e1af @anandology web.py test suite.
anandology authored
153 class MySQLTest(DBTest):
154 dbname = "mysql"
f54e168 @anandology Database transactions
anandology authored
155
156 def setUp(self):
157 self.db = webtest.setup_database(self.dbname)
158 # In mysql, transactions are supported only with INNODB engine.
159 self.db.query("CREATE TABLE person (name text, email text) ENGINE=INNODB")
dd6e1af @anandology web.py test suite.
anandology authored
160
2e6671c @anandology add testBoolean
anandology authored
161 def testBoolean(self):
162 # boolean datatype is not suppoted in MySQL (at least until v5.0)
163 pass
164
88c4669 @anandology test DB classes with each driver separately.
anandology authored
165 del DBTest
166
167 def is_test(cls):
168 import inspect
169 return inspect.isclass(cls) and webtest.TestCase in inspect.getmro(cls)
3ba0911 @anandology unicodify web.input
anandology authored
170
171 # ignore db tests when the required db adapter is not found.
88c4669 @anandology test DB classes with each driver separately.
anandology authored
172 for t in globals().values():
173 if is_test(t) and not t('_testable')._testable():
3ba0911 @anandology unicodify web.input
anandology authored
174 del globals()[t.__name__]
88c4669 @anandology test DB classes with each driver separately.
anandology authored
175 del t
3ba0911 @anandology unicodify web.input
anandology authored
176
b0df4ed @anandology ignore testPooling when DBUtils module is not available.
anandology authored
177 try:
178 import DBUtils
179 except ImportError, e:
180 print >> web.debug, str(e) + "(ignoring testPooling)"
181
dd6e1af @anandology web.py test suite.
anandology authored
182 if __name__ == '__main__':
183 webtest.main()
Something went wrong with that request. Please try again.