1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#-*- coding: ISO-8859-1 -*-
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# pysqlite2/test/dbapi.py: tests for DB-API compliance
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copyright (C) 2004-2010 Gerhard H�ring <gh@ghaering.de>
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This file is part of pysqlite.
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This software is provided 'as-is', without any express or implied
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# warranty.  In no event will the authors be held liable for any damages
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# arising from the use of this software.
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Permission is granted to anyone to use this software for any purpose,
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# including commercial applications, and to alter it and redistribute it
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# freely, subject to the following restrictions:
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 1. The origin of this software must not be misrepresented; you must not
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    claim that you wrote the original software. If you use this software
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    in a product, an acknowledgment in the product documentation would be
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    appreciated but is not required.
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 2. Altered source versions must be plainly marked as such, and must not be
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#    misrepresented as being the original software.
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 3. This notice may not be removed or altered from any source distribution.
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sqlite3 as sqlite
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import threading
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept ImportError:
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    threading = None
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ModuleTests(unittest.TestCase):
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckAPILevel(self):
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sqlite.apilevel, "2.0",
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         "apilevel is %s, should be 2.0" % sqlite.apilevel)
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckThreadSafety(self):
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sqlite.threadsafety, 1,
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         "threadsafety is %d, should be 1" % sqlite.threadsafety)
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckParamStyle(self):
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sqlite.paramstyle, "qmark",
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         "paramstyle is '%s', should be 'qmark'" %
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         sqlite.paramstyle)
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckWarning(self):
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.Warning, StandardError),
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "Warning is not a subclass of StandardError")
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckError(self):
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.Error, StandardError),
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "Error is not a subclass of StandardError")
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckInterfaceError(self):
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error),
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "InterfaceError is not a subclass of Error")
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckDatabaseError(self):
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error),
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "DatabaseError is not a subclass of Error")
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckDataError(self):
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError),
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "DataError is not a subclass of DatabaseError")
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckOperationalError(self):
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError),
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "OperationalError is not a subclass of DatabaseError")
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckIntegrityError(self):
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError),
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "IntegrityError is not a subclass of DatabaseError")
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckInternalError(self):
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError),
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "InternalError is not a subclass of DatabaseError")
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckProgrammingError(self):
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError),
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "ProgrammingError is not a subclass of DatabaseError")
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckNotSupportedError(self):
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(sqlite.NotSupportedError,
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                   sqlite.DatabaseError),
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "NotSupportedError is not a subclass of DatabaseError")
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ConnectionTests(unittest.TestCase):
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx = sqlite.connect(":memory:")
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cu = self.cx.cursor()
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cu.execute("create table test(id integer primary key, name text)")
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cu.execute("insert into test(name) values (?)", ("foo",))
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.close()
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCommit(self):
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.commit()
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCommitAfterNoChanges(self):
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A commit should also work when no changes were made to the database.
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.commit()
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.commit()
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckRollback(self):
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.rollback()
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckRollbackAfterNoChanges(self):
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A rollback should also work when no changes were made to the database.
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.rollback()
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.rollback()
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCursor(self):
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cu = self.cx.cursor()
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFailedOpen(self):
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.OperationalError:
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.fail("should have raised an OperationalError")
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClose(self):
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.close()
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExceptions(self):
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Optional DB-API extension.
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.Warning, sqlite.Warning)
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.Error, sqlite.Error)
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError)
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError)
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.DataError, sqlite.DataError)
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.OperationalError, sqlite.OperationalError)
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError)
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.InternalError, sqlite.InternalError)
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError)
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError)
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CursorTests(unittest.TestCase):
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx = sqlite.connect(":memory:")
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu = self.cx.cursor()
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("create table test(id integer primary key, name text, income number)")
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values (?)", ("foo",))
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.close()
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cx.close()
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteNoArgs(self):
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("delete from test")
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteIllegalSql(self):
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("select asdf")
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised an OperationalError")
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.OperationalError:
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception")
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteTooMuchSql(self):
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("select 5+4; select 4+5")
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a Warning")
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.Warning:
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception")
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteTooMuchSql2(self):
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select 5+4; -- foo bar")
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteTooMuchSql3(self):
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("""
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            select 5+4;
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            /*
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            foo
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            */
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """)
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteWrongSqlArg(self):
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute(42)
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a ValueError")
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError:
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception.")
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteArgInt(self):
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(id) values (?)", (42,))
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteArgFloat(self):
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(income) values (?)", (2500.32,))
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteArgString(self):
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values (?)", ("Hugo",))
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteArgStringWithZeroByte(self):
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",))
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test where id=?", (self.cu.lastrowid,))
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "Hu\x00go")
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteWrongNoOfArgs1(self):
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # too many parameters
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteWrongNoOfArgs2(self):
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # too little parameters
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("insert into test(id) values (?)")
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteWrongNoOfArgs3(self):
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # no parameters, parameters are needed
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("insert into test(id) values (?)")
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteParamList(self):
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test where name=?", ["foo"])
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "foo")
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteParamSequence(self):
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(object):
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __len__(self):
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 1
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, x):
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                assert x == 0
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "foo"
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test where name=?", L())
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "foo")
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteDictMapping(self):
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test where name=:name", {"name": "foo"})
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "foo")
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteDictMapping_Mapping(self):
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test only works with Python 2.5 or later
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if sys.version_info < (2, 5, 0):
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(dict):
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __missing__(self, key):
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "foo"
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test where name=:name", D())
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "foo")
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteDictMappingTooLittleArgs(self):
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteDictMappingNoArgs(self):
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("select name from test where name=:name")
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteDictMappingUnnamed(self):
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.execute("select name from test where name=?", {"name": "foo"})
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised ProgrammingError")
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClose(self):
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.close()
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckRowcountExecute(self):
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("delete from test")
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("update test set name='bar'")
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cu.rowcount, 2)
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckRowcountSelect(self):
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pysqlite does not know the rowcount of SELECT statements, because we
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        don't fetch all rows after executing the select statement. The rowcount
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        has thus to be -1.
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select 5 union select 6")
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cu.rowcount, -1)
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckRowcountExecutemany(self):
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("delete from test")
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cu.rowcount, 3)
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckTotalChanges(self):
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('foo')")
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.cx.total_changes < 2:
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("total changes reported wrong value")
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Checks for executemany:
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Sequences are required by the DB-API, iterators
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # enhancements in pysqlite.
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManySequence(self):
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)])
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManyIterator(self):
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyIter:
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.value = 5
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def next(self):
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self.value == 10:
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise StopIteration
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.value += 1
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return (self.value,)
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.executemany("insert into test(income) values (?)", MyIter())
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManyGenerator(self):
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def mygen():
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for i in range(5):
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                yield (i,)
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.executemany("insert into test(income) values (?)", mygen())
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManyWrongSqlArg(self):
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.executemany(42, [(3,)])
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a ValueError")
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError:
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception.")
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManySelect(self):
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.executemany("select ?", [(3,)])
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a ProgrammingError")
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception.")
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckExecuteManyNotIterable(self):
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.cu.executemany("insert into test(income) values (?)", 42)
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a TypeError")
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except Exception, e:
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            print "raised", e.__class__
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("raised wrong exception.")
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchIter(self):
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Optional DB-API extension.
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("delete from test")
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(id) values (?)", (5,))
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(id) values (?)", (6,))
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select id from test order by id")
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst = []
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for row in self.cu:
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            lst.append(row[0])
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst[0], 5)
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst[1], 6)
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchone(self):
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test")
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row[0], "foo")
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = self.cu.fetchone()
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row, None)
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchoneNoStatement(self):
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = self.cx.cursor()
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        row = cur.fetchone()
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(row, None)
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckArraySize(self):
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # must default ot 1
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cu.arraysize, 1)
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # now set to 2
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.arraysize = 2
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # now make the query return 3 rows
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("delete from test")
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('A')")
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('B')")
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("insert into test(name) values ('C')")
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test")
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchmany()
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(res), 2)
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchmany(self):
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test")
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchmany(100)
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(res), 1)
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchmany(100)
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(res, [])
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchmanyKwArg(self):
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Checks if fetchmany works with keyword arguments"""
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test")
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchmany(size=100)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(res), 1)
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckFetchall(self):
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.execute("select name from test")
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchall()
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(res), 1)
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = self.cu.fetchall()
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(res, [])
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckSetinputsizes(self):
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.setinputsizes([3, 4, 5])
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckSetoutputsize(self):
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.setoutputsize(5, 0)
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckSetoutputsizeNoColumn(self):
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cu.setoutputsize(42)
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCursorConnection(self):
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Optional DB-API extension.
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.cu.connection, self.cx)
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckWrongCursorCallable(self):
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def f(): pass
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur = self.cx.cursor(f)
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a TypeError")
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.fail("should have raised a ValueError")
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCursorWrongClass(self):
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo: pass
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        foo = Foo()
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur = sqlite.Cursor(foo)
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised a ValueError")
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep@unittest.skipUnless(threading, 'This test requires threading.')
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ThreadTests(unittest.TestCase):
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.con = sqlite.connect(":memory:")
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur = self.con.cursor()
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)")
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur.close()
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.con.close()
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConCursor(self):
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(con, errors):
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cur = con.cursor()
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConCommit(self):
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(con, errors):
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                con.commit()
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConRollback(self):
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(con, errors):
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                con.rollback()
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConClose(self):
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(con, errors):
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                con.close()
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCurImplicitBegin(self):
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(cur, errors):
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cur.execute("insert into test(name) values ('a')")
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCurClose(self):
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(cur, errors):
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cur.close()
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCurExecute(self):
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(cur, errors):
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cur.execute("select name from test")
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur.execute("insert into test(name) values ('a')")
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckCurIterNext(self):
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run(cur, errors):
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                row = cur.fetchone()
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("did not raise ProgrammingError")
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                errors.append("raised wrong exception")
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        errors = []
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur.execute("insert into test(name) values ('a')")
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.cur.execute("select name from test")
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(errors) > 0:
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("\n".join(errors))
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ConstructorTests(unittest.TestCase):
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckDate(self):
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = sqlite.Date(2004, 10, 28)
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckTime(self):
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = sqlite.Time(12, 39, 35)
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckTimestamp(self):
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35)
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckDateFromTicks(self):
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = sqlite.DateFromTicks(42)
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckTimeFromTicks(self):
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = sqlite.TimeFromTicks(42)
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckTimestampFromTicks(self):
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ts = sqlite.TimestampFromTicks(42)
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckBinary(self):
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = sqlite.Binary(chr(0) + "'")
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ExtensionTests(unittest.TestCase):
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckScriptStringSql(self):
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur.executescript("""
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            -- bla bla
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            /* a stupid comment */
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            create table a(i);
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            insert into a(i) values (5);
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """)
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur.execute("select i from a")
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = cur.fetchone()[0]
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(res, 5)
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckScriptStringUnicode(self):
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur.executescript(u"""
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            create table a(i);
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            insert into a(i) values (5);
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            select i from a;
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            delete from a;
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            insert into a(i) values (6);
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """)
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur.execute("select i from a")
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = cur.fetchone()[0]
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(res, 6)
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckScriptSyntaxError(self):
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raised = False
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur.executescript("create table test(x); asdf; create table test2(x)")
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.OperationalError:
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raised = True
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(raised, True, "should have raised an exception")
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckScriptErrorNormal(self):
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raised = False
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.OperationalError:
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raised = True
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(raised, True, "should have raised an exception")
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConnectionExecute(self):
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        result = con.execute("select 5").fetchone()[0]
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(result, 5, "Basic test of Connection.execute")
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConnectionExecutemany(self):
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.execute("create table test(foo)")
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.executemany("insert into test(foo) values (?)", [(3,), (4,)])
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        result = con.execute("select foo from test order by foo").fetchall()
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany")
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany")
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckConnectionExecutescript(self):
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.executescript("create table test(foo); insert into test(foo) values (5);")
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        result = con.execute("select foo from test").fetchone()[0]
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(result, 5, "Basic test of Connection.executescript")
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClosedConTests(unittest.TestCase):
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedConCursor(self):
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur = con.cursor()
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedConCommit(self):
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.commit()
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedConRollback(self):
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.rollback()
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedCurExecute(self):
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cur.execute("select 4")
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedCreateFunction(self):
778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def f(x): return 17
781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.create_function("foo", 1, f)
783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedCreateAggregate(self):
790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Agg:
793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def step(self, x):
796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def finalize(self):
798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 17
799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.create_aggregate("foo", 1, Agg)
801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedSetAuthorizer(self):
808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def authorizer(*args):
811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return sqlite.DENY
812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.set_authorizer(authorizer)
814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedSetProgressCallback(self):
821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def progress(): pass
824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con.set_progress_handler(progress, 100)
826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosedCall(self):
833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con.close()
835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            con()
837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except sqlite.ProgrammingError:
839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Should have raised a ProgrammingError")
842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClosedCurTests(unittest.TestCase):
844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def CheckClosed(self):
851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        con = sqlite.connect(":memory:")
852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur = con.cursor()
853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cur.close()
854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"):
856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if method_name in ("execute", "executescript"):
857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                params = ("select 4 union select 5",)
858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            elif method_name == "executemany":
859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                params = ("insert into foo(bar) values (?)", [(3,), (4,)])
860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                params = []
862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                method = getattr(cur, method_name)
865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                method(*params)
867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("Should have raised a ProgrammingError: method " + method_name)
868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except sqlite.ProgrammingError:
869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("Should have raised a ProgrammingError: " + method_name)
872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef suite():
874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    module_suite = unittest.makeSuite(ModuleTests, "Check")
875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    connection_suite = unittest.makeSuite(ConnectionTests, "Check")
876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    cursor_suite = unittest.makeSuite(CursorTests, "Check")
877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    thread_suite = unittest.makeSuite(ThreadTests, "Check")
878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    constructor_suite = unittest.makeSuite(ConstructorTests, "Check")
879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ext_suite = unittest.makeSuite(ExtensionTests, "Check")
880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    closed_con_suite = unittest.makeSuite(ClosedConTests, "Check")
881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    closed_cur_suite = unittest.makeSuite(ClosedCurTests, "Check")
882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_con_suite, closed_cur_suite))
883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test():
885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    runner = unittest.TextTestRunner()
886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    runner.run(suite())
887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test()
890