blob: 7fd3c0e0cf81e1a0b39e8d9d16c310111f5f0c02 [file] [log] [blame]
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001#-*- 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
24from __future__ import with_statement
25import unittest
26import sqlite3 as sqlite
27
28did_rollback = False
29
30class MyConnection(sqlite.Connection):
31 def rollback(self):
32 global did_rollback
33 did_rollback = True
34 sqlite.Connection.rollback(self)
35
36class 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]
Gregory P. Smith1844b0d2009-07-04 08:42:10 +000057 self.assertEqual(count, 1)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000058
59 def CheckContextManagerRollback(self):
60 """Is a rollback called in the context manager?"""
61 global did_rollback
Gregory P. Smith1844b0d2009-07-04 08:42:10 +000062 self.assertEqual(did_rollback, False)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000063 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
Gregory P. Smith1844b0d2009-07-04 08:42:10 +000069 self.assertEqual(did_rollback, True)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000070
71def suite():
72 ctx_suite = unittest.makeSuite(ContextTests, "Check")
73 return unittest.TestSuite((ctx_suite,))
74
75def test():
76 runner = unittest.TextTestRunner()
77 runner.run(suite())
78
79if __name__ == "__main__":
80 test()