summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sqlite3/test_transactions.py
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend@python.org>2023-08-17 06:45:48 (GMT)
committerGitHub <noreply@github.com>2023-08-17 06:45:48 (GMT)
commit1344cfac43a1920c596b0e8718ca0567889e697b (patch)
tree8cc0f9288d1c9f58ab92bd077462e532ac3bd057 /Lib/test/test_sqlite3/test_transactions.py
parentc9d83f93d804b80ee14480466ebee63a6f97dac2 (diff)
downloadcpython-1344cfac43a1920c596b0e8718ca0567889e697b.zip
cpython-1344cfac43a1920c596b0e8718ca0567889e697b.tar.gz
cpython-1344cfac43a1920c596b0e8718ca0567889e697b.tar.bz2
gh-105539: Explict resource management for connection objects in sqlite3 tests (#108017)
- Use memory_database() helper - Move test utility functions to util.py - Add convenience memory database mixin - Add check() helper for closed connection tests
Diffstat (limited to 'Lib/test/test_sqlite3/test_transactions.py')
-rw-r--r--Lib/test/test_sqlite3/test_transactions.py33
1 files changed, 11 insertions, 22 deletions
diff --git a/Lib/test/test_sqlite3/test_transactions.py b/Lib/test/test_sqlite3/test_transactions.py
index 5d211dd..b7b231d 100644
--- a/Lib/test/test_sqlite3/test_transactions.py
+++ b/Lib/test/test_sqlite3/test_transactions.py
@@ -28,7 +28,8 @@ from test.support import LOOPBACK_TIMEOUT
from test.support.os_helper import TESTFN, unlink
from test.support.script_helper import assert_python_ok
-from test.test_sqlite3.test_dbapi import memory_database
+from .util import memory_database
+from .util import MemoryDatabaseMixin
TIMEOUT = LOOPBACK_TIMEOUT / 10
@@ -132,14 +133,14 @@ class TransactionTests(unittest.TestCase):
def test_rollback_cursor_consistency(self):
"""Check that cursors behave correctly after rollback."""
- con = sqlite.connect(":memory:")
- cur = con.cursor()
- cur.execute("create table test(x)")
- cur.execute("insert into test(x) values (5)")
- cur.execute("select 1 union select 2 union select 3")
+ with memory_database() as con:
+ cur = con.cursor()
+ cur.execute("create table test(x)")
+ cur.execute("insert into test(x) values (5)")
+ cur.execute("select 1 union select 2 union select 3")
- con.rollback()
- self.assertEqual(cur.fetchall(), [(1,), (2,), (3,)])
+ con.rollback()
+ self.assertEqual(cur.fetchall(), [(1,), (2,), (3,)])
def test_multiple_cursors_and_iternext(self):
# gh-94028: statements are cleared and reset in cursor iternext.
@@ -218,10 +219,7 @@ class RollbackTests(unittest.TestCase):
-class SpecialCommandTests(unittest.TestCase):
- def setUp(self):
- self.con = sqlite.connect(":memory:")
- self.cur = self.con.cursor()
+class SpecialCommandTests(MemoryDatabaseMixin, unittest.TestCase):
def test_drop_table(self):
self.cur.execute("create table test(i)")
@@ -233,14 +231,8 @@ class SpecialCommandTests(unittest.TestCase):
self.cur.execute("insert into test(i) values (5)")
self.cur.execute("pragma count_changes=1")
- def tearDown(self):
- self.cur.close()
- self.con.close()
-
-class TransactionalDDL(unittest.TestCase):
- def setUp(self):
- self.con = sqlite.connect(":memory:")
+class TransactionalDDL(MemoryDatabaseMixin, unittest.TestCase):
def test_ddl_does_not_autostart_transaction(self):
# For backwards compatibility reasons, DDL statements should not
@@ -268,9 +260,6 @@ class TransactionalDDL(unittest.TestCase):
with self.assertRaises(sqlite.OperationalError):
self.con.execute("select * from test")
- def tearDown(self):
- self.con.close()
-
class IsolationLevelFromInit(unittest.TestCase):
CREATE = "create table t(t)"