Skip to content

Commit e5d8dbd

Browse files
Clean up the sqlite3 tests (pythonGH-93056)
Remove helper managed_connect(). Use memory_database() or contextlib.closing() + addCleanup(unlink) instead.
1 parent f9d6c59 commit e5d8dbd

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"
@@ -389,7 +377,7 @@ def test_cursor(self):
389377
def test_failed_open(self):
390378
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
391379
with self.assertRaises(sqlite.OperationalError):
392-
con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
380+
sqlite.connect(YOU_CANNOT_OPEN_THIS)
393381

394382
def test_close(self):
395383
self.cx.close()
@@ -653,7 +641,9 @@ def test_open_with_path_like_object(self):
653641
""" Checks that we can successfully connect to a database using an object that
654642
is PathLike, i.e. has __fspath__(). """
655643
path = FakePath(TESTFN)
656-
with managed_connect(path) as cx:
644+
self.addCleanup(unlink, path)
645+
self.assertFalse(os.path.exists(path))
646+
with contextlib.closing(sqlite.connect(path)) as cx:
657647
self.assertTrue(os.path.exists(path))
658648
cx.execute(self._sql)
659649

@@ -663,23 +653,26 @@ def test_open_with_path_like_object(self):
663653
def test_open_with_undecodable_path(self):
664654
path = TESTFN_UNDECODABLE
665655
self.addCleanup(unlink, path)
666-
with managed_connect(path, in_mem=True) as cx:
656+
self.assertFalse(os.path.exists(path))
657+
with contextlib.closing(sqlite.connect(path)) as cx:
667658
self.assertTrue(os.path.exists(path))
668659
cx.execute(self._sql)
669660

670661
def test_open_uri(self):
671662
path = TESTFN
663+
self.addCleanup(unlink, path)
672664
uri = "file:" + urllib.parse.quote(os.fsencode(path))
673665
self.assertFalse(os.path.exists(path))
674-
with managed_connect(uri, uri=True) as cx:
666+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
675667
self.assertTrue(os.path.exists(path))
676668
cx.execute(self._sql)
677669

678670
def test_open_unquoted_uri(self):
679671
path = TESTFN
672+
self.addCleanup(unlink, path)
680673
uri = "file:" + path
681674
self.assertFalse(os.path.exists(path))
682-
with managed_connect(uri, uri=True) as cx:
675+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
683676
self.assertTrue(os.path.exists(path))
684677
cx.execute(self._sql)
685678

@@ -695,7 +688,7 @@ def test_open_uri_readonly(self):
695688
sqlite.connect(path).close()
696689
self.assertTrue(os.path.exists(path))
697690
# Cannot modify new DB
698-
with managed_connect(uri, uri=True) as cx:
691+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
699692
with self.assertRaises(sqlite.OperationalError):
700693
cx.execute(self._sql)
701694

@@ -704,14 +697,12 @@ def test_open_uri_readonly(self):
704697
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
705698
def test_open_undecodable_uri(self):
706699
path = TESTFN_UNDECODABLE
700+
self.addCleanup(unlink, path)
707701
uri = "file:" + urllib.parse.quote(path)
708702
self.assertFalse(os.path.exists(path))
709-
try:
710-
with managed_connect(uri, uri=True, in_mem=True) as cx:
711-
self.assertTrue(os.path.exists(path))
712-
cx.execute(self._sql)
713-
finally:
714-
unlink(path)
703+
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
704+
self.assertTrue(os.path.exists(path))
705+
cx.execute(self._sql)
715706

716707
def test_factory_database_arg(self):
717708
def factory(database, *args, **kwargs):
@@ -722,12 +713,11 @@ def factory(database, *args, **kwargs):
722713
for database in (TESTFN, os.fsencode(TESTFN),
723714
FakePath(TESTFN), FakePath(os.fsencode(TESTFN))):
724715
database_arg = None
725-
with sqlite.connect(database, factory=factory):
726-
pass
716+
sqlite.connect(database, factory=factory).close()
727717
self.assertEqual(database_arg, database)
728718

729719
def test_database_keyword(self):
730-
with sqlite.connect(database=":memory:") as cx:
720+
with contextlib.closing(sqlite.connect(database=":memory:")) as cx:
731721
self.assertEqual(type(cx), sqlite.Connection)
732722

733723

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)