Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | #-*- coding: ISO-8859-1 -*- |
| 2 | # pysqlite2/test/dbapi.py: tests for DB-API compliance |
| 3 | # |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 4 | # Copyright (C) 2004-2007 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 |
| 25 | import threading |
| 26 | import sqlite3 as sqlite |
| 27 | |
| 28 | class ModuleTests(unittest.TestCase): |
| 29 | def CheckAPILevel(self): |
| 30 | self.assertEqual(sqlite.apilevel, "2.0", |
| 31 | "apilevel is %s, should be 2.0" % sqlite.apilevel) |
| 32 | |
| 33 | def CheckThreadSafety(self): |
| 34 | self.assertEqual(sqlite.threadsafety, 1, |
| 35 | "threadsafety is %d, should be 1" % sqlite.threadsafety) |
| 36 | |
| 37 | def CheckParamStyle(self): |
| 38 | self.assertEqual(sqlite.paramstyle, "qmark", |
| 39 | "paramstyle is '%s', should be 'qmark'" % |
| 40 | sqlite.paramstyle) |
| 41 | |
| 42 | def CheckWarning(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 43 | self.assert_(issubclass(sqlite.Warning, Exception), |
| 44 | "Warning is not a subclass of Exception") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | |
| 46 | def CheckError(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 47 | self.failUnless(issubclass(sqlite.Error, Exception), |
| 48 | "Error is not a subclass of Exception") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 49 | |
| 50 | def CheckInterfaceError(self): |
| 51 | self.failUnless(issubclass(sqlite.InterfaceError, sqlite.Error), |
| 52 | "InterfaceError is not a subclass of Error") |
| 53 | |
| 54 | def CheckDatabaseError(self): |
| 55 | self.failUnless(issubclass(sqlite.DatabaseError, sqlite.Error), |
| 56 | "DatabaseError is not a subclass of Error") |
| 57 | |
| 58 | def CheckDataError(self): |
| 59 | self.failUnless(issubclass(sqlite.DataError, sqlite.DatabaseError), |
| 60 | "DataError is not a subclass of DatabaseError") |
| 61 | |
| 62 | def CheckOperationalError(self): |
| 63 | self.failUnless(issubclass(sqlite.OperationalError, sqlite.DatabaseError), |
| 64 | "OperationalError is not a subclass of DatabaseError") |
| 65 | |
| 66 | def CheckIntegrityError(self): |
| 67 | self.failUnless(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), |
| 68 | "IntegrityError is not a subclass of DatabaseError") |
| 69 | |
| 70 | def CheckInternalError(self): |
| 71 | self.failUnless(issubclass(sqlite.InternalError, sqlite.DatabaseError), |
| 72 | "InternalError is not a subclass of DatabaseError") |
| 73 | |
| 74 | def CheckProgrammingError(self): |
| 75 | self.failUnless(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), |
| 76 | "ProgrammingError is not a subclass of DatabaseError") |
| 77 | |
| 78 | def CheckNotSupportedError(self): |
| 79 | self.failUnless(issubclass(sqlite.NotSupportedError, |
| 80 | sqlite.DatabaseError), |
| 81 | "NotSupportedError is not a subclass of DatabaseError") |
| 82 | |
| 83 | class ConnectionTests(unittest.TestCase): |
| 84 | def setUp(self): |
| 85 | self.cx = sqlite.connect(":memory:") |
| 86 | cu = self.cx.cursor() |
| 87 | cu.execute("create table test(id integer primary key, name text)") |
| 88 | cu.execute("insert into test(name) values (?)", ("foo",)) |
| 89 | |
| 90 | def tearDown(self): |
| 91 | self.cx.close() |
| 92 | |
| 93 | def CheckCommit(self): |
| 94 | self.cx.commit() |
| 95 | |
| 96 | def CheckCommitAfterNoChanges(self): |
| 97 | """ |
| 98 | A commit should also work when no changes were made to the database. |
| 99 | """ |
| 100 | self.cx.commit() |
| 101 | self.cx.commit() |
| 102 | |
| 103 | def CheckRollback(self): |
| 104 | self.cx.rollback() |
| 105 | |
| 106 | def CheckRollbackAfterNoChanges(self): |
| 107 | """ |
| 108 | A rollback should also work when no changes were made to the database. |
| 109 | """ |
| 110 | self.cx.rollback() |
| 111 | self.cx.rollback() |
| 112 | |
| 113 | def CheckCursor(self): |
| 114 | cu = self.cx.cursor() |
| 115 | |
| 116 | def CheckFailedOpen(self): |
| 117 | YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" |
| 118 | try: |
| 119 | con = sqlite.connect(YOU_CANNOT_OPEN_THIS) |
| 120 | except sqlite.OperationalError: |
| 121 | return |
| 122 | self.fail("should have raised an OperationalError") |
| 123 | |
| 124 | def CheckClose(self): |
| 125 | self.cx.close() |
| 126 | |
| 127 | def CheckExceptions(self): |
| 128 | # Optional DB-API extension. |
| 129 | self.failUnlessEqual(self.cx.Warning, sqlite.Warning) |
| 130 | self.failUnlessEqual(self.cx.Error, sqlite.Error) |
| 131 | self.failUnlessEqual(self.cx.InterfaceError, sqlite.InterfaceError) |
| 132 | self.failUnlessEqual(self.cx.DatabaseError, sqlite.DatabaseError) |
| 133 | self.failUnlessEqual(self.cx.DataError, sqlite.DataError) |
| 134 | self.failUnlessEqual(self.cx.OperationalError, sqlite.OperationalError) |
| 135 | self.failUnlessEqual(self.cx.IntegrityError, sqlite.IntegrityError) |
| 136 | self.failUnlessEqual(self.cx.InternalError, sqlite.InternalError) |
| 137 | self.failUnlessEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) |
| 138 | self.failUnlessEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) |
| 139 | |
| 140 | class CursorTests(unittest.TestCase): |
| 141 | def setUp(self): |
| 142 | self.cx = sqlite.connect(":memory:") |
| 143 | self.cu = self.cx.cursor() |
| 144 | self.cu.execute("create table test(id integer primary key, name text, income number)") |
| 145 | self.cu.execute("insert into test(name) values (?)", ("foo",)) |
| 146 | |
| 147 | def tearDown(self): |
| 148 | self.cu.close() |
| 149 | self.cx.close() |
| 150 | |
| 151 | def CheckExecuteNoArgs(self): |
| 152 | self.cu.execute("delete from test") |
| 153 | |
| 154 | def CheckExecuteIllegalSql(self): |
| 155 | try: |
| 156 | self.cu.execute("select asdf") |
| 157 | self.fail("should have raised an OperationalError") |
| 158 | except sqlite.OperationalError: |
| 159 | return |
| 160 | except: |
| 161 | self.fail("raised wrong exception") |
| 162 | |
| 163 | def CheckExecuteTooMuchSql(self): |
| 164 | try: |
| 165 | self.cu.execute("select 5+4; select 4+5") |
| 166 | self.fail("should have raised a Warning") |
| 167 | except sqlite.Warning: |
| 168 | return |
| 169 | except: |
| 170 | self.fail("raised wrong exception") |
| 171 | |
| 172 | def CheckExecuteTooMuchSql2(self): |
| 173 | self.cu.execute("select 5+4; -- foo bar") |
| 174 | |
| 175 | def CheckExecuteTooMuchSql3(self): |
| 176 | self.cu.execute(""" |
| 177 | select 5+4; |
| 178 | |
| 179 | /* |
| 180 | foo |
| 181 | */ |
| 182 | """) |
| 183 | |
| 184 | def CheckExecuteWrongSqlArg(self): |
| 185 | try: |
| 186 | self.cu.execute(42) |
| 187 | self.fail("should have raised a ValueError") |
| 188 | except ValueError: |
| 189 | return |
| 190 | except: |
| 191 | self.fail("raised wrong exception.") |
| 192 | |
| 193 | def CheckExecuteArgInt(self): |
| 194 | self.cu.execute("insert into test(id) values (?)", (42,)) |
| 195 | |
| 196 | def CheckExecuteArgFloat(self): |
| 197 | self.cu.execute("insert into test(income) values (?)", (2500.32,)) |
| 198 | |
| 199 | def CheckExecuteArgString(self): |
| 200 | self.cu.execute("insert into test(name) values (?)", ("Hugo",)) |
| 201 | |
| 202 | def CheckExecuteWrongNoOfArgs1(self): |
| 203 | # too many parameters |
| 204 | try: |
| 205 | self.cu.execute("insert into test(id) values (?)", (17, "Egon")) |
| 206 | self.fail("should have raised ProgrammingError") |
| 207 | except sqlite.ProgrammingError: |
| 208 | pass |
| 209 | |
| 210 | def CheckExecuteWrongNoOfArgs2(self): |
| 211 | # too little parameters |
| 212 | try: |
| 213 | self.cu.execute("insert into test(id) values (?)") |
| 214 | self.fail("should have raised ProgrammingError") |
| 215 | except sqlite.ProgrammingError: |
| 216 | pass |
| 217 | |
| 218 | def CheckExecuteWrongNoOfArgs3(self): |
| 219 | # no parameters, parameters are needed |
| 220 | try: |
| 221 | self.cu.execute("insert into test(id) values (?)") |
| 222 | self.fail("should have raised ProgrammingError") |
| 223 | except sqlite.ProgrammingError: |
| 224 | pass |
| 225 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 226 | def CheckExecuteParamList(self): |
| 227 | self.cu.execute("insert into test(name) values ('foo')") |
| 228 | self.cu.execute("select name from test where name=?", ["foo"]) |
| 229 | row = self.cu.fetchone() |
| 230 | self.failUnlessEqual(row[0], "foo") |
| 231 | |
| 232 | def CheckExecuteParamSequence(self): |
| 233 | class L(object): |
| 234 | def __len__(self): |
| 235 | return 1 |
| 236 | def __getitem__(self, x): |
| 237 | assert x == 0 |
| 238 | return "foo" |
| 239 | |
| 240 | self.cu.execute("insert into test(name) values ('foo')") |
| 241 | self.cu.execute("select name from test where name=?", L()) |
| 242 | row = self.cu.fetchone() |
| 243 | self.failUnlessEqual(row[0], "foo") |
| 244 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 245 | def CheckExecuteDictMapping(self): |
| 246 | self.cu.execute("insert into test(name) values ('foo')") |
| 247 | self.cu.execute("select name from test where name=:name", {"name": "foo"}) |
| 248 | row = self.cu.fetchone() |
| 249 | self.failUnlessEqual(row[0], "foo") |
| 250 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 251 | def CheckExecuteDictMapping_Mapping(self): |
| 252 | class D(dict): |
| 253 | def __missing__(self, key): |
| 254 | return "foo" |
| 255 | |
| 256 | self.cu.execute("insert into test(name) values ('foo')") |
| 257 | self.cu.execute("select name from test where name=:name", D()) |
| 258 | row = self.cu.fetchone() |
| 259 | self.failUnlessEqual(row[0], "foo") |
| 260 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | def CheckExecuteDictMappingTooLittleArgs(self): |
| 262 | self.cu.execute("insert into test(name) values ('foo')") |
| 263 | try: |
| 264 | self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) |
| 265 | self.fail("should have raised ProgrammingError") |
| 266 | except sqlite.ProgrammingError: |
| 267 | pass |
| 268 | |
| 269 | def CheckExecuteDictMappingNoArgs(self): |
| 270 | self.cu.execute("insert into test(name) values ('foo')") |
| 271 | try: |
| 272 | self.cu.execute("select name from test where name=:name") |
| 273 | self.fail("should have raised ProgrammingError") |
| 274 | except sqlite.ProgrammingError: |
| 275 | pass |
| 276 | |
| 277 | def CheckExecuteDictMappingUnnamed(self): |
| 278 | self.cu.execute("insert into test(name) values ('foo')") |
| 279 | try: |
| 280 | self.cu.execute("select name from test where name=?", {"name": "foo"}) |
| 281 | self.fail("should have raised ProgrammingError") |
| 282 | except sqlite.ProgrammingError: |
| 283 | pass |
| 284 | |
| 285 | def CheckClose(self): |
| 286 | self.cu.close() |
| 287 | |
| 288 | def CheckRowcountExecute(self): |
| 289 | self.cu.execute("delete from test") |
| 290 | self.cu.execute("insert into test(name) values ('foo')") |
| 291 | self.cu.execute("insert into test(name) values ('foo')") |
| 292 | self.cu.execute("update test set name='bar'") |
| 293 | self.failUnlessEqual(self.cu.rowcount, 2) |
| 294 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 295 | def CheckRowcountSelect(self): |
| 296 | """ |
| 297 | pysqlite does not know the rowcount of SELECT statements, because we |
| 298 | don't fetch all rows after executing the select statement. The rowcount |
| 299 | has thus to be -1. |
| 300 | """ |
| 301 | self.cu.execute("select 5 union select 6") |
| 302 | self.failUnlessEqual(self.cu.rowcount, -1) |
| 303 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 304 | def CheckRowcountExecutemany(self): |
| 305 | self.cu.execute("delete from test") |
| 306 | self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) |
| 307 | self.failUnlessEqual(self.cu.rowcount, 3) |
| 308 | |
| 309 | def CheckTotalChanges(self): |
| 310 | self.cu.execute("insert into test(name) values ('foo')") |
| 311 | self.cu.execute("insert into test(name) values ('foo')") |
| 312 | if self.cx.total_changes < 2: |
| 313 | self.fail("total changes reported wrong value") |
| 314 | |
| 315 | # Checks for executemany: |
| 316 | # Sequences are required by the DB-API, iterators |
| 317 | # enhancements in pysqlite. |
| 318 | |
| 319 | def CheckExecuteManySequence(self): |
| 320 | self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) |
| 321 | |
| 322 | def CheckExecuteManyIterator(self): |
| 323 | class MyIter: |
| 324 | def __init__(self): |
| 325 | self.value = 5 |
| 326 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 327 | def __next__(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 328 | if self.value == 10: |
| 329 | raise StopIteration |
| 330 | else: |
| 331 | self.value += 1 |
| 332 | return (self.value,) |
| 333 | |
| 334 | self.cu.executemany("insert into test(income) values (?)", MyIter()) |
| 335 | |
| 336 | def CheckExecuteManyGenerator(self): |
| 337 | def mygen(): |
| 338 | for i in range(5): |
| 339 | yield (i,) |
| 340 | |
| 341 | self.cu.executemany("insert into test(income) values (?)", mygen()) |
| 342 | |
| 343 | def CheckExecuteManyWrongSqlArg(self): |
| 344 | try: |
| 345 | self.cu.executemany(42, [(3,)]) |
| 346 | self.fail("should have raised a ValueError") |
| 347 | except ValueError: |
| 348 | return |
| 349 | except: |
| 350 | self.fail("raised wrong exception.") |
| 351 | |
| 352 | def CheckExecuteManySelect(self): |
| 353 | try: |
| 354 | self.cu.executemany("select ?", [(3,)]) |
| 355 | self.fail("should have raised a ProgrammingError") |
| 356 | except sqlite.ProgrammingError: |
| 357 | return |
| 358 | except: |
| 359 | self.fail("raised wrong exception.") |
| 360 | |
| 361 | def CheckExecuteManyNotIterable(self): |
| 362 | try: |
| 363 | self.cu.executemany("insert into test(income) values (?)", 42) |
| 364 | self.fail("should have raised a TypeError") |
| 365 | except TypeError: |
| 366 | return |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 367 | except Exception as e: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 368 | print("raised", e.__class__) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | self.fail("raised wrong exception.") |
| 370 | |
| 371 | def CheckFetchIter(self): |
| 372 | # Optional DB-API extension. |
| 373 | self.cu.execute("delete from test") |
| 374 | self.cu.execute("insert into test(id) values (?)", (5,)) |
| 375 | self.cu.execute("insert into test(id) values (?)", (6,)) |
| 376 | self.cu.execute("select id from test order by id") |
| 377 | lst = [] |
| 378 | for row in self.cu: |
| 379 | lst.append(row[0]) |
| 380 | self.failUnlessEqual(lst[0], 5) |
| 381 | self.failUnlessEqual(lst[1], 6) |
| 382 | |
| 383 | def CheckFetchone(self): |
| 384 | self.cu.execute("select name from test") |
| 385 | row = self.cu.fetchone() |
| 386 | self.failUnlessEqual(row[0], "foo") |
| 387 | row = self.cu.fetchone() |
| 388 | self.failUnlessEqual(row, None) |
| 389 | |
| 390 | def CheckFetchoneNoStatement(self): |
| 391 | cur = self.cx.cursor() |
| 392 | row = cur.fetchone() |
| 393 | self.failUnlessEqual(row, None) |
| 394 | |
| 395 | def CheckArraySize(self): |
| 396 | # must default ot 1 |
| 397 | self.failUnlessEqual(self.cu.arraysize, 1) |
| 398 | |
| 399 | # now set to 2 |
| 400 | self.cu.arraysize = 2 |
| 401 | |
| 402 | # now make the query return 3 rows |
| 403 | self.cu.execute("delete from test") |
| 404 | self.cu.execute("insert into test(name) values ('A')") |
| 405 | self.cu.execute("insert into test(name) values ('B')") |
| 406 | self.cu.execute("insert into test(name) values ('C')") |
| 407 | self.cu.execute("select name from test") |
| 408 | res = self.cu.fetchmany() |
| 409 | |
| 410 | self.failUnlessEqual(len(res), 2) |
| 411 | |
| 412 | def CheckFetchmany(self): |
| 413 | self.cu.execute("select name from test") |
| 414 | res = self.cu.fetchmany(100) |
| 415 | self.failUnlessEqual(len(res), 1) |
| 416 | res = self.cu.fetchmany(100) |
| 417 | self.failUnlessEqual(res, []) |
| 418 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 419 | def CheckFetchmanyKwArg(self): |
| 420 | """Checks if fetchmany works with keyword arguments""" |
| 421 | self.cu.execute("select name from test") |
| 422 | res = self.cu.fetchmany(size=100) |
| 423 | self.failUnlessEqual(len(res), 1) |
| 424 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | def CheckFetchall(self): |
| 426 | self.cu.execute("select name from test") |
| 427 | res = self.cu.fetchall() |
| 428 | self.failUnlessEqual(len(res), 1) |
| 429 | res = self.cu.fetchall() |
| 430 | self.failUnlessEqual(res, []) |
| 431 | |
| 432 | def CheckSetinputsizes(self): |
| 433 | self.cu.setinputsizes([3, 4, 5]) |
| 434 | |
| 435 | def CheckSetoutputsize(self): |
| 436 | self.cu.setoutputsize(5, 0) |
| 437 | |
| 438 | def CheckSetoutputsizeNoColumn(self): |
| 439 | self.cu.setoutputsize(42) |
| 440 | |
| 441 | def CheckCursorConnection(self): |
| 442 | # Optional DB-API extension. |
| 443 | self.failUnlessEqual(self.cu.connection, self.cx) |
| 444 | |
| 445 | def CheckWrongCursorCallable(self): |
| 446 | try: |
| 447 | def f(): pass |
| 448 | cur = self.cx.cursor(f) |
| 449 | self.fail("should have raised a TypeError") |
| 450 | except TypeError: |
| 451 | return |
| 452 | self.fail("should have raised a ValueError") |
| 453 | |
| 454 | def CheckCursorWrongClass(self): |
| 455 | class Foo: pass |
| 456 | foo = Foo() |
| 457 | try: |
| 458 | cur = sqlite.Cursor(foo) |
| 459 | self.fail("should have raised a ValueError") |
| 460 | except TypeError: |
| 461 | pass |
| 462 | |
| 463 | class ThreadTests(unittest.TestCase): |
| 464 | def setUp(self): |
| 465 | self.con = sqlite.connect(":memory:") |
| 466 | self.cur = self.con.cursor() |
| 467 | self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)") |
| 468 | |
| 469 | def tearDown(self): |
| 470 | self.cur.close() |
| 471 | self.con.close() |
| 472 | |
| 473 | def CheckConCursor(self): |
| 474 | def run(con, errors): |
| 475 | try: |
| 476 | cur = con.cursor() |
| 477 | errors.append("did not raise ProgrammingError") |
| 478 | return |
| 479 | except sqlite.ProgrammingError: |
| 480 | return |
| 481 | except: |
| 482 | errors.append("raised wrong exception") |
| 483 | |
| 484 | errors = [] |
| 485 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 486 | t.start() |
| 487 | t.join() |
| 488 | if len(errors) > 0: |
| 489 | self.fail("\n".join(errors)) |
| 490 | |
| 491 | def CheckConCommit(self): |
| 492 | def run(con, errors): |
| 493 | try: |
| 494 | con.commit() |
| 495 | errors.append("did not raise ProgrammingError") |
| 496 | return |
| 497 | except sqlite.ProgrammingError: |
| 498 | return |
| 499 | except: |
| 500 | errors.append("raised wrong exception") |
| 501 | |
| 502 | errors = [] |
| 503 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 504 | t.start() |
| 505 | t.join() |
| 506 | if len(errors) > 0: |
| 507 | self.fail("\n".join(errors)) |
| 508 | |
| 509 | def CheckConRollback(self): |
| 510 | def run(con, errors): |
| 511 | try: |
| 512 | con.rollback() |
| 513 | errors.append("did not raise ProgrammingError") |
| 514 | return |
| 515 | except sqlite.ProgrammingError: |
| 516 | return |
| 517 | except: |
| 518 | errors.append("raised wrong exception") |
| 519 | |
| 520 | errors = [] |
| 521 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 522 | t.start() |
| 523 | t.join() |
| 524 | if len(errors) > 0: |
| 525 | self.fail("\n".join(errors)) |
| 526 | |
| 527 | def CheckConClose(self): |
| 528 | def run(con, errors): |
| 529 | try: |
| 530 | con.close() |
| 531 | errors.append("did not raise ProgrammingError") |
| 532 | return |
| 533 | except sqlite.ProgrammingError: |
| 534 | return |
| 535 | except: |
| 536 | errors.append("raised wrong exception") |
| 537 | |
| 538 | errors = [] |
| 539 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 540 | t.start() |
| 541 | t.join() |
| 542 | if len(errors) > 0: |
| 543 | self.fail("\n".join(errors)) |
| 544 | |
| 545 | def CheckCurImplicitBegin(self): |
| 546 | def run(cur, errors): |
| 547 | try: |
| 548 | cur.execute("insert into test(name) values ('a')") |
| 549 | errors.append("did not raise ProgrammingError") |
| 550 | return |
| 551 | except sqlite.ProgrammingError: |
| 552 | return |
| 553 | except: |
| 554 | errors.append("raised wrong exception") |
| 555 | |
| 556 | errors = [] |
| 557 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 558 | t.start() |
| 559 | t.join() |
| 560 | if len(errors) > 0: |
| 561 | self.fail("\n".join(errors)) |
| 562 | |
| 563 | def CheckCurClose(self): |
| 564 | def run(cur, errors): |
| 565 | try: |
| 566 | cur.close() |
| 567 | errors.append("did not raise ProgrammingError") |
| 568 | return |
| 569 | except sqlite.ProgrammingError: |
| 570 | return |
| 571 | except: |
| 572 | errors.append("raised wrong exception") |
| 573 | |
| 574 | errors = [] |
| 575 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 576 | t.start() |
| 577 | t.join() |
| 578 | if len(errors) > 0: |
| 579 | self.fail("\n".join(errors)) |
| 580 | |
| 581 | def CheckCurExecute(self): |
| 582 | def run(cur, errors): |
| 583 | try: |
| 584 | cur.execute("select name from test") |
| 585 | errors.append("did not raise ProgrammingError") |
| 586 | return |
| 587 | except sqlite.ProgrammingError: |
| 588 | return |
| 589 | except: |
| 590 | errors.append("raised wrong exception") |
| 591 | |
| 592 | errors = [] |
| 593 | self.cur.execute("insert into test(name) values ('a')") |
| 594 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 595 | t.start() |
| 596 | t.join() |
| 597 | if len(errors) > 0: |
| 598 | self.fail("\n".join(errors)) |
| 599 | |
| 600 | def CheckCurIterNext(self): |
| 601 | def run(cur, errors): |
| 602 | try: |
| 603 | row = cur.fetchone() |
| 604 | errors.append("did not raise ProgrammingError") |
| 605 | return |
| 606 | except sqlite.ProgrammingError: |
| 607 | return |
| 608 | except: |
| 609 | errors.append("raised wrong exception") |
| 610 | |
| 611 | errors = [] |
| 612 | self.cur.execute("insert into test(name) values ('a')") |
| 613 | self.cur.execute("select name from test") |
| 614 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 615 | t.start() |
| 616 | t.join() |
| 617 | if len(errors) > 0: |
| 618 | self.fail("\n".join(errors)) |
| 619 | |
| 620 | class ConstructorTests(unittest.TestCase): |
| 621 | def CheckDate(self): |
| 622 | d = sqlite.Date(2004, 10, 28) |
| 623 | |
| 624 | def CheckTime(self): |
| 625 | t = sqlite.Time(12, 39, 35) |
| 626 | |
| 627 | def CheckTimestamp(self): |
| 628 | ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) |
| 629 | |
| 630 | def CheckDateFromTicks(self): |
| 631 | d = sqlite.DateFromTicks(42) |
| 632 | |
| 633 | def CheckTimeFromTicks(self): |
| 634 | t = sqlite.TimeFromTicks(42) |
| 635 | |
| 636 | def CheckTimestampFromTicks(self): |
| 637 | ts = sqlite.TimestampFromTicks(42) |
| 638 | |
| 639 | def CheckBinary(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 640 | b = sqlite.Binary(b"\0'") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 641 | |
| 642 | class ExtensionTests(unittest.TestCase): |
| 643 | def CheckScriptStringSql(self): |
| 644 | con = sqlite.connect(":memory:") |
| 645 | cur = con.cursor() |
| 646 | cur.executescript(""" |
| 647 | -- bla bla |
| 648 | /* a stupid comment */ |
| 649 | create table a(i); |
| 650 | insert into a(i) values (5); |
| 651 | """) |
| 652 | cur.execute("select i from a") |
| 653 | res = cur.fetchone()[0] |
| 654 | self.failUnlessEqual(res, 5) |
| 655 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 656 | def CheckScriptErrorIncomplete(self): |
| 657 | con = sqlite.connect(":memory:") |
| 658 | cur = con.cursor() |
| 659 | raised = False |
| 660 | try: |
| 661 | cur.executescript("create table test(sadfsadfdsa") |
| 662 | except sqlite.ProgrammingError: |
| 663 | raised = True |
| 664 | self.failUnlessEqual(raised, True, "should have raised an exception") |
| 665 | |
| 666 | def CheckScriptErrorNormal(self): |
| 667 | con = sqlite.connect(":memory:") |
| 668 | cur = con.cursor() |
| 669 | raised = False |
| 670 | try: |
| 671 | cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") |
| 672 | except sqlite.OperationalError: |
| 673 | raised = True |
| 674 | self.failUnlessEqual(raised, True, "should have raised an exception") |
| 675 | |
| 676 | def CheckConnectionExecute(self): |
| 677 | con = sqlite.connect(":memory:") |
| 678 | result = con.execute("select 5").fetchone()[0] |
| 679 | self.failUnlessEqual(result, 5, "Basic test of Connection.execute") |
| 680 | |
| 681 | def CheckConnectionExecutemany(self): |
| 682 | con = sqlite.connect(":memory:") |
| 683 | con.execute("create table test(foo)") |
| 684 | con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) |
| 685 | result = con.execute("select foo from test order by foo").fetchall() |
| 686 | self.failUnlessEqual(result[0][0], 3, "Basic test of Connection.executemany") |
| 687 | self.failUnlessEqual(result[1][0], 4, "Basic test of Connection.executemany") |
| 688 | |
| 689 | def CheckConnectionExecutescript(self): |
| 690 | con = sqlite.connect(":memory:") |
| 691 | con.executescript("create table test(foo); insert into test(foo) values (5);") |
| 692 | result = con.execute("select foo from test").fetchone()[0] |
| 693 | self.failUnlessEqual(result, 5, "Basic test of Connection.executescript") |
| 694 | |
| 695 | class ClosedTests(unittest.TestCase): |
| 696 | def setUp(self): |
| 697 | pass |
| 698 | |
| 699 | def tearDown(self): |
| 700 | pass |
| 701 | |
| 702 | def CheckClosedConCursor(self): |
| 703 | con = sqlite.connect(":memory:") |
| 704 | con.close() |
| 705 | try: |
| 706 | cur = con.cursor() |
| 707 | self.fail("Should have raised a ProgrammingError") |
| 708 | except sqlite.ProgrammingError: |
| 709 | pass |
| 710 | except: |
| 711 | self.fail("Should have raised a ProgrammingError") |
| 712 | |
| 713 | def CheckClosedConCommit(self): |
| 714 | con = sqlite.connect(":memory:") |
| 715 | con.close() |
| 716 | try: |
| 717 | con.commit() |
| 718 | self.fail("Should have raised a ProgrammingError") |
| 719 | except sqlite.ProgrammingError: |
| 720 | pass |
| 721 | except: |
| 722 | self.fail("Should have raised a ProgrammingError") |
| 723 | |
| 724 | def CheckClosedConRollback(self): |
| 725 | con = sqlite.connect(":memory:") |
| 726 | con.close() |
| 727 | try: |
| 728 | con.rollback() |
| 729 | self.fail("Should have raised a ProgrammingError") |
| 730 | except sqlite.ProgrammingError: |
| 731 | pass |
| 732 | except: |
| 733 | self.fail("Should have raised a ProgrammingError") |
| 734 | |
| 735 | def CheckClosedCurExecute(self): |
| 736 | con = sqlite.connect(":memory:") |
| 737 | cur = con.cursor() |
| 738 | con.close() |
| 739 | try: |
| 740 | cur.execute("select 4") |
| 741 | self.fail("Should have raised a ProgrammingError") |
| 742 | except sqlite.ProgrammingError: |
| 743 | pass |
| 744 | except: |
| 745 | self.fail("Should have raised a ProgrammingError") |
| 746 | |
| 747 | def suite(): |
| 748 | module_suite = unittest.makeSuite(ModuleTests, "Check") |
| 749 | connection_suite = unittest.makeSuite(ConnectionTests, "Check") |
| 750 | cursor_suite = unittest.makeSuite(CursorTests, "Check") |
| 751 | thread_suite = unittest.makeSuite(ThreadTests, "Check") |
| 752 | constructor_suite = unittest.makeSuite(ConstructorTests, "Check") |
| 753 | ext_suite = unittest.makeSuite(ExtensionTests, "Check") |
| 754 | closed_suite = unittest.makeSuite(ClosedTests, "Check") |
| 755 | return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_suite)) |
| 756 | |
| 757 | def test(): |
| 758 | runner = unittest.TextTestRunner() |
| 759 | runner.run(suite()) |
| 760 | |
| 761 | if __name__ == "__main__": |
| 762 | test() |