Commit 1003b34c authored by Berker Peksag's avatar Berker Peksag

Modernize sqlite3 tests

Update current tests that use old pattern with assertRaises
to make them more maintainable.
parent 0e1d6802
This diff is collapsed.
...@@ -33,19 +33,14 @@ class CollationTests(unittest.TestCase): ...@@ -33,19 +33,14 @@ class CollationTests(unittest.TestCase):
def CheckCreateCollationNotCallable(self): def CheckCreateCollationNotCallable(self):
con = sqlite.connect(":memory:") con = sqlite.connect(":memory:")
try: with self.assertRaises(TypeError) as cm:
con.create_collation("X", 42) con.create_collation("X", 42)
self.fail("should have raised a TypeError") self.assertEqual(str(cm.exception), 'parameter must be callable')
except TypeError as e:
self.assertEqual(e.args[0], "parameter must be callable")
def CheckCreateCollationNotAscii(self): def CheckCreateCollationNotAscii(self):
con = sqlite.connect(":memory:") con = sqlite.connect(":memory:")
try: with self.assertRaises(sqlite.ProgrammingError):
con.create_collation("collä", lambda x, y: (x > y) - (x < y)) 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), @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
'old SQLite versions crash on this test') 'old SQLite versions crash on this test')
...@@ -70,11 +65,9 @@ class CollationTests(unittest.TestCase): ...@@ -70,11 +65,9 @@ class CollationTests(unittest.TestCase):
self.fail("the expected order was not returned") self.fail("the expected order was not returned")
con.create_collation("mycoll", None) con.create_collation("mycoll", None)
try: with self.assertRaises(sqlite.OperationalError) as cm:
result = con.execute(sql).fetchall() result = con.execute(sql).fetchall()
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
except sqlite.OperationalError as e:
self.assertEqual(e.args[0].lower(), "no such collation sequence: mycoll")
def CheckCollationReturnsLargeInteger(self): def CheckCollationReturnsLargeInteger(self):
def mycoll(x, y): def mycoll(x, y):
...@@ -106,8 +99,8 @@ class CollationTests(unittest.TestCase): ...@@ -106,8 +99,8 @@ class CollationTests(unittest.TestCase):
result = con.execute(""" result = con.execute("""
select x from (select 'a' as x union select 'b' as x) order by x collate mycoll select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
""").fetchall() """).fetchall()
if result[0][0] != 'b' or result[1][0] != 'a': self.assertEqual(result[0][0], 'b')
self.fail("wrong collation function is used") self.assertEqual(result[1][0], 'a')
def CheckDeregisterCollation(self): def CheckDeregisterCollation(self):
""" """
...@@ -117,12 +110,9 @@ class CollationTests(unittest.TestCase): ...@@ -117,12 +110,9 @@ class CollationTests(unittest.TestCase):
con = sqlite.connect(":memory:") con = sqlite.connect(":memory:")
con.create_collation("mycoll", lambda x, y: (x > y) - (x < y)) con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
con.create_collation("mycoll", None) 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") con.execute("select 'a' as x union select 'b' as x order by x collate mycoll")
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
except sqlite.OperationalError as e:
if not e.args[0].startswith("no such collation sequence"):
self.fail("wrong OperationalError raised")
class ProgressTests(unittest.TestCase): class ProgressTests(unittest.TestCase):
def CheckProgressHandlerUsed(self): def CheckProgressHandlerUsed(self):
......
...@@ -170,14 +170,8 @@ class RegressionTests(unittest.TestCase): ...@@ -170,14 +170,8 @@ class RegressionTests(unittest.TestCase):
con = sqlite.connect(":memory:") con = sqlite.connect(":memory:")
cur = Cursor(con) cur = Cursor(con)
try: with self.assertRaises(sqlite.ProgrammingError):
cur.execute("select 4+5").fetchall() 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): def CheckStrSubclass(self):
""" """
...@@ -196,13 +190,8 @@ class RegressionTests(unittest.TestCase): ...@@ -196,13 +190,8 @@ class RegressionTests(unittest.TestCase):
pass pass
con = Connection(":memory:") con = Connection(":memory:")
try: with self.assertRaises(sqlite.ProgrammingError):
cur = con.cursor() cur = con.cursor()
self.fail("should have raised ProgrammingError")
except sqlite.ProgrammingError:
pass
except:
self.fail("should have raised ProgrammingError")
def CheckCursorRegistration(self): def CheckCursorRegistration(self):
""" """
...@@ -223,13 +212,8 @@ class RegressionTests(unittest.TestCase): ...@@ -223,13 +212,8 @@ class RegressionTests(unittest.TestCase):
cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)]) cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)])
cur.execute("select x from foo") cur.execute("select x from foo")
con.rollback() con.rollback()
try: with self.assertRaises(sqlite.InterfaceError):
cur.fetchall() cur.fetchall()
self.fail("should have raised InterfaceError")
except sqlite.InterfaceError:
pass
except:
self.fail("should have raised InterfaceError")
def CheckAutoCommit(self): def CheckAutoCommit(self):
""" """
......
...@@ -118,13 +118,8 @@ class TransactionTests(unittest.TestCase): ...@@ -118,13 +118,8 @@ class TransactionTests(unittest.TestCase):
return return
self.cur1.execute("create table test(i)") self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)") 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.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): def CheckLocking(self):
""" """
...@@ -137,13 +132,8 @@ class TransactionTests(unittest.TestCase): ...@@ -137,13 +132,8 @@ class TransactionTests(unittest.TestCase):
return return
self.cur1.execute("create table test(i)") self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)") 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.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!!! # NO self.con2.rollback() HERE!!!
self.con1.commit() self.con1.commit()
...@@ -159,13 +149,8 @@ class TransactionTests(unittest.TestCase): ...@@ -159,13 +149,8 @@ class TransactionTests(unittest.TestCase):
cur.execute("select 1 union select 2 union select 3") cur.execute("select 1 union select 2 union select 3")
con.rollback() con.rollback()
try: with self.assertRaises(sqlite.InterfaceError):
cur.fetchall() 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): class SpecialCommandTests(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -185,24 +185,14 @@ class DeclTypesTests(unittest.TestCase): ...@@ -185,24 +185,14 @@ class DeclTypesTests(unittest.TestCase):
def CheckUnsupportedSeq(self): def CheckUnsupportedSeq(self):
class Bar: pass class Bar: pass
val = Bar() val = Bar()
try: with self.assertRaises(sqlite.InterfaceError):
self.cur.execute("insert into test(f) values (?)", (val,)) 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): def CheckUnsupportedDict(self):
class Bar: pass class Bar: pass
val = Bar() val = Bar()
try: with self.assertRaises(sqlite.InterfaceError):
self.cur.execute("insert into test(f) values (:val)", {"val": val}) 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): def CheckBlob(self):
# default # default
......
...@@ -162,11 +162,8 @@ class FunctionTests(unittest.TestCase): ...@@ -162,11 +162,8 @@ class FunctionTests(unittest.TestCase):
self.con.close() self.con.close()
def CheckFuncErrorOnCreate(self): def CheckFuncErrorOnCreate(self):
try: with self.assertRaises(sqlite.OperationalError):
self.con.create_function("bla", -100, lambda x: 2*x) 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 CheckFuncRefCount(self):
def getfunc(): def getfunc():
...@@ -231,12 +228,10 @@ class FunctionTests(unittest.TestCase): ...@@ -231,12 +228,10 @@ class FunctionTests(unittest.TestCase):
def CheckFuncException(self): def CheckFuncException(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select raiseexception()") cur.execute("select raiseexception()")
cur.fetchone() cur.fetchone()
self.fail("should have raised OperationalError") self.assertEqual(str(cm.exception), 'user-defined function raised exception')
except sqlite.OperationalError as e:
self.assertEqual(e.args[0], 'user-defined function raised exception')
def CheckParamString(self): def CheckParamString(self):
cur = self.con.cursor() cur = self.con.cursor()
...@@ -312,55 +307,42 @@ class AggregateTests(unittest.TestCase): ...@@ -312,55 +307,42 @@ class AggregateTests(unittest.TestCase):
pass pass
def CheckAggrErrorOnCreate(self): def CheckAggrErrorOnCreate(self):
try: with self.assertRaises(sqlite.OperationalError):
self.con.create_function("bla", -100, AggrSum) self.con.create_function("bla", -100, AggrSum)
self.fail("should have raised an OperationalError")
except sqlite.OperationalError:
pass
def CheckAggrNoStep(self): def CheckAggrNoStep(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(AttributeError) as cm:
cur.execute("select nostep(t) from test") cur.execute("select nostep(t) from test")
self.fail("should have raised an AttributeError") self.assertEqual(str(cm.exception), "'AggrNoStep' object has no attribute 'step'")
except AttributeError as e:
self.assertEqual(e.args[0], "'AggrNoStep' object has no attribute 'step'")
def CheckAggrNoFinalize(self): def CheckAggrNoFinalize(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select nofinalize(t) from test") cur.execute("select nofinalize(t) from test")
val = cur.fetchone()[0] val = cur.fetchone()[0]
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
except sqlite.OperationalError as e:
self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
def CheckAggrExceptionInInit(self): def CheckAggrExceptionInInit(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excInit(t) from test") cur.execute("select excInit(t) from test")
val = cur.fetchone()[0] val = cur.fetchone()[0]
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error")
except sqlite.OperationalError as e:
self.assertEqual(e.args[0], "user-defined aggregate's '__init__' method raised error")
def CheckAggrExceptionInStep(self): def CheckAggrExceptionInStep(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excStep(t) from test") cur.execute("select excStep(t) from test")
val = cur.fetchone()[0] val = cur.fetchone()[0]
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error")
except sqlite.OperationalError as e:
self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error")
def CheckAggrExceptionInFinalize(self): def CheckAggrExceptionInFinalize(self):
cur = self.con.cursor() cur = self.con.cursor()
try: with self.assertRaises(sqlite.OperationalError) as cm:
cur.execute("select excFinalize(t) from test") cur.execute("select excFinalize(t) from test")
val = cur.fetchone()[0] val = cur.fetchone()[0]
self.fail("should have raised an OperationalError") self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
except sqlite.OperationalError as e:
self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
def CheckAggrCheckParamStr(self): def CheckAggrCheckParamStr(self):
cur = self.con.cursor() cur = self.con.cursor()
...@@ -433,22 +415,14 @@ class AuthorizerTests(unittest.TestCase): ...@@ -433,22 +415,14 @@ class AuthorizerTests(unittest.TestCase):
pass pass
def test_table_access(self): def test_table_access(self):
try: with self.assertRaises(sqlite.DatabaseError) as cm:
self.con.execute("select * from t2") self.con.execute("select * from t2")
except sqlite.DatabaseError as e: self.assertIn('prohibited', str(cm.exception))
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")
def test_column_access(self): def test_column_access(self):
try: with self.assertRaises(sqlite.DatabaseError) as cm:
self.con.execute("select c2 from t1") self.con.execute("select c2 from t1")
except sqlite.DatabaseError as e: self.assertIn('prohibited', str(cm.exception))
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")
class AuthorizerRaiseExceptionTests(AuthorizerTests): class AuthorizerRaiseExceptionTests(AuthorizerTests):
@staticmethod @staticmethod
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment