Skip to content

Commit 4e2b664

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.
1 parent 5b71b51 commit 4e2b664

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
@@ -661,21 +661,58 @@ def test_open_with_path_like_object(self):
661661
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
662662
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
663663
def test_open_with_undecodable_path(self):
664-
self.addCleanup(unlink, TESTFN_UNDECODABLE)
665664
path = TESTFN_UNDECODABLE
666-
with managed_connect(path) as cx:
665+
self.addCleanup(unlink, path)
666+
with managed_connect(path, in_mem=True) as cx:
667667
self.assertTrue(os.path.exists(path))
668668
cx.execute(self._sql)
669669

670670
def test_open_uri(self):
671-
with managed_connect(TESTFN) as cx:
671+
path = TESTFN
672+
uri = "file:" + urllib.parse.quote(os.fsencode(path))
673+
self.assertFalse(os.path.exists(path))
674+
with managed_connect(uri, uri=True) as cx:
675+
self.assertTrue(os.path.exists(path))
672676
cx.execute(self._sql)
673-
with managed_connect(f"file:{TESTFN}", uri=True) as cx:
677+
678+
def test_open_unquoted_uri(self):
679+
path = TESTFN
680+
uri = "file:" + path
681+
self.assertFalse(os.path.exists(path))
682+
with managed_connect(uri, uri=True) as cx:
683+
self.assertTrue(os.path.exists(path))
674684
cx.execute(self._sql)
685+
686+
def test_open_uri_readonly(self):
687+
path = TESTFN
688+
self.addCleanup(unlink, path)
689+
uri = "file:" + urllib.parse.quote(os.fsencode(path)) + "?mode=ro"
690+
self.assertFalse(os.path.exists(path))
691+
# Cannot create new DB
675692
with self.assertRaises(sqlite.OperationalError):
676-
with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx:
693+
sqlite.connect(uri, uri=True)
694+
self.assertFalse(os.path.exists(path))
695+
sqlite.connect(path).close()
696+
self.assertTrue(os.path.exists(path))
697+
# Cannot modify new DB
698+
with managed_connect(uri, uri=True) as cx:
699+
with self.assertRaises(sqlite.OperationalError):
677700
cx.execute(self._sql)
678701

702+
@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
703+
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
704+
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
705+
def test_open_undecodable_uri(self):
706+
path = TESTFN_UNDECODABLE
707+
uri = "file:" + urllib.parse.quote(path)
708+
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)
715+
679716
def test_factory_database_arg(self):
680717
def factory(database, *args, **kwargs):
681718
nonlocal database_arg

0 commit comments

Comments
 (0)