| 1 |
import contextlib |
| 2 |
import functools |
| 3 |
import io |
| 4 |
import re |
| 5 |
import sqlite3 |
| 6 |
import test.support |
| 7 |
import unittest |
| 8 |
|
| 9 |
|
| 10 |
# Helper for temporary memory databases |
| 11 |
def memory_database(*args, **kwargs): |
| 12 |
cx = sqlite3.connect(":memory:", *args, **kwargs) |
| 13 |
return contextlib.closing(cx) |
| 14 |
|
| 15 |
|
| 16 |
# Temporarily limit a database connection parameter |
| 17 |
@contextlib.contextmanager |
| 18 |
def cx_limit(cx, category=sqlite3.SQLITE_LIMIT_SQL_LENGTH, limit=128): |
| 19 |
try: |
| 20 |
_prev = cx.setlimit(category, limit) |
| 21 |
yield limit |
| 22 |
finally: |
| 23 |
cx.setlimit(category, _prev) |
| 24 |
|
| 25 |
|
| 26 |
def with_tracebacks(exc, regex="", name="", msg_regex=""): |
| 27 |
"""Convenience decorator for testing callback tracebacks.""" |
| 28 |
def decorator(func): |
| 29 |
exc_regex = re.compile(regex) if regex else None |
| 30 |
_msg_regex = re.compile(msg_regex) if msg_regex else None |
| 31 |
@functools.wraps(func) |
| 32 |
def wrapper(self, *args, **kwargs): |
| 33 |
with test.support.catch_unraisable_exception() as cm: |
| 34 |
# First, run the test with traceback enabled. |
| 35 |
with check_tracebacks(self, cm, exc, exc_regex, _msg_regex, name): |
| 36 |
func(self, *args, **kwargs) |
| 37 |
|
| 38 |
# Then run the test with traceback disabled. |
| 39 |
func(self, *args, **kwargs) |
| 40 |
return wrapper |
| 41 |
return decorator |
| 42 |
|
| 43 |
|
| 44 |
@contextlib.contextmanager |
| 45 |
def check_tracebacks(self, cm, exc, exc_regex, msg_regex, obj_name): |
| 46 |
"""Convenience context manager for testing callback tracebacks.""" |
| 47 |
sqlite3.enable_callback_tracebacks(True) |
| 48 |
try: |
| 49 |
buf = io.StringIO() |
| 50 |
with contextlib.redirect_stderr(buf): |
| 51 |
yield |
| 52 |
|
| 53 |
self.assertEqual(cm.unraisable.exc_type, exc) |
| 54 |
if exc_regex: |
| 55 |
msg = str(cm.unraisable.exc_value) |
| 56 |
self.assertIsNotNone(exc_regex.search(msg), (exc_regex, msg)) |
| 57 |
if msg_regex: |
| 58 |
msg = cm.unraisable.err_msg |
| 59 |
self.assertIsNotNone(msg_regex.search(msg), (msg_regex, msg)) |
| 60 |
if obj_name: |
| 61 |
self.assertEqual(cm.unraisable.object.__name__, obj_name) |
| 62 |
finally: |
| 63 |
sqlite3.enable_callback_tracebacks(False) |
| 64 |
|
| 65 |
|
| 66 |
class MemoryDatabaseMixin: |
| 67 |
|
| 68 |
def setUp(self): |
| 69 |
self.con = sqlite3.connect(":memory:") |
| 70 |
self.cur = self.con.cursor() |
| 71 |
|
| 72 |
def tearDown(self): |
| 73 |
self.cur.close() |
| 74 |
self.con.close() |
| 75 |
|
| 76 |
@property |
| 77 |
def cx(self): |
| 78 |
return self.con |
| 79 |
|
| 80 |
@property |
| 81 |
def cu(self): |
| 82 |
return self.cur |
| 83 |
|
| 84 |
|
| 85 |
def requires_virtual_table(module): |
| 86 |
with memory_database() as cx: |
| 87 |
supported = (module,) in list(cx.execute("PRAGMA module_list")) |
| 88 |
reason = f"Requires {module!r} virtual table support" |
| 89 |
return unittest.skipUnless(supported, reason) |