Skip to content

Commit 748c83d

Browse files
Clean up the sqlite3 tests (GH-93056)
Remove helper managed_connect(). Use memory_database() or contextlib.closing() + addCleanup(unlink) instead. (cherry picked from commit e5d8dbd) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 09c3dff commit 748c83d

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@
3636
from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE, unlink, temp_dir, FakePath
3737

3838

39-
# Helper for tests using TESTFN
40-
@contextlib.contextmanager
41-
def managed_connect(*args, in_mem=False, **kwargs):
42-
cx = sqlite.connect(*args, **kwargs)
43-
try:
44-
yield cx
45-
finally:
46-
cx.close()
47-
if not in_mem:
48-
unlink(TESTFN)
49-
50-
5139
# Helper for temporary memory databases
5240
def memory_database(*args, **kwargs):
5341
cx = sqlite.connect(":memory:", *args, **kwargs)
@@ -331,7 +319,7 @@ def test_error_code_on_exception(self):
331319
@unittest.skipIf(sqlite.sqlite_version_info <= (3, 7, 16),
332320
"Requires SQLite 3.7.16 or newer")
333321
def test_extended_error_code_on_exception(self):
334-
with managed_connect(":memory:", in_mem=True) as con:
322+
with memory_database() as con:
335323
with con:
336324
con.execute("create table t(t integer check(t > 0))")
337325
errmsg = "constraint failed"
@@ -398,7 +386,7 @@ def test_cursor(self):
398386
def test_failed_open(self):
399387
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
400388
with self.assertRaises(sqlite.OperationalError):
401-
con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
389+
sqlite.connect(YOU_CANNOT_OPEN_THIS)
402390

403391
def test_close(self):
404392
self.cx.close()
@@ -662,7 +650,9 @@ def test_open_with_path_like_object(self):
662650
""" Checks that we can successfully connect to a database using an object that
663651
is PathLike, i.e. has __fspath__(). """
664652
path = FakePath(TESTFN)
665-
with managed_connect(path) as cx:
653+
self.addCleanup(unlink, path)
654+
self.assertFalse(os.path.exists(path))
655+
with contextlib.closing(sqlite.connect(path)) as cx:
666656
self.assertTrue(os.path.exists(path))
667657
cx.execute(self._sql)
668658

@@ -672,23 +662,26 @@ def test_open_with_path_like_object(self):
672662
def test_open_with_undecodable_path(self):
673663
path = TESTFN_UNDECODABLE
674664
self.addCleanup(unlink, path)
675-
with managed_connect(path, in_mem=True) as cx:
665+
self.assertFalse(os.path.exists(path))
666+
with contextlib.closing(sqlite.connect(path)) as cx:
676667
self.assertTrue(os.path.exists(path))
677668
cx.execute(self._sql)
678669

679670
def test_open_uri(self):
680671
path = TESTFN
672+
self.addCleanup(unlink, path)
681673
uri = "file:" + urllib.parse.quote(os.fsencode(path))
682674
self.assertFalse(os.path.exists(path))
683-
with managed_connect(uri, uri=True) as cx:
675+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
684676
self.assertTrue(os.path.exists(path))
685677
cx.execute(self._sql)
686678

687679
def test_open_unquoted_uri(self):
688680
path = TESTFN
681+
self.addCleanup(unlink, path)
689682
uri = "file:" + path
690683
self.assertFalse(os.path.exists(path))
691-
with managed_connect(uri, uri=True) as cx:
684+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
692685
self.assertTrue(os.path.exists(path))
693686
cx.execute(self._sql)
694687

@@ -704,7 +697,7 @@ def test_open_uri_readonly(self):
704697
sqlite.connect(path).close()
705698
self.assertTrue(os.path.exists(path))
706699
# Cannot modify new DB
707-
with managed_connect(uri, uri=True) as cx:
700+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
708701
with self.assertRaises(sqlite.OperationalError):
709702
cx.execute(self._sql)
710703

@@ -713,14 +706,12 @@ def test_open_uri_readonly(self):
713706
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
714707
def test_open_undecodable_uri(self):
715708
path = TESTFN_UNDECODABLE
709+
self.addCleanup(unlink, path)
716710
uri = "file:" + urllib.parse.quote(path)
717711
self.assertFalse(os.path.exists(path))
718-
try:
719-
with managed_connect(uri, uri=True, in_mem=True) as cx:
720-
self.assertTrue(os.path.exists(path))
721-
cx.execute(self._sql)
722-
finally:
723-
unlink(path)
712+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
713+
self.assertTrue(os.path.exists(path))
714+
cx.execute(self._sql)
724715

725716
def test_factory_database_arg(self):
726717
def factory(database, *args, **kwargs):
@@ -731,12 +722,11 @@ def factory(database, *args, **kwargs):
731722
for database in (TESTFN, os.fsencode(TESTFN),
732723
FakePath(TESTFN), FakePath(os.fsencode(TESTFN))):
733724
database_arg = None
734-
with sqlite.connect(database, factory=factory):
735-
pass
725+
sqlite.connect(database, factory=factory).close()
736726
self.assertEqual(database_arg, database)
737727

738728
def test_database_keyword(self):
739-
with sqlite.connect(database=":memory:") as cx:
729+
with contextlib.closing(sqlite.connect(database=":memory:")) as cx:
740730
self.assertEqual(type(cx), sqlite.Connection)
741731

742732

Lib/test/test_sqlite3/test_regression.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from test import support
3030
from unittest.mock import patch
31-
from test.test_sqlite3.test_dbapi import memory_database, managed_connect, cx_limit
31+
from test.test_sqlite3.test_dbapi import memory_database, cx_limit
3232

3333

3434
class RegressionTests(unittest.TestCase):
@@ -422,7 +422,7 @@ def test_return_empty_bytestring(self):
422422
self.assertEqual(val, b'')
423423

424424
def test_table_lock_cursor_replace_stmt(self):
425-
with managed_connect(":memory:", in_mem=True) as con:
425+
with memory_database() as con:
426426
cur = con.cursor()
427427
cur.execute("create table t(t)")
428428
cur.executemany("insert into t values(?)",
@@ -433,7 +433,7 @@ def test_table_lock_cursor_replace_stmt(self):
433433
con.commit()
434434

435435
def test_table_lock_cursor_dealloc(self):
436-
with managed_connect(":memory:", in_mem=True) as con:
436+
with memory_database() as con:
437437
con.execute("create table t(t)")
438438
con.executemany("insert into t values(?)",
439439
((v,) for v in range(5)))
@@ -444,7 +444,7 @@ def test_table_lock_cursor_dealloc(self):
444444
con.commit()
445445

446446
def test_table_lock_cursor_non_readonly_select(self):
447-
with managed_connect(":memory:", in_mem=True) as con:
447+
with memory_database() as con:
448448
con.execute("create table t(t)")
449449
con.executemany("insert into t values(?)",
450450
((v,) for v in range(5)))
@@ -459,7 +459,7 @@ def dup(v):
459459
con.commit()
460460

461461
def test_executescript_step_through_select(self):
462-
with managed_connect(":memory:", in_mem=True) as con:
462+
with memory_database() as con:
463463
values = [(v,) for v in range(5)]
464464
with con:
465465
con.execute("create table t(t)")

0 commit comments

Comments
 (0)