14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#-*- coding: ISO-8859-1 -*-
24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# pysqlite2/test/regression.py: pysqlite regression tests
34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#
44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Copyright (C) 2007 Gerhard H�ring <gh@ghaering.de>
54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#
64adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# This file is part of pysqlite.
74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#
84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# This software is provided 'as-is', without any express or implied
94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# warranty.  In no event will the authors be held liable for any damages
104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# arising from the use of this software.
114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#
124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Permission is granted to anyone to use this software for any purpose,
134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# including commercial applications, and to alter it and redistribute it
144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# freely, subject to the following restrictions:
154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#
164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# 1. The origin of this software must not be misrepresented; you must not
174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#    claim that you wrote the original software. If you use this software
184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#    in a product, an acknowledgment in the product documentation would be
194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#    appreciated but is not required.
204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# 2. Altered source versions must be plainly marked as such, and must not be
214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao#    misrepresented as being the original software.
224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# 3. This notice may not be removed or altered from any source distribution.
234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofrom __future__ import with_statement
254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport unittest
264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport sqlite3 as sqlite
274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodid_rollback = False
294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass MyConnection(sqlite.Connection):
314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def rollback(self):
324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        global did_rollback
334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        did_rollback = True
344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        sqlite.Connection.rollback(self)
354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass ContextTests(unittest.TestCase):
374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def setUp(self):
384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        global did_rollback
394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.con = sqlite.connect(":memory:", factory=MyConnection)
404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.con.execute("create table test(c unique)")
414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        did_rollback = False
424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def tearDown(self):
444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.con.close()
454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def CheckContextManager(self):
474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Can the connection be used as a context manager at all?"""
484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        with self.con:
494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def CheckContextManagerCommit(self):
524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Is a commit called in the context manager?"""
534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        with self.con:
544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.con.execute("insert into test(c) values ('foo')")
554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.con.rollback()
564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        count = self.con.execute("select count(*) from test").fetchone()[0]
574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(count, 1)
584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def CheckContextManagerRollback(self):
604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Is a rollback called in the context manager?"""
614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        global did_rollback
624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(did_rollback, False)
634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            with self.con:
654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.con.execute("insert into test(c) values (4)")
664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.con.execute("insert into test(c) values (4)")
674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except sqlite.IntegrityError:
684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertEqual(did_rollback, True)
704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef suite():
724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    ctx_suite = unittest.makeSuite(ContextTests, "Check")
734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    return unittest.TestSuite((ctx_suite,))
744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef test():
764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    runner = unittest.TextTestRunner()
774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    runner.run(suite())
784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif __name__ == "__main__":
804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test()
81