1ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#-*- coding: ISO-8859-1 -*-
2ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# pysqlite2/test/regression.py: pysqlite regression tests
3ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#
4ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# Copyright (C) 2007 Gerhard H�ring <gh@ghaering.de>
5ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#
6ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# This file is part of pysqlite.
7ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#
8ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# This software is provided 'as-is', without any express or implied
9ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# warranty.  In no event will the authors be held liable for any damages
10ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# arising from the use of this software.
11ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#
12ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# Permission is granted to anyone to use this software for any purpose,
13ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# including commercial applications, and to alter it and redistribute it
14ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# freely, subject to the following restrictions:
15ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#
16ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# 1. The origin of this software must not be misrepresented; you must not
17ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#    claim that you wrote the original software. If you use this software
18ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#    in a product, an acknowledgment in the product documentation would be
19ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#    appreciated but is not required.
20ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# 2. Altered source versions must be plainly marked as such, and must not be
21ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot#    misrepresented as being the original software.
22ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot# 3. This notice may not be removed or altered from any source distribution.
23ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
24ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotfrom __future__ import with_statement
25ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotimport unittest
26ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotimport sqlite3 as sqlite
27ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
28ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotdid_rollback = False
29ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
30ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotclass MyConnection(sqlite.Connection):
31ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def rollback(self):
32ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        global did_rollback
33ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        did_rollback = True
34ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        sqlite.Connection.rollback(self)
35ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
36ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotclass ContextTests(unittest.TestCase):
37ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def setUp(self):
38ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        global did_rollback
39ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.con = sqlite.connect(":memory:", factory=MyConnection)
40ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.con.execute("create table test(c unique)")
41ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        did_rollback = False
42ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
43ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def tearDown(self):
44ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.con.close()
45ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
46ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def CheckContextManager(self):
47ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        """Can the connection be used as a context manager at all?"""
48ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        with self.con:
49ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot            pass
50ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
51ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def CheckContextManagerCommit(self):
52ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        """Is a commit called in the context manager?"""
53ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        with self.con:
54ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot            self.con.execute("insert into test(c) values ('foo')")
55ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.con.rollback()
56ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        count = self.con.execute("select count(*) from test").fetchone()[0]
57ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.assertEqual(count, 1)
58ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
59ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    def CheckContextManagerRollback(self):
60ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        """Is a rollback called in the context manager?"""
61ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        global did_rollback
62ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.assertEqual(did_rollback, False)
63ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        try:
64ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot            with self.con:
65ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot                self.con.execute("insert into test(c) values (4)")
66ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot                self.con.execute("insert into test(c) values (4)")
67ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        except sqlite.IntegrityError:
68ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot            pass
69ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot        self.assertEqual(did_rollback, True)
70ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
71ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotdef suite():
72ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    ctx_suite = unittest.makeSuite(ContextTests, "Check")
73ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    return unittest.TestSuite((ctx_suite,))
74ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
75ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotdef test():
76ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    runner = unittest.TextTestRunner()
77ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    runner.run(suite())
78ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot
79ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robotif __name__ == "__main__":
80ec1a0b3abe08fb9a3952e8f48231cda1f6d9b1fandroid-build-team Robot    test()
81