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