aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/sqlite3/test/dbapi.py
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2021-06-03 18:38:19 +0200
committerGitHub <noreply@github.com>2021-06-03 09:38:19 -0700
commitd88b47b5a396aa8d66f9a0e6b13a0825d59d0eff (patch)
treed932a399ff6b69078f5a0bf5874b4e7e438a6b69 /Lib/sqlite3/test/dbapi.py
parent2c1e2583fdc4db6b43d163239ea42b0e8394171f (diff)
downloadcpython-d88b47b5a396aa8d66f9a0e6b13a0825d59d0eff.tar.gz
cpython-d88b47b5a396aa8d66f9a0e6b13a0825d59d0eff.zip
bpo-42213: Remove redundant cyclic GC hack in sqlite3 (GH-26517)
The sqlite3 module now fully implements the GC protocol, so there's no need for this workaround anymore. - Add and use managed resource helper for connections using TESTFN - Sort test imports - Split open-tests into their own test case Automerge-Triggered-By: GH:vstinner
Diffstat (limited to 'Lib/sqlite3/test/dbapi.py')
-rw-r--r--Lib/sqlite3/test/dbapi.py39
1 files changed, 26 insertions, 13 deletions
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index ab331353394..77fafe09300 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -20,14 +20,26 @@
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
-import threading
-import unittest
+import contextlib
import sqlite3 as sqlite
import sys
+import threading
+import unittest
from test.support.os_helper import TESTFN, unlink
+# Helper for tests using TESTFN
+@contextlib.contextmanager
+def managed_connect(*args, **kwargs):
+ cx = sqlite.connect(*args, **kwargs)
+ try:
+ yield cx
+ finally:
+ cx.close()
+ unlink(TESTFN)
+
+
class ModuleTests(unittest.TestCase):
def test_api_level(self):
self.assertEqual(sqlite.apilevel, "2.0",
@@ -190,26 +202,27 @@ class ConnectionTests(unittest.TestCase):
with self.assertRaises(AttributeError):
self.cx.in_transaction = True
+class OpenTests(unittest.TestCase):
+ _sql = "create table test(id integer)"
+
def test_open_with_path_like_object(self):
""" Checks that we can successfully connect to a database using an object that
is PathLike, i.e. has __fspath__(). """
- self.addCleanup(unlink, TESTFN)
class Path:
def __fspath__(self):
return TESTFN
path = Path()
- with sqlite.connect(path) as cx:
- cx.execute('create table test(id integer)')
+ with managed_connect(path) as cx:
+ cx.execute(self._sql)
def test_open_uri(self):
- self.addCleanup(unlink, TESTFN)
- with sqlite.connect(TESTFN) as cx:
- cx.execute('create table test(id integer)')
- with sqlite.connect('file:' + TESTFN, uri=True) as cx:
- cx.execute('insert into test(id) values(0)')
- with sqlite.connect('file:' + TESTFN + '?mode=ro', uri=True) as cx:
- with self.assertRaises(sqlite.OperationalError):
- cx.execute('insert into test(id) values(1)')
+ with managed_connect(TESTFN) as cx:
+ cx.execute(self._sql)
+ with managed_connect(f"file:{TESTFN}", uri=True) as cx:
+ cx.execute(self._sql)
+ with self.assertRaises(sqlite.OperationalError):
+ with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx:
+ cx.execute(self._sql)
class CursorTests(unittest.TestCase):