blob: 8327aa195bfa2cc2f3fd921eee8581094065def0 [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äringe7ea7452008-03-29 00:45:29 +00004# Copyright (C) 2004-2007 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
25import threading
26import sqlite3 as sqlite
27
28class ModuleTests(unittest.TestCase):
29 def CheckAPILevel(self):
30 self.assertEqual(sqlite.apilevel, "2.0",
31 "apilevel is %s, should be 2.0" % sqlite.apilevel)
32
33 def CheckThreadSafety(self):
34 self.assertEqual(sqlite.threadsafety, 1,
35 "threadsafety is %d, should be 1" % sqlite.threadsafety)
36
37 def CheckParamStyle(self):
38 self.assertEqual(sqlite.paramstyle, "qmark",
39 "paramstyle is '%s', should be 'qmark'" %
40 sqlite.paramstyle)
41
42 def CheckWarning(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000043 self.assert_(issubclass(sqlite.Warning, Exception),
44 "Warning is not a subclass of Exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045
46 def CheckError(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000047 self.failUnless(issubclass(sqlite.Error, Exception),
48 "Error is not a subclass of Exception")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049
50 def CheckInterfaceError(self):
51 self.failUnless(issubclass(sqlite.InterfaceError, sqlite.Error),
52 "InterfaceError is not a subclass of Error")
53
54 def CheckDatabaseError(self):
55 self.failUnless(issubclass(sqlite.DatabaseError, sqlite.Error),
56 "DatabaseError is not a subclass of Error")
57
58 def CheckDataError(self):
59 self.failUnless(issubclass(sqlite.DataError, sqlite.DatabaseError),
60 "DataError is not a subclass of DatabaseError")
61
62 def CheckOperationalError(self):
63 self.failUnless(issubclass(sqlite.OperationalError, sqlite.DatabaseError),
64 "OperationalError is not a subclass of DatabaseError")
65
66 def CheckIntegrityError(self):
67 self.failUnless(issubclass(sqlite.IntegrityError, sqlite.DatabaseError),
68 "IntegrityError is not a subclass of DatabaseError")
69
70 def CheckInternalError(self):
71 self.failUnless(issubclass(sqlite.InternalError, sqlite.DatabaseError),
72 "InternalError is not a subclass of DatabaseError")
73
74 def CheckProgrammingError(self):
75 self.failUnless(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError),
76 "ProgrammingError is not a subclass of DatabaseError")
77
78 def CheckNotSupportedError(self):
79 self.failUnless(issubclass(sqlite.NotSupportedError,
80 sqlite.DatabaseError),
81 "NotSupportedError is not a subclass of DatabaseError")
82
83class ConnectionTests(unittest.TestCase):
84 def setUp(self):
85 self.cx = sqlite.connect(":memory:")
86 cu = self.cx.cursor()
87 cu.execute("create table test(id integer primary key, name text)")
88 cu.execute("insert into test(name) values (?)", ("foo",))
89
90 def tearDown(self):
91 self.cx.close()
92
93 def CheckCommit(self):
94 self.cx.commit()
95
96 def CheckCommitAfterNoChanges(self):
97 """
98 A commit should also work when no changes were made to the database.
99 """
100 self.cx.commit()
101 self.cx.commit()
102
103 def CheckRollback(self):
104 self.cx.rollback()
105
106 def CheckRollbackAfterNoChanges(self):
107 """
108 A rollback should also work when no changes were made to the database.
109 """
110 self.cx.rollback()
111 self.cx.rollback()
112
113 def CheckCursor(self):
114 cu = self.cx.cursor()
115
116 def CheckFailedOpen(self):
117 YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
118 try:
119 con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
120 except sqlite.OperationalError:
121 return
122 self.fail("should have raised an OperationalError")
123
124 def CheckClose(self):
125 self.cx.close()
126
127 def CheckExceptions(self):
128 # Optional DB-API extension.
129 self.failUnlessEqual(self.cx.Warning, sqlite.Warning)
130 self.failUnlessEqual(self.cx.Error, sqlite.Error)
131 self.failUnlessEqual(self.cx.InterfaceError, sqlite.InterfaceError)
132 self.failUnlessEqual(self.cx.DatabaseError, sqlite.DatabaseError)
133 self.failUnlessEqual(self.cx.DataError, sqlite.DataError)
134 self.failUnlessEqual(self.cx.OperationalError, sqlite.OperationalError)
135 self.failUnlessEqual(self.cx.IntegrityError, sqlite.IntegrityError)
136 self.failUnlessEqual(self.cx.InternalError, sqlite.InternalError)
137 self.failUnlessEqual(self.cx.ProgrammingError, sqlite.ProgrammingError)
138 self.failUnlessEqual(self.cx.NotSupportedError, sqlite.NotSupportedError)
139
140class CursorTests(unittest.TestCase):
141 def setUp(self):
142 self.cx = sqlite.connect(":memory:")
143 self.cu = self.cx.cursor()
144 self.cu.execute("create table test(id integer primary key, name text, income number)")
145 self.cu.execute("insert into test(name) values (?)", ("foo",))
146
147 def tearDown(self):
148 self.cu.close()
149 self.cx.close()
150
151 def CheckExecuteNoArgs(self):
152 self.cu.execute("delete from test")
153
154 def CheckExecuteIllegalSql(self):
155 try:
156 self.cu.execute("select asdf")
157 self.fail("should have raised an OperationalError")
158 except sqlite.OperationalError:
159 return
160 except:
161 self.fail("raised wrong exception")
162
163 def CheckExecuteTooMuchSql(self):
164 try:
165 self.cu.execute("select 5+4; select 4+5")
166 self.fail("should have raised a Warning")
167 except sqlite.Warning:
168 return
169 except:
170 self.fail("raised wrong exception")
171
172 def CheckExecuteTooMuchSql2(self):
173 self.cu.execute("select 5+4; -- foo bar")
174
175 def CheckExecuteTooMuchSql3(self):
176 self.cu.execute("""
177 select 5+4;
178
179 /*
180 foo
181 */
182 """)
183
184 def CheckExecuteWrongSqlArg(self):
185 try:
186 self.cu.execute(42)
187 self.fail("should have raised a ValueError")
188 except ValueError:
189 return
190 except:
191 self.fail("raised wrong exception.")
192
193 def CheckExecuteArgInt(self):
194 self.cu.execute("insert into test(id) values (?)", (42,))
195
196 def CheckExecuteArgFloat(self):
197 self.cu.execute("insert into test(income) values (?)", (2500.32,))
198
199 def CheckExecuteArgString(self):
200 self.cu.execute("insert into test(name) values (?)", ("Hugo",))
201
202 def CheckExecuteWrongNoOfArgs1(self):
203 # too many parameters
204 try:
205 self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
206 self.fail("should have raised ProgrammingError")
207 except sqlite.ProgrammingError:
208 pass
209
210 def CheckExecuteWrongNoOfArgs2(self):
211 # too little parameters
212 try:
213 self.cu.execute("insert into test(id) values (?)")
214 self.fail("should have raised ProgrammingError")
215 except sqlite.ProgrammingError:
216 pass
217
218 def CheckExecuteWrongNoOfArgs3(self):
219 # no parameters, parameters are needed
220 try:
221 self.cu.execute("insert into test(id) values (?)")
222 self.fail("should have raised ProgrammingError")
223 except sqlite.ProgrammingError:
224 pass
225
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000226 def CheckExecuteParamList(self):
227 self.cu.execute("insert into test(name) values ('foo')")
228 self.cu.execute("select name from test where name=?", ["foo"])
229 row = self.cu.fetchone()
230 self.failUnlessEqual(row[0], "foo")
231
232 def CheckExecuteParamSequence(self):
233 class L(object):
234 def __len__(self):
235 return 1
236 def __getitem__(self, x):
237 assert x == 0
238 return "foo"
239
240 self.cu.execute("insert into test(name) values ('foo')")
241 self.cu.execute("select name from test where name=?", L())
242 row = self.cu.fetchone()
243 self.failUnlessEqual(row[0], "foo")
244
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 def CheckExecuteDictMapping(self):
246 self.cu.execute("insert into test(name) values ('foo')")
247 self.cu.execute("select name from test where name=:name", {"name": "foo"})
248 row = self.cu.fetchone()
249 self.failUnlessEqual(row[0], "foo")
250
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000251 def CheckExecuteDictMapping_Mapping(self):
252 class D(dict):
253 def __missing__(self, key):
254 return "foo"
255
256 self.cu.execute("insert into test(name) values ('foo')")
257 self.cu.execute("select name from test where name=:name", D())
258 row = self.cu.fetchone()
259 self.failUnlessEqual(row[0], "foo")
260
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000261 def CheckExecuteDictMappingTooLittleArgs(self):
262 self.cu.execute("insert into test(name) values ('foo')")
263 try:
264 self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
265 self.fail("should have raised ProgrammingError")
266 except sqlite.ProgrammingError:
267 pass
268
269 def CheckExecuteDictMappingNoArgs(self):
270 self.cu.execute("insert into test(name) values ('foo')")
271 try:
272 self.cu.execute("select name from test where name=:name")
273 self.fail("should have raised ProgrammingError")
274 except sqlite.ProgrammingError:
275 pass
276
277 def CheckExecuteDictMappingUnnamed(self):
278 self.cu.execute("insert into test(name) values ('foo')")
279 try:
280 self.cu.execute("select name from test where name=?", {"name": "foo"})
281 self.fail("should have raised ProgrammingError")
282 except sqlite.ProgrammingError:
283 pass
284
285 def CheckClose(self):
286 self.cu.close()
287
288 def CheckRowcountExecute(self):
289 self.cu.execute("delete from test")
290 self.cu.execute("insert into test(name) values ('foo')")
291 self.cu.execute("insert into test(name) values ('foo')")
292 self.cu.execute("update test set name='bar'")
293 self.failUnlessEqual(self.cu.rowcount, 2)
294
295 def CheckRowcountExecutemany(self):
296 self.cu.execute("delete from test")
297 self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
298 self.failUnlessEqual(self.cu.rowcount, 3)
299
300 def CheckTotalChanges(self):
301 self.cu.execute("insert into test(name) values ('foo')")
302 self.cu.execute("insert into test(name) values ('foo')")
303 if self.cx.total_changes < 2:
304 self.fail("total changes reported wrong value")
305
306 # Checks for executemany:
307 # Sequences are required by the DB-API, iterators
308 # enhancements in pysqlite.
309
310 def CheckExecuteManySequence(self):
311 self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)])
312
313 def CheckExecuteManyIterator(self):
314 class MyIter:
315 def __init__(self):
316 self.value = 5
317
Georg Brandla18af4e2007-04-21 15:47:16 +0000318 def __next__(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 if self.value == 10:
320 raise StopIteration
321 else:
322 self.value += 1
323 return (self.value,)
324
325 self.cu.executemany("insert into test(income) values (?)", MyIter())
326
327 def CheckExecuteManyGenerator(self):
328 def mygen():
329 for i in range(5):
330 yield (i,)
331
332 self.cu.executemany("insert into test(income) values (?)", mygen())
333
334 def CheckExecuteManyWrongSqlArg(self):
335 try:
336 self.cu.executemany(42, [(3,)])
337 self.fail("should have raised a ValueError")
338 except ValueError:
339 return
340 except:
341 self.fail("raised wrong exception.")
342
343 def CheckExecuteManySelect(self):
344 try:
345 self.cu.executemany("select ?", [(3,)])
346 self.fail("should have raised a ProgrammingError")
347 except sqlite.ProgrammingError:
348 return
349 except:
350 self.fail("raised wrong exception.")
351
352 def CheckExecuteManyNotIterable(self):
353 try:
354 self.cu.executemany("insert into test(income) values (?)", 42)
355 self.fail("should have raised a TypeError")
356 except TypeError:
357 return
Guido van Rossumb940e112007-01-10 16:19:56 +0000358 except Exception as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000359 print("raised", e.__class__)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 self.fail("raised wrong exception.")
361
362 def CheckFetchIter(self):
363 # Optional DB-API extension.
364 self.cu.execute("delete from test")
365 self.cu.execute("insert into test(id) values (?)", (5,))
366 self.cu.execute("insert into test(id) values (?)", (6,))
367 self.cu.execute("select id from test order by id")
368 lst = []
369 for row in self.cu:
370 lst.append(row[0])
371 self.failUnlessEqual(lst[0], 5)
372 self.failUnlessEqual(lst[1], 6)
373
374 def CheckFetchone(self):
375 self.cu.execute("select name from test")
376 row = self.cu.fetchone()
377 self.failUnlessEqual(row[0], "foo")
378 row = self.cu.fetchone()
379 self.failUnlessEqual(row, None)
380
381 def CheckFetchoneNoStatement(self):
382 cur = self.cx.cursor()
383 row = cur.fetchone()
384 self.failUnlessEqual(row, None)
385
386 def CheckArraySize(self):
387 # must default ot 1
388 self.failUnlessEqual(self.cu.arraysize, 1)
389
390 # now set to 2
391 self.cu.arraysize = 2
392
393 # now make the query return 3 rows
394 self.cu.execute("delete from test")
395 self.cu.execute("insert into test(name) values ('A')")
396 self.cu.execute("insert into test(name) values ('B')")
397 self.cu.execute("insert into test(name) values ('C')")
398 self.cu.execute("select name from test")
399 res = self.cu.fetchmany()
400
401 self.failUnlessEqual(len(res), 2)
402
403 def CheckFetchmany(self):
404 self.cu.execute("select name from test")
405 res = self.cu.fetchmany(100)
406 self.failUnlessEqual(len(res), 1)
407 res = self.cu.fetchmany(100)
408 self.failUnlessEqual(res, [])
409
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000410 def CheckFetchmanyKwArg(self):
411 """Checks if fetchmany works with keyword arguments"""
412 self.cu.execute("select name from test")
413 res = self.cu.fetchmany(size=100)
414 self.failUnlessEqual(len(res), 1)
415
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 def CheckFetchall(self):
417 self.cu.execute("select name from test")
418 res = self.cu.fetchall()
419 self.failUnlessEqual(len(res), 1)
420 res = self.cu.fetchall()
421 self.failUnlessEqual(res, [])
422
423 def CheckSetinputsizes(self):
424 self.cu.setinputsizes([3, 4, 5])
425
426 def CheckSetoutputsize(self):
427 self.cu.setoutputsize(5, 0)
428
429 def CheckSetoutputsizeNoColumn(self):
430 self.cu.setoutputsize(42)
431
432 def CheckCursorConnection(self):
433 # Optional DB-API extension.
434 self.failUnlessEqual(self.cu.connection, self.cx)
435
436 def CheckWrongCursorCallable(self):
437 try:
438 def f(): pass
439 cur = self.cx.cursor(f)
440 self.fail("should have raised a TypeError")
441 except TypeError:
442 return
443 self.fail("should have raised a ValueError")
444
445 def CheckCursorWrongClass(self):
446 class Foo: pass
447 foo = Foo()
448 try:
449 cur = sqlite.Cursor(foo)
450 self.fail("should have raised a ValueError")
451 except TypeError:
452 pass
453
454class ThreadTests(unittest.TestCase):
455 def setUp(self):
456 self.con = sqlite.connect(":memory:")
457 self.cur = self.con.cursor()
458 self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)")
459
460 def tearDown(self):
461 self.cur.close()
462 self.con.close()
463
464 def CheckConCursor(self):
465 def run(con, errors):
466 try:
467 cur = con.cursor()
468 errors.append("did not raise ProgrammingError")
469 return
470 except sqlite.ProgrammingError:
471 return
472 except:
473 errors.append("raised wrong exception")
474
475 errors = []
476 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
477 t.start()
478 t.join()
479 if len(errors) > 0:
480 self.fail("\n".join(errors))
481
482 def CheckConCommit(self):
483 def run(con, errors):
484 try:
485 con.commit()
486 errors.append("did not raise ProgrammingError")
487 return
488 except sqlite.ProgrammingError:
489 return
490 except:
491 errors.append("raised wrong exception")
492
493 errors = []
494 t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
495 t.start()
496 t.join()
497 if len(errors) > 0:
498 self.fail("\n".join(errors))
499
500 def CheckConRollback(self):
501 def run(con, errors):
502 try:
503 con.rollback()
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 CheckConClose(self):
519 def run(con, errors):
520 try:
521 con.close()
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 CheckCurImplicitBegin(self):
537 def run(cur, errors):
538 try:
539 cur.execute("insert into test(name) values ('a')")
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={"cur": self.cur, "errors": errors})
549 t.start()
550 t.join()
551 if len(errors) > 0:
552 self.fail("\n".join(errors))
553
554 def CheckCurClose(self):
555 def run(cur, errors):
556 try:
557 cur.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={"cur": self.cur, "errors": errors})
567 t.start()
568 t.join()
569 if len(errors) > 0:
570 self.fail("\n".join(errors))
571
572 def CheckCurExecute(self):
573 def run(cur, errors):
574 try:
575 cur.execute("select name from test")
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 self.cur.execute("insert into test(name) values ('a')")
585 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
586 t.start()
587 t.join()
588 if len(errors) > 0:
589 self.fail("\n".join(errors))
590
591 def CheckCurIterNext(self):
592 def run(cur, errors):
593 try:
594 row = cur.fetchone()
595 errors.append("did not raise ProgrammingError")
596 return
597 except sqlite.ProgrammingError:
598 return
599 except:
600 errors.append("raised wrong exception")
601
602 errors = []
603 self.cur.execute("insert into test(name) values ('a')")
604 self.cur.execute("select name from test")
605 t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
606 t.start()
607 t.join()
608 if len(errors) > 0:
609 self.fail("\n".join(errors))
610
611class ConstructorTests(unittest.TestCase):
612 def CheckDate(self):
613 d = sqlite.Date(2004, 10, 28)
614
615 def CheckTime(self):
616 t = sqlite.Time(12, 39, 35)
617
618 def CheckTimestamp(self):
619 ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35)
620
621 def CheckDateFromTicks(self):
622 d = sqlite.DateFromTicks(42)
623
624 def CheckTimeFromTicks(self):
625 t = sqlite.TimeFromTicks(42)
626
627 def CheckTimestampFromTicks(self):
628 ts = sqlite.TimestampFromTicks(42)
629
630 def CheckBinary(self):
Guido van Rossumbae07c92007-10-08 02:46:15 +0000631 b = sqlite.Binary(b"\0'")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632
633class ExtensionTests(unittest.TestCase):
634 def CheckScriptStringSql(self):
635 con = sqlite.connect(":memory:")
636 cur = con.cursor()
637 cur.executescript("""
638 -- bla bla
639 /* a stupid comment */
640 create table a(i);
641 insert into a(i) values (5);
642 """)
643 cur.execute("select i from a")
644 res = cur.fetchone()[0]
645 self.failUnlessEqual(res, 5)
646
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 def CheckScriptErrorIncomplete(self):
648 con = sqlite.connect(":memory:")
649 cur = con.cursor()
650 raised = False
651 try:
652 cur.executescript("create table test(sadfsadfdsa")
653 except sqlite.ProgrammingError:
654 raised = True
655 self.failUnlessEqual(raised, True, "should have raised an exception")
656
657 def CheckScriptErrorNormal(self):
658 con = sqlite.connect(":memory:")
659 cur = con.cursor()
660 raised = False
661 try:
662 cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
663 except sqlite.OperationalError:
664 raised = True
665 self.failUnlessEqual(raised, True, "should have raised an exception")
666
667 def CheckConnectionExecute(self):
668 con = sqlite.connect(":memory:")
669 result = con.execute("select 5").fetchone()[0]
670 self.failUnlessEqual(result, 5, "Basic test of Connection.execute")
671
672 def CheckConnectionExecutemany(self):
673 con = sqlite.connect(":memory:")
674 con.execute("create table test(foo)")
675 con.executemany("insert into test(foo) values (?)", [(3,), (4,)])
676 result = con.execute("select foo from test order by foo").fetchall()
677 self.failUnlessEqual(result[0][0], 3, "Basic test of Connection.executemany")
678 self.failUnlessEqual(result[1][0], 4, "Basic test of Connection.executemany")
679
680 def CheckConnectionExecutescript(self):
681 con = sqlite.connect(":memory:")
682 con.executescript("create table test(foo); insert into test(foo) values (5);")
683 result = con.execute("select foo from test").fetchone()[0]
684 self.failUnlessEqual(result, 5, "Basic test of Connection.executescript")
685
686class ClosedTests(unittest.TestCase):
687 def setUp(self):
688 pass
689
690 def tearDown(self):
691 pass
692
693 def CheckClosedConCursor(self):
694 con = sqlite.connect(":memory:")
695 con.close()
696 try:
697 cur = con.cursor()
698 self.fail("Should have raised a ProgrammingError")
699 except sqlite.ProgrammingError:
700 pass
701 except:
702 self.fail("Should have raised a ProgrammingError")
703
704 def CheckClosedConCommit(self):
705 con = sqlite.connect(":memory:")
706 con.close()
707 try:
708 con.commit()
709 self.fail("Should have raised a ProgrammingError")
710 except sqlite.ProgrammingError:
711 pass
712 except:
713 self.fail("Should have raised a ProgrammingError")
714
715 def CheckClosedConRollback(self):
716 con = sqlite.connect(":memory:")
717 con.close()
718 try:
719 con.rollback()
720 self.fail("Should have raised a ProgrammingError")
721 except sqlite.ProgrammingError:
722 pass
723 except:
724 self.fail("Should have raised a ProgrammingError")
725
726 def CheckClosedCurExecute(self):
727 con = sqlite.connect(":memory:")
728 cur = con.cursor()
729 con.close()
730 try:
731 cur.execute("select 4")
732 self.fail("Should have raised a ProgrammingError")
733 except sqlite.ProgrammingError:
734 pass
735 except:
736 self.fail("Should have raised a ProgrammingError")
737
738def suite():
739 module_suite = unittest.makeSuite(ModuleTests, "Check")
740 connection_suite = unittest.makeSuite(ConnectionTests, "Check")
741 cursor_suite = unittest.makeSuite(CursorTests, "Check")
742 thread_suite = unittest.makeSuite(ThreadTests, "Check")
743 constructor_suite = unittest.makeSuite(ConstructorTests, "Check")
744 ext_suite = unittest.makeSuite(ExtensionTests, "Check")
745 closed_suite = unittest.makeSuite(ClosedTests, "Check")
746 return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_suite))
747
748def test():
749 runner = unittest.TextTestRunner()
750 runner.run(suite())
751
752if __name__ == "__main__":
753 test()