blob: 518b8ae41098f750a7af9b274148b6ee1712a179 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001#-*- coding: ISO-8859-1 -*-
2# pysqlite2/test/dbapi.py: tests for DB-API compliance
3#
Gerhard Häringf9cee222010-03-05 15:20:03 +00004# Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005#
6# This file is part of pysqlite.
7#
8# This software is provided 'as-is', without any express or implied
9# warranty. In no event will the authors be held liable for any damages
10# arising from the use of this software.
11#
12# Permission is granted to anyone to use this software for any purpose,
13# including commercial applications, and to alter it and redistribute it
14# freely, subject to the following restrictions:
15#
16# 1. The origin of this software must not be misrepresented; you must not
17# claim that you wrote the original software. If you use this software
18# in a product, an acknowledgment in the product documentation would be
19# appreciated but is not required.
20# 2. Altered source versions must be plainly marked as such, and must not be
21# misrepresented as being the original software.
22# 3. This notice may not be removed or altered from any source distribution.
23
24import unittest
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000025import sqlite3 as sqlite
Victor Stinner45df8202010-04-28 22:31:17 +000026try:
27 import threading
28except ImportError:
29 threading = None
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030
31class ModuleTests(unittest.TestCase):
32 def CheckAPILevel(self):
33 self.assertEqual(sqlite.apilevel, "2.0",
34 "apilevel is %s, should be 2.0" % sqlite.apilevel)
35
36 def CheckThreadSafety(self):
37 self.assertEqual(sqlite.threadsafety, 1,
38 "threadsafety is %d, should be 1" % sqlite.threadsafety)
39
40 def CheckParamStyle(self):
41 self.assertEqual(sqlite.paramstyle, "qmark",
42 "paramstyle is '%s', should be 'qmark'" %
43 sqlite.paramstyle)
44
45 def CheckWarning(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000046 self.assertTrue(issubclass(sqlite.Warning, Exception),
Guido van Rossumcd16bf62007-06-13 18:07:49 +000047 "Warning is not a subclass of Exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048
49 def CheckError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000050 self.assertTrue(issubclass(sqlite.Error, Exception),
Guido van Rossumcd16bf62007-06-13 18:07:49 +000051 "Error is not a subclass of Exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
53 def CheckInterfaceError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000054 self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055 "InterfaceError is not a subclass of Error")
56
57 def CheckDatabaseError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000058 self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059 "DatabaseError is not a subclass of Error")
60
61 def CheckDataError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000062 self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063 "DataError is not a subclass of DatabaseError")
64
65 def CheckOperationalError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000066 self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067 "OperationalError is not a subclass of DatabaseError")
68
69 def CheckIntegrityError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000070 self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 "IntegrityError is not a subclass of DatabaseError")
72
73 def CheckInternalError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000074 self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 "InternalError is not a subclass of DatabaseError")
76
77 def CheckProgrammingError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000078 self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079 "ProgrammingError is not a subclass of DatabaseError")
80
81 def CheckNotSupportedError(self):
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000082 self.assertTrue(issubclass(sqlite.NotSupportedError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 sqlite.DatabaseError),
84 "NotSupportedError is not a subclass of DatabaseError")
85
86class ConnectionTests(unittest.TestCase):
R. David Murrayd35251d2010-06-01 01:32:12 +000087
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 def setUp(self):
89 self.cx = sqlite.connect(":memory:")
90 cu = self.cx.cursor()
91 cu.execute("create table test(id integer primary key, name text)")
92 cu.execute("insert into test(name) values (?)", ("foo",))
93
94 def tearDown(self):
95 self.cx.close()
96
97 def CheckCommit(self):
98 self.cx.commit()
99
100 def CheckCommitAfterNoChanges(self):
101 """
102 A commit should also work when no changes were made to the database.
103 """
104 self.cx.commit()
105 self.cx.commit()
106
107 def CheckRollback(self):
108 self.cx.rollback()
109
110 def CheckRollbackAfterNoChanges(self):
111 """
112 A rollback should also work when no changes were made to the database.
113 """
114 self.cx.rollback()
115 self.cx.rollback()
116
117 def CheckCursor(self):
118 cu = self.cx.cursor()
119
120 def CheckFailedOpen(self):
121 YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
122 try:
123 con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
124 except sqlite.OperationalError:
125 return
126 self.fail("should have raised an OperationalError")
127
128 def CheckClose(self):
129 self.cx.close()
130
131 def CheckExceptions(self):
132 # Optional DB-API extension.
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000133 self.assertEqual(self.cx.Warning, sqlite.Warning)
134 self.assertEqual(self.cx.Error, sqlite.Error)
135 self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError)
136 self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError)
137 self.assertEqual(self.cx.DataError, sqlite.DataError)
138 self.assertEqual(self.cx.OperationalError, sqlite.OperationalError)
139 self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError)
140 self.assertEqual(self.cx.InternalError, sqlite.InternalError)
141 self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError)
142 self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143
R. David Murrayd35251d2010-06-01 01:32:12 +0000144 def CheckInTransaction(self):
145 # Can't use db from setUp because we want to test initial state.
146 cx = sqlite.connect(":memory:")
147 cu = cx.cursor()
148 self.assertEqual(cx.in_transaction, False)
149 cu.execute("create table transactiontest(id integer primary key, name text)")
150 self.assertEqual(cx.in_transaction, False)
151 cu.execute("insert into transactiontest(name) values (?)", ("foo",))
152 self.assertEqual(cx.in_transaction, True)
153 cu.execute("select name from transactiontest where name=?", ["foo"])
154 row = cu.fetchone()
155 self.assertEqual(cx.in_transaction, True)
156 cx.commit()
157 self.assertEqual(cx.in_transaction, False)
158 cu.execute("select name from transactiontest where name=?", ["foo"])
159 row = cu.fetchone()
160 self.assertEqual(cx.in_transaction, False)
161
162 def CheckInTransactionRO(self):
163 with self.assertRaises(AttributeError):
164 self.cx.in_transaction = True
165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166class CursorTests(unittest.TestCase):
167 def setUp(self):
168 self.cx = sqlite.connect(":memory:")
169 self.cu = self.cx.cursor()
170 self.cu.execute("create table test(id integer primary key, name text, income number)")
171 self.cu.execute("insert into test(name) values (?)", ("foo",))
172
173 def tearDown(self):
174 self.cu.close()
175 self.cx.close()
176
177 def CheckExecuteNoArgs(self):
178 self.cu.execute("delete from test")
179
180 def CheckExecuteIllegalSql(self):
181 try:
182 self.cu.execute("select asdf")
183 self.fail("should have raised an OperationalError")
184 except sqlite.OperationalError:
185 return
186 except:
187 self.fail("raised wrong exception")
188
189 def CheckExecuteTooMuchSql(self):
190 try:
191 self.cu.execute("select 5+4; select 4+5")
192 self.fail("should have raised a Warning")
193 except sqlite.Warning:
194 return
195 except:
196 self.fail("raised wrong exception")
197
198 def CheckExecuteTooMuchSql2(self):
199 self.cu.execute("select 5+4; -- foo bar")
200
201 def CheckExecuteTooMuchSql3(self):
202 self.cu.execute("""
203 select 5+4;
204
205 /*
206 foo
207 */
208 """)
209
210 def CheckExecuteWrongSqlArg(self):
211 try:
212 self.cu.execute(42)
213 self.fail("should have raised a ValueError")
214 except ValueError:
215 return
216 except:
217 self.fail("raised wrong exception.")
218
219 def CheckExecuteArgInt(self):
220 self.cu.execute("insert into test(id) values (?)", (42,))
221
222 def CheckExecuteArgFloat(self):
223 self.cu.execute("insert into test(income) values (?)", (2500.32,))
224
225 def CheckExecuteArgString(self):
226 self.cu.execute("insert into test(name) values (?)", ("Hugo",))
227
228 def CheckExecuteWrongNoOfArgs1(self):
229 # too many parameters
230 try:
231 self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
232 self.fail("should have raised ProgrammingError")
233 except sqlite.ProgrammingError:
234 pass
235
236 def CheckExecuteWrongNoOfArgs2(self):
237 # too little parameters
238 try:
239 self.cu.execute("insert into test(id) values (?)")
240 self.fail("should have raised ProgrammingError")
241 except sqlite.ProgrammingError:
242 pass
243
244 def CheckExecuteWrongNoOfArgs3(self):
245 # no parameters, parameters are needed
246 try:
247 self.cu.execute("insert into test(id) values (?)")
248 self.fail("should have raised ProgrammingError")
249 except sqlite.ProgrammingError:
250 pass
251
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000252 def CheckExecuteParamList(self):
253 self.cu.execute("insert into test(name) values ('foo')")
254 self.cu.execute("select name from test where name=?", ["foo"])
255 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000256 self.assertEqual(row[0], "foo")
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000257
258 def CheckExecuteParamSequence(self):
259 class L(object):
260 def __len__(self):
261 return 1
262 def __getitem__(self, x):
263 assert x == 0
264 return "foo"
265
266 self.cu.execute("insert into test(name) values ('foo')")
267 self.cu.execute("select name from test where name=?", L())
268 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000269 self.assertEqual(row[0], "foo")
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000270
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 def CheckExecuteDictMapping(self):
272 self.cu.execute("insert into test(name) values ('foo')")
273 self.cu.execute("select name from test where name=:name", {"name": "foo"})
274 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000275 self.assertEqual(row[0], "foo")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000277 def CheckExecuteDictMapping_Mapping(self):
278 class D(dict):
279 def __missing__(self, key):
280 return "foo"
281
282 self.cu.execute("insert into test(name) values ('foo')")
283 self.cu.execute("select name from test where name=:name", D())
284 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000285 self.assertEqual(row[0], "foo")
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 def CheckExecuteDictMappingTooLittleArgs(self):
288 self.cu.execute("insert into test(name) values ('foo')")
289 try:
290 self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
291 self.fail("should have raised ProgrammingError")
292 except sqlite.ProgrammingError:
293 pass
294
295 def CheckExecuteDictMappingNoArgs(self):
296 self.cu.execute("insert into test(name) values ('foo')")
297 try:
298 self.cu.execute("select name from test where name=:name")
299 self.fail("should have raised ProgrammingError")
300 except sqlite.ProgrammingError:
301 pass
302
303 def CheckExecuteDictMappingUnnamed(self):
304 self.cu.execute("insert into test(name) values ('foo')")
305 try:
306 self.cu.execute("select name from test where name=?", {"name": "foo"})
307 self.fail("should have raised ProgrammingError")
308 except sqlite.ProgrammingError:
309 pass
310
311 def CheckClose(self):
312 self.cu.close()
313
314 def CheckRowcountExecute(self):
315 self.cu.execute("delete from test")
316 self.cu.execute("insert into test(name) values ('foo')")
317 self.cu.execute("insert into test(name) values ('foo')")
318 self.cu.execute("update test set name='bar'")
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000319 self.assertEqual(self.cu.rowcount, 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320
Georg Brandlf78e02b2008-06-10 17:40:04 +0000321 def CheckRowcountSelect(self):
322 """
323 pysqlite does not know the rowcount of SELECT statements, because we
324 don't fetch all rows after executing the select statement. The rowcount
325 has thus to be -1.
326 """
327 self.cu.execute("select 5 union select 6")
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000328 self.assertEqual(self.cu.rowcount, -1)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 def CheckRowcountExecutemany(self):
331 self.cu.execute("delete from test")
332 self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000333 self.assertEqual(self.cu.rowcount, 3)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334
335 def CheckTotalChanges(self):
336 self.cu.execute("insert into test(name) values ('foo')")
337 self.cu.execute("insert into test(name) values ('foo')")
338 if self.cx.total_changes < 2:
339 self.fail("total changes reported wrong value")
340
341 # Checks for executemany:
342 # Sequences are required by the DB-API, iterators
343 # enhancements in pysqlite.
344
345 def CheckExecuteManySequence(self):
346 self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)])
347
348 def CheckExecuteManyIterator(self):
349 class MyIter:
350 def __init__(self):
351 self.value = 5
352
Georg Brandla18af4e2007-04-21 15:47:16 +0000353 def __next__(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354 if self.value == 10:
355 raise StopIteration
356 else:
357 self.value += 1
358 return (self.value,)
359
360 self.cu.executemany("insert into test(income) values (?)", MyIter())
361
362 def CheckExecuteManyGenerator(self):
363 def mygen():
364 for i in range(5):
365 yield (i,)
366
367 self.cu.executemany("insert into test(income) values (?)", mygen())
368
369 def CheckExecuteManyWrongSqlArg(self):
370 try:
371 self.cu.executemany(42, [(3,)])
372 self.fail("should have raised a ValueError")
373 except ValueError:
374 return
375 except:
376 self.fail("raised wrong exception.")
377
378 def CheckExecuteManySelect(self):
379 try:
380 self.cu.executemany("select ?", [(3,)])
381 self.fail("should have raised a ProgrammingError")
382 except sqlite.ProgrammingError:
383 return
384 except:
385 self.fail("raised wrong exception.")
386
387 def CheckExecuteManyNotIterable(self):
388 try:
389 self.cu.executemany("insert into test(income) values (?)", 42)
390 self.fail("should have raised a TypeError")
391 except TypeError:
392 return
Guido van Rossumb940e112007-01-10 16:19:56 +0000393 except Exception as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000394 print("raised", e.__class__)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 self.fail("raised wrong exception.")
396
397 def CheckFetchIter(self):
398 # Optional DB-API extension.
399 self.cu.execute("delete from test")
400 self.cu.execute("insert into test(id) values (?)", (5,))
401 self.cu.execute("insert into test(id) values (?)", (6,))
402 self.cu.execute("select id from test order by id")
403 lst = []
404 for row in self.cu:
405 lst.append(row[0])
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000406 self.assertEqual(lst[0], 5)
407 self.assertEqual(lst[1], 6)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408
409 def CheckFetchone(self):
410 self.cu.execute("select name from test")
411 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000412 self.assertEqual(row[0], "foo")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413 row = self.cu.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000414 self.assertEqual(row, None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000415
416 def CheckFetchoneNoStatement(self):
417 cur = self.cx.cursor()
418 row = cur.fetchone()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000419 self.assertEqual(row, None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420
421 def CheckArraySize(self):
422 # must default ot 1
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000423 self.assertEqual(self.cu.arraysize, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424
425 # now set to 2
426 self.cu.arraysize = 2
427
428 # now make the query return 3 rows
429 self.cu.execute("delete from test")
430 self.cu.execute("insert into test(name) values ('A')")
431 self.cu.execute("insert into test(name) values ('B')")
432 self.cu.execute("insert into test(name) values ('C')")
433 self.cu.execute("select name from test")
434 res = self.cu.fetchmany()
435
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000436 self.assertEqual(len(res), 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437
438 def CheckFetchmany(self):
439 self.cu.execute("select name from test")
440 res = self.cu.fetchmany(100)
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000441 self.assertEqual(len(res), 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 res = self.cu.fetchmany(100)
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000443 self.assertEqual(res, [])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000445 def CheckFetchmanyKwArg(self):
446 """Checks if fetchmany works with keyword arguments"""
447 self.cu.execute("select name from test")
448 res = self.cu.fetchmany(size=100)
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000449 self.assertEqual(len(res), 1)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000450
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 def CheckFetchall(self):
452 self.cu.execute("select name from test")
453 res = self.cu.fetchall()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000454 self.assertEqual(len(res), 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 res = self.cu.fetchall()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000456 self.assertEqual(res, [])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457
458 def CheckSetinputsizes(self):
459 self.cu.setinputsizes([3, 4, 5])
460
461 def CheckSetoutputsize(self):
462 self.cu.setoutputsize(5, 0)
463
464 def CheckSetoutputsizeNoColumn(self):
465 self.cu.setoutputsize(42)
466
467 def CheckCursorConnection(self):
468 # Optional DB-API extension.
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000469 self.assertEqual(self.cu.connection, self.cx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470
471 def CheckWrongCursorCallable(self):
472 try:
473 def f(): pass
474 cur = self.cx.cursor(f)
475 self.fail("should have raised a TypeError")
476 except TypeError:
477 return
478 self.fail("should have raised a ValueError")
479
480 def CheckCursorWrongClass(self):
481 class Foo: pass
482 foo = Foo()
483 try:
484 cur = sqlite.Cursor(foo)
485 self.fail("should have raised a ValueError")
486 except TypeError:
487 pass
488
Victor Stinner45df8202010-04-28 22:31:17 +0000489@unittest.skipUnless(threading, 'This test requires threading.')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490class ThreadTests(unittest.TestCase):
491 def setUp(self):
492 self.con = sqlite.connect(":memory:")
493 self.cur = self.con.cursor()
494 self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)")
495
496 def tearDown(self):
497 self.cur.close()
498 self.con.close()
499
500 def CheckConCursor(self):
501 def run(con, errors):
502 try:
503 cur = con.cursor()
504 errors.append("did not raise ProgrammingError")
505 return
506 except sqlite.ProgrammingError:
507 return
508 except:
509 errors.append("raised wrong exception")
510
511 errors = []
512 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
513 t.start()
514 t.join()
515 if len(errors) > 0:
516 self.fail("\n".join(errors))
517
518 def CheckConCommit(self):
519 def run(con, errors):
520 try:
521 con.commit()
522 errors.append("did not raise ProgrammingError")
523 return
524 except sqlite.ProgrammingError:
525 return
526 except:
527 errors.append("raised wrong exception")
528
529 errors = []
530 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
531 t.start()
532 t.join()
533 if len(errors) > 0:
534 self.fail("\n".join(errors))
535
536 def CheckConRollback(self):
537 def run(con, errors):
538 try:
539 con.rollback()
540 errors.append("did not raise ProgrammingError")
541 return
542 except sqlite.ProgrammingError:
543 return
544 except:
545 errors.append("raised wrong exception")
546
547 errors = []
548 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
549 t.start()
550 t.join()
551 if len(errors) > 0:
552 self.fail("\n".join(errors))
553
554 def CheckConClose(self):
555 def run(con, errors):
556 try:
557 con.close()
558 errors.append("did not raise ProgrammingError")
559 return
560 except sqlite.ProgrammingError:
561 return
562 except:
563 errors.append("raised wrong exception")
564
565 errors = []
566 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
567 t.start()
568 t.join()
569 if len(errors) > 0:
570 self.fail("\n".join(errors))
571
572 def CheckCurImplicitBegin(self):
573 def run(cur, errors):
574 try:
575 cur.execute("insert into test(name) values ('a')")
576 errors.append("did not raise ProgrammingError")
577 return
578 except sqlite.ProgrammingError:
579 return
580 except:
581 errors.append("raised wrong exception")
582
583 errors = []
584 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
585 t.start()
586 t.join()
587 if len(errors) > 0:
588 self.fail("\n".join(errors))
589
590 def CheckCurClose(self):
591 def run(cur, errors):
592 try:
593 cur.close()
594 errors.append("did not raise ProgrammingError")
595 return
596 except sqlite.ProgrammingError:
597 return
598 except:
599 errors.append("raised wrong exception")
600
601 errors = []
602 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
603 t.start()
604 t.join()
605 if len(errors) > 0:
606 self.fail("\n".join(errors))
607
608 def CheckCurExecute(self):
609 def run(cur, errors):
610 try:
611 cur.execute("select name from test")
612 errors.append("did not raise ProgrammingError")
613 return
614 except sqlite.ProgrammingError:
615 return
616 except:
617 errors.append("raised wrong exception")
618
619 errors = []
620 self.cur.execute("insert into test(name) values ('a')")
621 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
622 t.start()
623 t.join()
624 if len(errors) > 0:
625 self.fail("\n".join(errors))
626
627 def CheckCurIterNext(self):
628 def run(cur, errors):
629 try:
630 row = cur.fetchone()
631 errors.append("did not raise ProgrammingError")
632 return
633 except sqlite.ProgrammingError:
634 return
635 except:
636 errors.append("raised wrong exception")
637
638 errors = []
639 self.cur.execute("insert into test(name) values ('a')")
640 self.cur.execute("select name from test")
641 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
642 t.start()
643 t.join()
644 if len(errors) > 0:
645 self.fail("\n".join(errors))
646
647class ConstructorTests(unittest.TestCase):
648 def CheckDate(self):
649 d = sqlite.Date(2004, 10, 28)
650
651 def CheckTime(self):
652 t = sqlite.Time(12, 39, 35)
653
654 def CheckTimestamp(self):
655 ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35)
656
657 def CheckDateFromTicks(self):
658 d = sqlite.DateFromTicks(42)
659
660 def CheckTimeFromTicks(self):
661 t = sqlite.TimeFromTicks(42)
662
663 def CheckTimestampFromTicks(self):
664 ts = sqlite.TimestampFromTicks(42)
665
666 def CheckBinary(self):
Guido van Rossumbae07c92007-10-08 02:46:15 +0000667 b = sqlite.Binary(b"\0'")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668
669class ExtensionTests(unittest.TestCase):
670 def CheckScriptStringSql(self):
671 con = sqlite.connect(":memory:")
672 cur = con.cursor()
673 cur.executescript("""
674 -- bla bla
675 /* a stupid comment */
676 create table a(i);
677 insert into a(i) values (5);
678 """)
679 cur.execute("select i from a")
680 res = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000681 self.assertEqual(res, 5)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682
Gerhard Häringf9cee222010-03-05 15:20:03 +0000683 def CheckScriptSyntaxError(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 con = sqlite.connect(":memory:")
685 cur = con.cursor()
686 raised = False
687 try:
Gerhard Häringf9cee222010-03-05 15:20:03 +0000688 cur.executescript("create table test(x); asdf; create table test2(x)")
689 except sqlite.OperationalError:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 raised = True
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000691 self.assertEqual(raised, True, "should have raised an exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692
693 def CheckScriptErrorNormal(self):
694 con = sqlite.connect(":memory:")
695 cur = con.cursor()
696 raised = False
697 try:
698 cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
699 except sqlite.OperationalError:
700 raised = True
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000701 self.assertEqual(raised, True, "should have raised an exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702
703 def CheckConnectionExecute(self):
704 con = sqlite.connect(":memory:")
705 result = con.execute("select 5").fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000706 self.assertEqual(result, 5, "Basic test of Connection.execute")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707
708 def CheckConnectionExecutemany(self):
709 con = sqlite.connect(":memory:")
710 con.execute("create table test(foo)")
711 con.executemany("insert into test(foo) values (?)", [(3,), (4,)])
712 result = con.execute("select foo from test order by foo").fetchall()
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000713 self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany")
714 self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715
716 def CheckConnectionExecutescript(self):
717 con = sqlite.connect(":memory:")
718 con.executescript("create table test(foo); insert into test(foo) values (5);")
719 result = con.execute("select foo from test").fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000720 self.assertEqual(result, 5, "Basic test of Connection.executescript")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721
Gerhard Häringf9cee222010-03-05 15:20:03 +0000722class ClosedConTests(unittest.TestCase):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 def setUp(self):
724 pass
725
726 def tearDown(self):
727 pass
728
729 def CheckClosedConCursor(self):
730 con = sqlite.connect(":memory:")
731 con.close()
732 try:
733 cur = con.cursor()
734 self.fail("Should have raised a ProgrammingError")
735 except sqlite.ProgrammingError:
736 pass
737 except:
738 self.fail("Should have raised a ProgrammingError")
739
740 def CheckClosedConCommit(self):
741 con = sqlite.connect(":memory:")
742 con.close()
743 try:
744 con.commit()
745 self.fail("Should have raised a ProgrammingError")
746 except sqlite.ProgrammingError:
747 pass
748 except:
749 self.fail("Should have raised a ProgrammingError")
750
751 def CheckClosedConRollback(self):
752 con = sqlite.connect(":memory:")
753 con.close()
754 try:
755 con.rollback()
756 self.fail("Should have raised a ProgrammingError")
757 except sqlite.ProgrammingError:
758 pass
759 except:
760 self.fail("Should have raised a ProgrammingError")
761
762 def CheckClosedCurExecute(self):
763 con = sqlite.connect(":memory:")
764 cur = con.cursor()
765 con.close()
766 try:
767 cur.execute("select 4")
768 self.fail("Should have raised a ProgrammingError")
769 except sqlite.ProgrammingError:
770 pass
771 except:
772 self.fail("Should have raised a ProgrammingError")
773
Gerhard Häringf9cee222010-03-05 15:20:03 +0000774 def CheckClosedCreateFunction(self):
775 con = sqlite.connect(":memory:")
776 con.close()
777 def f(x): return 17
778 try:
779 con.create_function("foo", 1, f)
780 self.fail("Should have raised a ProgrammingError")
781 except sqlite.ProgrammingError:
782 pass
783 except:
784 self.fail("Should have raised a ProgrammingError")
785
786 def CheckClosedCreateAggregate(self):
787 con = sqlite.connect(":memory:")
788 con.close()
789 class Agg:
790 def __init__(self):
791 pass
792 def step(self, x):
793 pass
794 def finalize(self):
795 return 17
796 try:
797 con.create_aggregate("foo", 1, Agg)
798 self.fail("Should have raised a ProgrammingError")
799 except sqlite.ProgrammingError:
800 pass
801 except:
802 self.fail("Should have raised a ProgrammingError")
803
804 def CheckClosedSetAuthorizer(self):
805 con = sqlite.connect(":memory:")
806 con.close()
807 def authorizer(*args):
808 return sqlite.DENY
809 try:
810 con.set_authorizer(authorizer)
811 self.fail("Should have raised a ProgrammingError")
812 except sqlite.ProgrammingError:
813 pass
814 except:
815 self.fail("Should have raised a ProgrammingError")
816
817 def CheckClosedSetProgressCallback(self):
818 con = sqlite.connect(":memory:")
819 con.close()
820 def progress(): pass
821 try:
822 con.set_progress_handler(progress, 100)
823 self.fail("Should have raised a ProgrammingError")
824 except sqlite.ProgrammingError:
825 pass
826 except:
827 self.fail("Should have raised a ProgrammingError")
828
829 def CheckClosedCall(self):
830 con = sqlite.connect(":memory:")
831 con.close()
832 try:
833 con()
834 self.fail("Should have raised a ProgrammingError")
835 except sqlite.ProgrammingError:
836 pass
837 except:
838 self.fail("Should have raised a ProgrammingError")
839
840class ClosedCurTests(unittest.TestCase):
841 def setUp(self):
842 pass
843
844 def tearDown(self):
845 pass
846
847 def CheckClosed(self):
848 con = sqlite.connect(":memory:")
849 cur = con.cursor()
850 cur.close()
851
852 for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"):
853 if method_name in ("execute", "executescript"):
854 params = ("select 4 union select 5",)
855 elif method_name == "executemany":
856 params = ("insert into foo(bar) values (?)", [(3,), (4,)])
857 else:
858 params = []
859
860 try:
861 method = getattr(cur, method_name)
862
863 method(*params)
864 self.fail("Should have raised a ProgrammingError: method " + method_name)
865 except sqlite.ProgrammingError:
866 pass
867 except:
868 self.fail("Should have raised a ProgrammingError: " + method_name)
869
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870def suite():
871 module_suite = unittest.makeSuite(ModuleTests, "Check")
872 connection_suite = unittest.makeSuite(ConnectionTests, "Check")
873 cursor_suite = unittest.makeSuite(CursorTests, "Check")
874 thread_suite = unittest.makeSuite(ThreadTests, "Check")
875 constructor_suite = unittest.makeSuite(ConstructorTests, "Check")
876 ext_suite = unittest.makeSuite(ExtensionTests, "Check")
Gerhard Häringf9cee222010-03-05 15:20:03 +0000877 closed_con_suite = unittest.makeSuite(ClosedConTests, "Check")
878 closed_cur_suite = unittest.makeSuite(ClosedCurTests, "Check")
879 return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_con_suite, closed_cur_suite))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880
881def test():
882 runner = unittest.TextTestRunner()
883 runner.run(suite())
884
885if __name__ == "__main__":
886 test()