Skip to content

Commit 76b6ed1

Browse files
Improve tests for opening Sqlite by URI (GH-93047)
* Test with with escaped non-ascii characters * Test read-only open of existing DB. (cherry picked from commit 4e2b664) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent dd923c5 commit 76b6ed1

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,21 +670,58 @@ def test_open_with_path_like_object(self):
670670
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
671671
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
672672
def test_open_with_undecodable_path(self):
673-
self.addCleanup(unlink, TESTFN_UNDECODABLE)
674673
path = TESTFN_UNDECODABLE
675-
with managed_connect(path) as cx:
674+
self.addCleanup(unlink, path)
675+
with managed_connect(path, in_mem=True) as cx:
676676
self.assertTrue(os.path.exists(path))
677677
cx.execute(self._sql)
678678

679679
def test_open_uri(self):
680-
with managed_connect(TESTFN) as cx:
680+
path = TESTFN
681+
uri = "file:" + urllib.parse.quote(os.fsencode(path))
682+
self.assertFalse(os.path.exists(path))
683+
with managed_connect(uri, uri=True) as cx:
684+
self.assertTrue(os.path.exists(path))
681685
cx.execute(self._sql)
682-
with managed_connect(f"file:{TESTFN}", uri=True) as cx:
686+
687+
def test_open_unquoted_uri(self):
688+
path = TESTFN
689+
uri = "file:" + path
690+
self.assertFalse(os.path.exists(path))
691+
with managed_connect(uri, uri=True) as cx:
692+
self.assertTrue(os.path.exists(path))
683693
cx.execute(self._sql)
694+
695+
def test_open_uri_readonly(self):
696+
path = TESTFN
697+
self.addCleanup(unlink, path)
698+
uri = "file:" + urllib.parse.quote(os.fsencode(path)) + "?mode=ro"
699+
self.assertFalse(os.path.exists(path))
700+
# Cannot create new DB
684701
with self.assertRaises(sqlite.OperationalError):
685-
with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx:
702+
sqlite.connect(uri, uri=True)
703+
self.assertFalse(os.path.exists(path))
704+
sqlite.connect(path).close()
705+
self.assertTrue(os.path.exists(path))
706+
# Cannot modify new DB
707+
with managed_connect(uri, uri=True) as cx:
708+
with self.assertRaises(sqlite.OperationalError):
686709
cx.execute(self._sql)
687710

711+
@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
712+
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
713+
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
714+
def test_open_undecodable_uri(self):
715+
path = TESTFN_UNDECODABLE
716+
uri = "file:" + urllib.parse.quote(path)
717+
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)
724+
688725
def test_factory_database_arg(self):
689726
def factory(database, *args, **kwargs):
690727
nonlocal database_arg

0 commit comments

Comments
 (0)