|
1 #-*- coding: ISO-8859-1 -*- |
|
2 # pysqlite2/test/regression.py: pysqlite regression tests |
|
3 # |
|
4 # Copyright (C) 2007 Gerhard Häring <gh@ghaering.de> |
|
5 # |
|
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 |
|
24 from __future__ import with_statement |
|
25 import unittest |
|
26 import sqlite3 as sqlite |
|
27 |
|
28 did_rollback = False |
|
29 |
|
30 class MyConnection(sqlite.Connection): |
|
31 def rollback(self): |
|
32 global did_rollback |
|
33 did_rollback = True |
|
34 sqlite.Connection.rollback(self) |
|
35 |
|
36 class ContextTests(unittest.TestCase): |
|
37 def setUp(self): |
|
38 global did_rollback |
|
39 self.con = sqlite.connect(":memory:", factory=MyConnection) |
|
40 self.con.execute("create table test(c unique)") |
|
41 did_rollback = False |
|
42 |
|
43 def tearDown(self): |
|
44 self.con.close() |
|
45 |
|
46 def CheckContextManager(self): |
|
47 """Can the connection be used as a context manager at all?""" |
|
48 with self.con: |
|
49 pass |
|
50 |
|
51 def CheckContextManagerCommit(self): |
|
52 """Is a commit called in the context manager?""" |
|
53 with self.con: |
|
54 self.con.execute("insert into test(c) values ('foo')") |
|
55 self.con.rollback() |
|
56 count = self.con.execute("select count(*) from test").fetchone()[0] |
|
57 self.failUnlessEqual(count, 1) |
|
58 |
|
59 def CheckContextManagerRollback(self): |
|
60 """Is a rollback called in the context manager?""" |
|
61 global did_rollback |
|
62 self.failUnlessEqual(did_rollback, False) |
|
63 try: |
|
64 with self.con: |
|
65 self.con.execute("insert into test(c) values (4)") |
|
66 self.con.execute("insert into test(c) values (4)") |
|
67 except sqlite.IntegrityError: |
|
68 pass |
|
69 self.failUnlessEqual(did_rollback, True) |
|
70 |
|
71 def suite(): |
|
72 ctx_suite = unittest.makeSuite(ContextTests, "Check") |
|
73 return unittest.TestSuite((ctx_suite,)) |
|
74 |
|
75 def test(): |
|
76 runner = unittest.TextTestRunner() |
|
77 runner.run(suite()) |
|
78 |
|
79 if __name__ == "__main__": |
|
80 test() |