Skip to content
This repository
Newer
Older
100644 153 lines (121 sloc) 4.768 kb
dd6e1af5 » anandology
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'
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
7 driver = None
dd6e1af5 » anandology
2007-12-24 web.py test suite.
8
f54e1687 » anandology
2008-01-04 Database transactions
9 def setUp(self):
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
10 self.db = webtest.setup_database(self.dbname, driver=self.driver)
2e6671cb » anandology
2009-05-08 add testBoolean
11 self.db.query("CREATE TABLE person (name text, email text, active boolean)")
dd6e1af5 » anandology
2007-12-24 web.py test suite.
12
f54e1687 » anandology
2008-01-04 Database transactions
13 def tearDown(self):
dd6e1af5 » anandology
2007-12-24 web.py test suite.
14 # there might be some error with the current connection, delete from a new connection
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
15 self.db = webtest.setup_database(self.dbname, driver=self.driver)
dd6e1af5 » anandology
2007-12-24 web.py test suite.
16 self.db.query('DROP TABLE person')
3ba0911e » anandology
2008-11-01 unicodify web.input
17
18 def _testable(self):
19 try:
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
20 webtest.setup_database(self.dbname, driver=self.driver)
3ba0911e » anandology
2008-11-01 unicodify web.input
21 return True
22 except ImportError, e:
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
23 print >> web.debug, str(e), "(ignoring %s)" % self.__class__.__name__
3ba0911e » anandology
2008-11-01 unicodify web.input
24 return False
dd6e1af5 » anandology
2007-12-24 web.py test suite.
25
26 def testUnicode(self):
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
27 # Bug#177265: unicode queries throw errors
dd6e1af5 » anandology
2007-12-24 web.py test suite.
28 self.db.select('person', where='name=$name', vars={'name': u'\xf4'})
f54e1687 » anandology
2008-01-04 Database transactions
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)
757601d1 » Anand
2008-04-17 db pooling (Bug#212430)
78
79 def testPooling(self):
d2a1c0bc » Anand
2008-05-27 enable pooling by default
80 db = webtest.setup_database(self.dbname, pooling=True)
479ee3d4 » Anand
2008-05-17 fixed DB Pooling. changed db.hasPooling to db.has_pooling
81 self.assertEquals(db.ctx.db.__class__.__module__, 'DBUtils.PooledDB')
82 db.select('person', limit=1)
780f52f6 » Anand
2008-05-01 new: db.multiple_insert
83
84 def test_multiple_insert(self):
85 db = webtest.setup_database(self.dbname)
86 db.multiple_insert('person', [dict(name='a'), dict(name='b')], seqname=False)
87
88 assert db.select("person", where="name='a'")
89 assert db.select("person", where="name='b'")
90
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
91 def test_result_is_unicode(self):
18b6b3fb » Anand
2008-11-27 testcase to test unicode in db result
92 db = webtest.setup_database(self.dbname)
93 self.db.insert('person', False, name='user')
94 name = db.select('person')[0].name
95 self.assertEquals(type(name), unicode)
96
2e6671cb » anandology
2009-05-08 add testBoolean
97 def testBoolean(self):
98 def t(active):
99 name ='name-%s' % active
100 self.db.insert('person', False, name=name, active=active)
101 a = self.db.select('person', where='name=$name', vars=locals())[0].active
102 self.assertEquals(a, active)
103 t(False)
104 t(True)
105
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
106 class PostgresTest(DBTest):
107 dbname = "postgres"
108 driver = "psycopg2"
109
110 class PostgresTest_psycopg(PostgresTest):
111 driver = "psycopg"
112
113 class PostgresTest_pgdb(PostgresTest):
114 driver = "pgdb"
115
dd6e1af5 » anandology
2007-12-24 web.py test suite.
116 class SqliteTest(DBTest):
117 dbname = "sqlite"
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
118 driver = "sqlite3"
dd6e1af5 » anandology
2007-12-24 web.py test suite.
119
f54e1687 » anandology
2008-01-04 Database transactions
120 def testNestedTransactions(self):
121 #nested transactions does not work with sqlite
122 pass
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
123
124 class SqliteTest_pysqlite2(SqliteTest):
125 driver = "pysqlite2.dbapi2"
126
dd6e1af5 » anandology
2007-12-24 web.py test suite.
127 class MySQLTest(DBTest):
128 dbname = "mysql"
f54e1687 » anandology
2008-01-04 Database transactions
129
130 def setUp(self):
131 self.db = webtest.setup_database(self.dbname)
132 # In mysql, transactions are supported only with INNODB engine.
133 self.db.query("CREATE TABLE person (name text, email text) ENGINE=INNODB")
dd6e1af5 » anandology
2007-12-24 web.py test suite.
134
2e6671cb » anandology
2009-05-08 add testBoolean
135 def testBoolean(self):
136 # boolean datatype is not suppoted in MySQL (at least until v5.0)
137 pass
138
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
139 del DBTest
140
141 def is_test(cls):
142 import inspect
143 return inspect.isclass(cls) and webtest.TestCase in inspect.getmro(cls)
3ba0911e » anandology
2008-11-01 unicodify web.input
144
145 # ignore db tests when the required db adapter is not found.
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
146 for t in globals().values():
147 if is_test(t) and not t('_testable')._testable():
3ba0911e » anandology
2008-11-01 unicodify web.input
148 del globals()[t.__name__]
88c46699 » anandology
2009-05-08 test DB classes with each driver separately.
149
150 del t
3ba0911e » anandology
2008-11-01 unicodify web.input
151
dd6e1af5 » anandology
2007-12-24 web.py test suite.
152 if __name__ == '__main__':
153 webtest.main()
Something went wrong with that request. Please try again.