Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 163 lines (130 sloc) 5.017 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
93 assert db.select("person", where="name='a'")
94 assert db.select("person", where="name='b'")
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
2e6671c @anandology add testBoolean
anandology authored
102 def testBoolean(self):
103 def t(active):
104 name ='name-%s' % active
105 self.db.insert('person', False, name=name, active=active)
106 a = self.db.select('person', where='name=$name', vars=locals())[0].active
107 self.assertEquals(a, active)
108 t(False)
109 t(True)
110
88c4669 @anandology test DB classes with each driver separately.
anandology authored
111 class PostgresTest(DBTest):
112 dbname = "postgres"
113 driver = "psycopg2"
114
115 class PostgresTest_psycopg(PostgresTest):
116 driver = "psycopg"
117
118 class PostgresTest_pgdb(PostgresTest):
119 driver = "pgdb"
120
dd6e1af @anandology web.py test suite.
anandology authored
121 class SqliteTest(DBTest):
122 dbname = "sqlite"
88c4669 @anandology test DB classes with each driver separately.
anandology authored
123 driver = "sqlite3"
dd6e1af @anandology web.py test suite.
anandology authored
124
f54e168 @anandology Database transactions
anandology authored
125 def testNestedTransactions(self):
126 #nested transactions does not work with sqlite
127 pass
88c4669 @anandology test DB classes with each driver separately.
anandology authored
128
129 class SqliteTest_pysqlite2(SqliteTest):
130 driver = "pysqlite2.dbapi2"
131
dd6e1af @anandology web.py test suite.
anandology authored
132 class MySQLTest(DBTest):
133 dbname = "mysql"
f54e168 @anandology Database transactions
anandology authored
134
135 def setUp(self):
136 self.db = webtest.setup_database(self.dbname)
137 # In mysql, transactions are supported only with INNODB engine.
138 self.db.query("CREATE TABLE person (name text, email text) ENGINE=INNODB")
dd6e1af @anandology web.py test suite.
anandology authored
139
2e6671c @anandology add testBoolean
anandology authored
140 def testBoolean(self):
141 # boolean datatype is not suppoted in MySQL (at least until v5.0)
142 pass
143
88c4669 @anandology test DB classes with each driver separately.
anandology authored
144 del DBTest
145
146 def is_test(cls):
147 import inspect
148 return inspect.isclass(cls) and webtest.TestCase in inspect.getmro(cls)
3ba0911 @anandology unicodify web.input
anandology authored
149
150 # ignore db tests when the required db adapter is not found.
88c4669 @anandology test DB classes with each driver separately.
anandology authored
151 for t in globals().values():
152 if is_test(t) and not t('_testable')._testable():
3ba0911 @anandology unicodify web.input
anandology authored
153 del globals()[t.__name__]
88c4669 @anandology test DB classes with each driver separately.
anandology authored
154 del t
3ba0911 @anandology unicodify web.input
anandology authored
155
b0df4ed @anandology ignore testPooling when DBUtils module is not available.
anandology authored
156 try:
157 import DBUtils
158 except ImportError, e:
159 print >> web.debug, str(e) + "(ignoring testPooling)"
160
dd6e1af @anandology web.py test suite.
anandology authored
161 if __name__ == '__main__':
162 webtest.main()
Something went wrong with that request. Please try again.