diff options
author | Erlend Egeberg Aasland <erlend.aasland@innova.no> | 2021-06-24 13:56:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-24 12:56:56 +0100 |
commit | 2c1ae09764446beda5248759fb99c859e14f1b25 (patch) | |
tree | afd2b070b5aab841d9667251bccaf0294e19d210 /Lib/sqlite3/test/userfunctions.py | |
parent | 9049ea51eca081984c8ae37dfeb68b75d624e90d (diff) | |
download | cpython-2c1ae09764446beda5248759fb99c859e14f1b25.tar.gz cpython-2c1ae09764446beda5248759fb99c859e14f1b25.zip |
bpo-43553: Improve `sqlite3` test coverage (GH-26886)
Diffstat (limited to 'Lib/sqlite3/test/userfunctions.py')
-rw-r--r-- | Lib/sqlite3/test/userfunctions.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/sqlite3/test/userfunctions.py b/Lib/sqlite3/test/userfunctions.py index 42908907249..dc900f6486f 100644 --- a/Lib/sqlite3/test/userfunctions.py +++ b/Lib/sqlite3/test/userfunctions.py @@ -21,11 +21,36 @@ # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. +import contextlib +import functools +import io import unittest import unittest.mock import gc import sqlite3 as sqlite +def with_tracebacks(strings): + """Convenience decorator for testing callback tracebacks.""" + strings.append('Traceback') + + def decorator(func): + @functools.wraps(func) + def wrapper(self, *args, **kwargs): + # First, run the test with traceback enabled. + sqlite.enable_callback_tracebacks(True) + buf = io.StringIO() + with contextlib.redirect_stderr(buf): + func(self, *args, **kwargs) + tb = buf.getvalue() + for s in strings: + self.assertIn(s, tb) + + # Then run the test with traceback disabled. + sqlite.enable_callback_tracebacks(False) + func(self, *args, **kwargs) + return wrapper + return decorator + def func_returntext(): return "foo" def func_returnunicode(): @@ -228,6 +253,7 @@ class FunctionTests(unittest.TestCase): val = cur.fetchone()[0] self.assertEqual(val, 1<<31) + @with_tracebacks(['func_raiseexception', '5/0', 'ZeroDivisionError']) def test_func_exception(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: @@ -387,6 +413,7 @@ class AggregateTests(unittest.TestCase): val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error") + @with_tracebacks(['__init__', '5/0', 'ZeroDivisionError']) def test_aggr_exception_in_init(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: @@ -394,6 +421,7 @@ class AggregateTests(unittest.TestCase): val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error") + @with_tracebacks(['step', '5/0', 'ZeroDivisionError']) def test_aggr_exception_in_step(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: @@ -401,6 +429,7 @@ class AggregateTests(unittest.TestCase): val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error") + @with_tracebacks(['finalize', '5/0', 'ZeroDivisionError']) def test_aggr_exception_in_finalize(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: @@ -502,6 +531,14 @@ class AuthorizerRaiseExceptionTests(AuthorizerTests): raise ValueError return sqlite.SQLITE_OK + @with_tracebacks(['authorizer_cb', 'ValueError']) + def test_table_access(self): + super().test_table_access() + + @with_tracebacks(['authorizer_cb', 'ValueError']) + def test_column_access(self): + super().test_table_access() + class AuthorizerIllegalTypeTests(AuthorizerTests): @staticmethod def authorizer_cb(action, arg1, arg2, dbname, source): |