def CheckFailedOpen(self):
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
- try:
+ with self.assertRaises(sqlite.OperationalError):
con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
- except sqlite.OperationalError:
- return
- self.fail("should have raised an OperationalError")
def CheckClose(self):
self.cx.close()
self.cu.execute("delete from test")
def CheckExecuteIllegalSql(self):
- try:
+ with self.assertRaises(sqlite.OperationalError):
self.cu.execute("select asdf")
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError:
- return
- except:
- self.fail("raised wrong exception")
def CheckExecuteTooMuchSql(self):
- try:
+ with self.assertRaises(sqlite.Warning):
self.cu.execute("select 5+4; select 4+5")
- self.fail("should have raised a Warning")
- except sqlite.Warning:
- return
- except:
- self.fail("raised wrong exception")
def CheckExecuteTooMuchSql2(self):
self.cu.execute("select 5+4; -- foo bar")
""")
def CheckExecuteWrongSqlArg(self):
- try:
+ with self.assertRaises(ValueError):
self.cu.execute(42)
- self.fail("should have raised a ValueError")
- except ValueError:
- return
- except:
- self.fail("raised wrong exception.")
def CheckExecuteArgInt(self):
self.cu.execute("insert into test(id) values (?)", (42,))
def CheckExecuteWrongNoOfArgs1(self):
# too many parameters
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckExecuteWrongNoOfArgs2(self):
# too little parameters
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("insert into test(id) values (?)")
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckExecuteWrongNoOfArgs3(self):
# no parameters, parameters are needed
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("insert into test(id) values (?)")
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckExecuteParamList(self):
self.cu.execute("insert into test(name) values ('foo')")
def CheckExecuteDictMappingTooLittleArgs(self):
self.cu.execute("insert into test(name) values ('foo')")
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckExecuteDictMappingNoArgs(self):
self.cu.execute("insert into test(name) values ('foo')")
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("select name from test where name=:name")
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckExecuteDictMappingUnnamed(self):
self.cu.execute("insert into test(name) values ('foo')")
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.execute("select name from test where name=?", {"name": "foo"})
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
def CheckClose(self):
self.cu.close()
self.cu.executemany("insert into test(income) values (?)", mygen())
def CheckExecuteManyWrongSqlArg(self):
- try:
+ with self.assertRaises(ValueError):
self.cu.executemany(42, [(3,)])
- self.fail("should have raised a ValueError")
- except ValueError:
- return
- except:
- self.fail("raised wrong exception.")
def CheckExecuteManySelect(self):
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
self.cu.executemany("select ?", [(3,)])
- self.fail("should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- return
- except:
- self.fail("raised wrong exception.")
def CheckExecuteManyNotIterable(self):
- try:
+ with self.assertRaises(TypeError):
self.cu.executemany("insert into test(income) values (?)", 42)
- self.fail("should have raised a TypeError")
- except TypeError:
- return
- except Exception as e:
- print("raised", e.__class__)
- self.fail("raised wrong exception.")
def CheckFetchIter(self):
# Optional DB-API extension.
self.assertEqual(self.cu.connection, self.cx)
def CheckWrongCursorCallable(self):
- try:
+ with self.assertRaises(TypeError):
def f(): pass
cur = self.cx.cursor(f)
- self.fail("should have raised a TypeError")
- except TypeError:
- return
- self.fail("should have raised a ValueError")
def CheckCursorWrongClass(self):
class Foo: pass
foo = Foo()
- try:
+ with self.assertRaises(TypeError):
cur = sqlite.Cursor(foo)
- self.fail("should have raised a ValueError")
- except TypeError:
- pass
@unittest.skipUnless(threading, 'This test requires threading.')
class ThreadTests(unittest.TestCase):
def CheckScriptSyntaxError(self):
con = sqlite.connect(":memory:")
cur = con.cursor()
- raised = False
- try:
+ with self.assertRaises(sqlite.OperationalError):
cur.executescript("create table test(x); asdf; create table test2(x)")
- except sqlite.OperationalError:
- raised = True
- self.assertEqual(raised, True, "should have raised an exception")
def CheckScriptErrorNormal(self):
con = sqlite.connect(":memory:")
cur = con.cursor()
- raised = False
- try:
+ with self.assertRaises(sqlite.OperationalError):
cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
- except sqlite.OperationalError:
- raised = True
- self.assertEqual(raised, True, "should have raised an exception")
def CheckCursorExecutescriptAsBytes(self):
con = sqlite.connect(":memory:")
def CheckClosedConCursor(self):
con = sqlite.connect(":memory:")
con.close()
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
cur = con.cursor()
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedConCommit(self):
con = sqlite.connect(":memory:")
con.close()
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.commit()
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedConRollback(self):
con = sqlite.connect(":memory:")
con.close()
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.rollback()
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedCurExecute(self):
con = sqlite.connect(":memory:")
cur = con.cursor()
con.close()
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
cur.execute("select 4")
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedCreateFunction(self):
con = sqlite.connect(":memory:")
con.close()
def f(x): return 17
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.create_function("foo", 1, f)
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedCreateAggregate(self):
con = sqlite.connect(":memory:")
pass
def finalize(self):
return 17
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.create_aggregate("foo", 1, Agg)
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedSetAuthorizer(self):
con = sqlite.connect(":memory:")
con.close()
def authorizer(*args):
return sqlite.DENY
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.set_authorizer(authorizer)
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedSetProgressCallback(self):
con = sqlite.connect(":memory:")
con.close()
def progress(): pass
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.set_progress_handler(progress, 100)
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
def CheckClosedCall(self):
con = sqlite.connect(":memory:")
con.close()
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con()
- self.fail("Should have raised a ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError")
class ClosedCurTests(unittest.TestCase):
def setUp(self):
else:
params = []
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
method = getattr(cur, method_name)
-
method(*params)
- self.fail("Should have raised a ProgrammingError: method " + method_name)
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("Should have raised a ProgrammingError: " + method_name)
def suite():
module_suite = unittest.makeSuite(ModuleTests, "Check")
def CheckCreateCollationNotCallable(self):
con = sqlite.connect(":memory:")
- try:
+ with self.assertRaises(TypeError) as cm:
con.create_collation("X", 42)
- self.fail("should have raised a TypeError")
- except TypeError as e:
- self.assertEqual(e.args[0], "parameter must be callable")
+ self.assertEqual(str(cm.exception), 'parameter must be callable')
def CheckCreateCollationNotAscii(self):
con = sqlite.connect(":memory:")
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
con.create_collation("collä", lambda x, y: (x > y) - (x < y))
- self.fail("should have raised a ProgrammingError")
- except sqlite.ProgrammingError as e:
- pass
@unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
'old SQLite versions crash on this test')
self.fail("the expected order was not returned")
con.create_collation("mycoll", None)
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
result = con.execute(sql).fetchall()
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0].lower(), "no such collation sequence: mycoll")
+ self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
def CheckCollationReturnsLargeInteger(self):
def mycoll(x, y):
result = con.execute("""
select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
""").fetchall()
- if result[0][0] != 'b' or result[1][0] != 'a':
- self.fail("wrong collation function is used")
+ self.assertEqual(result[0][0], 'b')
+ self.assertEqual(result[1][0], 'a')
def CheckDeregisterCollation(self):
"""
con = sqlite.connect(":memory:")
con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
con.create_collation("mycoll", None)
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
con.execute("select 'a' as x union select 'b' as x order by x collate mycoll")
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- if not e.args[0].startswith("no such collation sequence"):
- self.fail("wrong OperationalError raised")
+ self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
class ProgressTests(unittest.TestCase):
def CheckProgressHandlerUsed(self):
con = sqlite.connect(":memory:")
cur = Cursor(con)
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
cur.execute("select 4+5").fetchall()
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("should have raised ProgrammingError")
-
def CheckStrSubclass(self):
"""
pass
con = Connection(":memory:")
- try:
+ with self.assertRaises(sqlite.ProgrammingError):
cur = con.cursor()
- self.fail("should have raised ProgrammingError")
- except sqlite.ProgrammingError:
- pass
- except:
- self.fail("should have raised ProgrammingError")
def CheckCursorRegistration(self):
"""
cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)])
cur.execute("select x from foo")
con.rollback()
- try:
+ with self.assertRaises(sqlite.InterfaceError):
cur.fetchall()
- self.fail("should have raised InterfaceError")
- except sqlite.InterfaceError:
- pass
- except:
- self.fail("should have raised InterfaceError")
def CheckAutoCommit(self):
"""
return
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
- try:
+ with self.assertRaises(sqlite.OperationalError):
self.cur2.execute("insert into test(i) values (5)")
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError:
- pass
- except:
- self.fail("should have raised an OperationalError")
def CheckLocking(self):
"""
return
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
- try:
+ with self.assertRaises(sqlite.OperationalError):
self.cur2.execute("insert into test(i) values (5)")
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError:
- pass
- except:
- self.fail("should have raised an OperationalError")
# NO self.con2.rollback() HERE!!!
self.con1.commit()
cur.execute("select 1 union select 2 union select 3")
con.rollback()
- try:
+ with self.assertRaises(sqlite.InterfaceError):
cur.fetchall()
- self.fail("InterfaceError should have been raised")
- except sqlite.InterfaceError as e:
- pass
- except:
- self.fail("InterfaceError should have been raised")
class SpecialCommandTests(unittest.TestCase):
def setUp(self):
def CheckUnsupportedSeq(self):
class Bar: pass
val = Bar()
- try:
+ with self.assertRaises(sqlite.InterfaceError):
self.cur.execute("insert into test(f) values (?)", (val,))
- self.fail("should have raised an InterfaceError")
- except sqlite.InterfaceError:
- pass
- except:
- self.fail("should have raised an InterfaceError")
def CheckUnsupportedDict(self):
class Bar: pass
val = Bar()
- try:
+ with self.assertRaises(sqlite.InterfaceError):
self.cur.execute("insert into test(f) values (:val)", {"val": val})
- self.fail("should have raised an InterfaceError")
- except sqlite.InterfaceError:
- pass
- except:
- self.fail("should have raised an InterfaceError")
def CheckBlob(self):
# default
self.con.close()
def CheckFuncErrorOnCreate(self):
- try:
+ with self.assertRaises(sqlite.OperationalError):
self.con.create_function("bla", -100, lambda x: 2*x)
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError:
- pass
def CheckFuncRefCount(self):
def getfunc():
def CheckFuncException(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select raiseexception()")
cur.fetchone()
- self.fail("should have raised OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0], 'user-defined function raised exception')
+ self.assertEqual(str(cm.exception), 'user-defined function raised exception')
def CheckParamString(self):
cur = self.con.cursor()
pass
def CheckAggrErrorOnCreate(self):
- try:
+ with self.assertRaises(sqlite.OperationalError):
self.con.create_function("bla", -100, AggrSum)
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError:
- pass
def CheckAggrNoStep(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(AttributeError) as cm:
cur.execute("select nostep(t) from test")
- self.fail("should have raised an AttributeError")
- except AttributeError as e:
- self.assertEqual(e.args[0], "'AggrNoStep' object has no attribute 'step'")
+ self.assertEqual(str(cm.exception), "'AggrNoStep' object has no attribute 'step'")
def CheckAggrNoFinalize(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select nofinalize(t) from test")
val = cur.fetchone()[0]
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
+ self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
def CheckAggrExceptionInInit(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excInit(t) from test")
val = cur.fetchone()[0]
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0], "user-defined aggregate's '__init__' method raised error")
+ self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error")
def CheckAggrExceptionInStep(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excStep(t) from test")
val = cur.fetchone()[0]
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error")
+ self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error")
def CheckAggrExceptionInFinalize(self):
cur = self.con.cursor()
- try:
+ with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excFinalize(t) from test")
val = cur.fetchone()[0]
- self.fail("should have raised an OperationalError")
- except sqlite.OperationalError as e:
- self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
+ self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
def CheckAggrCheckParamStr(self):
cur = self.con.cursor()
pass
def test_table_access(self):
- try:
+ with self.assertRaises(sqlite.DatabaseError) as cm:
self.con.execute("select * from t2")
- except sqlite.DatabaseError as e:
- if not e.args[0].endswith("prohibited"):
- self.fail("wrong exception text: %s" % e.args[0])
- return
- self.fail("should have raised an exception due to missing privileges")
+ self.assertIn('prohibited', str(cm.exception))
def test_column_access(self):
- try:
+ with self.assertRaises(sqlite.DatabaseError) as cm:
self.con.execute("select c2 from t1")
- except sqlite.DatabaseError as e:
- if not e.args[0].endswith("prohibited"):
- self.fail("wrong exception text: %s" % e.args[0])
- return
- self.fail("should have raised an exception due to missing privileges")
+ self.assertIn('prohibited', str(cm.exception))
class AuthorizerRaiseExceptionTests(AuthorizerTests):
@staticmethod