Skip to content
This repository
Newer
Older
100644 93 lines (74 sloc) 2.876 kb
dd6e1af5 »
2007-12-24 web.py test suite.
1 """DB test"""
2 import webtest
3 import web
4
5 class DBTest(webtest.TestCase):
6 dbname = 'postgres'
7
f54e1687 »
2008-01-04 Database transactions
8 def setUp(self):
dd6e1af5 »
2007-12-24 web.py test suite.
9 self.db = webtest.setup_database(self.dbname)
10 self.db.query("CREATE TABLE person (name text, email text)")
11
f54e1687 »
2008-01-04 Database transactions
12 def tearDown(self):
dd6e1af5 »
2007-12-24 web.py test suite.
13 # there might be some error with the current connection, delete from a new connection
14 self.db = webtest.setup_database(self.dbname)
15 self.db.query('DROP TABLE person')
16
17 def testUnicode(self):
18 """Bug#177265: unicode queries throw errors"""
19 self.db.select('person', where='name=$name', vars={'name': u'\xf4'})
f54e1687 »
2008-01-04 Database transactions
20
21 def assertRows(self, n):
22 result = self.db.select('person')
23 self.assertEquals(len(list(result)), n)
24
25 def testCommit(self):
26 t = self.db.transaction()
27 self.db.insert('person', False, name='user1')
28 t.commit()
29
30 t = self.db.transaction()
31 self.db.insert('person', False, name='user2')
32 self.db.insert('person', False, name='user3')
33 t.commit()
34
35 self.assertRows(3)
36
37 def testRollback(self):
38 t = self.db.transaction()
39 self.db.insert('person', False, name='user1')
40 self.db.insert('person', False, name='user2')
41 self.db.insert('person', False, name='user3')
42 t.rollback()
43 self.assertRows(0)
44
45 def testWrongQuery(self):
46 # It should be possible to run a correct query after getting an error from a wrong query.
47 try:
48 self.db.select('notthere')
49 except:
50 pass
51 self.db.select('person')
52
53 def testNestedTransactions(self):
54 t1 = self.db.transaction()
55 self.db.insert('person', False, name='user1')
56 self.assertRows(1)
57
58 t2 = self.db.transaction()
59 self.db.insert('person', False, name='user2')
60 self.assertRows(2)
61 t2.rollback()
62 self.assertRows(1)
63 t3 = self.db.transaction()
64 self.db.insert('person', False, name='user3')
65 self.assertRows(2)
66 t3.commit()
67 t1.commit()
68 self.assertRows(2)
757601d1 »
2008-04-17 db pooling (Bug#212430)
69
70 def testPooling(self):
71 db = webtest.setup_database(self.dbname)
72 db.hasPooling = True
73 import DBUtils
74 self.assertTrue(isinstance(db.ctx.db, DBUtils.PooledDB.PooledDB))
75
dd6e1af5 »
2007-12-24 web.py test suite.
76 class SqliteTest(DBTest):
77 dbname = "sqlite"
78
f54e1687 »
2008-01-04 Database transactions
79 def testNestedTransactions(self):
80 #nested transactions does not work with sqlite
81 pass
82
dd6e1af5 »
2007-12-24 web.py test suite.
83 class MySQLTest(DBTest):
84 dbname = "mysql"
f54e1687 »
2008-01-04 Database transactions
85
86 def setUp(self):
87 self.db = webtest.setup_database(self.dbname)
88 # In mysql, transactions are supported only with INNODB engine.
89 self.db.query("CREATE TABLE person (name text, email text) ENGINE=INNODB")
dd6e1af5 »
2007-12-24 web.py test suite.
90
91 if __name__ == '__main__':
92 webtest.main()
Something went wrong with that request. Please try again.