aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_sqlite3/test_backup.py
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend@python.org>2023-08-17 08:45:48 +0200
committerGitHub <noreply@github.com>2023-08-17 08:45:48 +0200
commit1344cfac43a1920c596b0e8718ca0567889e697b (patch)
tree8cc0f9288d1c9f58ab92bd077462e532ac3bd057 /Lib/test/test_sqlite3/test_backup.py
parentc9d83f93d804b80ee14480466ebee63a6f97dac2 (diff)
downloadcpython-1344cfac43a1920c596b0e8718ca0567889e697b.tar.gz
cpython-1344cfac43a1920c596b0e8718ca0567889e697b.zip
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_backup.py')
-rw-r--r--Lib/test/test_sqlite3/test_backup.py52
1 files changed, 27 insertions, 25 deletions
diff --git a/Lib/test/test_sqlite3/test_backup.py b/Lib/test/test_sqlite3/test_backup.py
index 87ab29c54d6..4584d976bce 100644
--- a/Lib/test/test_sqlite3/test_backup.py
+++ b/Lib/test/test_sqlite3/test_backup.py
@@ -1,6 +1,8 @@
import sqlite3 as sqlite
import unittest
+from .util import memory_database
+
class BackupTests(unittest.TestCase):
def setUp(self):
@@ -32,32 +34,32 @@ class BackupTests(unittest.TestCase):
self.cx.backup(self.cx)
def test_bad_target_closed_connection(self):
- bck = sqlite.connect(':memory:')
- bck.close()
- with self.assertRaises(sqlite.ProgrammingError):
- self.cx.backup(bck)
+ with memory_database() as bck:
+ bck.close()
+ with self.assertRaises(sqlite.ProgrammingError):
+ self.cx.backup(bck)
def test_bad_source_closed_connection(self):
- bck = sqlite.connect(':memory:')
- source = sqlite.connect(":memory:")
- source.close()
- with self.assertRaises(sqlite.ProgrammingError):
- source.backup(bck)
+ with memory_database() as bck:
+ source = sqlite.connect(":memory:")
+ source.close()
+ with self.assertRaises(sqlite.ProgrammingError):
+ source.backup(bck)
def test_bad_target_in_transaction(self):
- bck = sqlite.connect(':memory:')
- bck.execute('CREATE TABLE bar (key INTEGER)')
- bck.executemany('INSERT INTO bar (key) VALUES (?)', [(3,), (4,)])
- with self.assertRaises(sqlite.OperationalError) as cm:
- self.cx.backup(bck)
+ with memory_database() as bck:
+ bck.execute('CREATE TABLE bar (key INTEGER)')
+ bck.executemany('INSERT INTO bar (key) VALUES (?)', [(3,), (4,)])
+ with self.assertRaises(sqlite.OperationalError) as cm:
+ self.cx.backup(bck)
def test_keyword_only_args(self):
with self.assertRaises(TypeError):
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, 1)
def test_simple(self):
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck)
self.verify_backup(bck)
@@ -67,7 +69,7 @@ class BackupTests(unittest.TestCase):
def progress(status, remaining, total):
journal.append(status)
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, pages=1, progress=progress)
self.verify_backup(bck)
@@ -81,7 +83,7 @@ class BackupTests(unittest.TestCase):
def progress(status, remaining, total):
journal.append(remaining)
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, progress=progress)
self.verify_backup(bck)
@@ -94,7 +96,7 @@ class BackupTests(unittest.TestCase):
def progress(status, remaining, total):
journal.append(remaining)
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, pages=-1, progress=progress)
self.verify_backup(bck)
@@ -103,7 +105,7 @@ class BackupTests(unittest.TestCase):
def test_non_callable_progress(self):
with self.assertRaises(TypeError) as cm:
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, pages=1, progress='bar')
self.assertEqual(str(cm.exception), 'progress argument must be a callable')
@@ -116,7 +118,7 @@ class BackupTests(unittest.TestCase):
self.cx.commit()
journal.append(remaining)
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, pages=1, progress=progress)
self.verify_backup(bck)
@@ -140,12 +142,12 @@ class BackupTests(unittest.TestCase):
self.assertEqual(str(err.exception), 'nearly out of space')
def test_database_source_name(self):
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, name='main')
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, name='temp')
with self.assertRaises(sqlite.OperationalError) as cm:
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, name='non-existing')
self.assertIn("unknown database", str(cm.exception))
@@ -153,7 +155,7 @@ class BackupTests(unittest.TestCase):
self.cx.execute('CREATE TABLE attached_db.foo (key INTEGER)')
self.cx.executemany('INSERT INTO attached_db.foo (key) VALUES (?)', [(3,), (4,)])
self.cx.commit()
- with sqlite.connect(':memory:') as bck:
+ with memory_database() as bck:
self.cx.backup(bck, name='attached_db')
self.verify_backup(bck)