Skip to content

Improve tests for opening Sqlite by URI #93047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,21 +661,58 @@ def test_open_with_path_like_object(self):
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
def test_open_with_undecodable_path(self):
self.addCleanup(unlink, TESTFN_UNDECODABLE)
path = TESTFN_UNDECODABLE
with managed_connect(path) as cx:
self.addCleanup(unlink, path)
with managed_connect(path, in_mem=True) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)

def test_open_uri(self):
with managed_connect(TESTFN) as cx:
path = TESTFN
uri = "file:" + urllib.parse.quote(os.fsencode(path))
self.assertFalse(os.path.exists(path))
with managed_connect(uri, uri=True) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)
with managed_connect(f"file:{TESTFN}", uri=True) as cx:

def test_open_unquoted_uri(self):
path = TESTFN
uri = "file:" + path
self.assertFalse(os.path.exists(path))
with managed_connect(uri, uri=True) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)

def test_open_uri_readonly(self):
path = TESTFN
self.addCleanup(unlink, path)
uri = "file:" + urllib.parse.quote(os.fsencode(path)) + "?mode=ro"
self.assertFalse(os.path.exists(path))
# Cannot create new DB
with self.assertRaises(sqlite.OperationalError):
with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx:
sqlite.connect(uri, uri=True)
self.assertFalse(os.path.exists(path))
sqlite.connect(path).close()
self.assertTrue(os.path.exists(path))
# Cannot modify new DB
with managed_connect(uri, uri=True) as cx:
with self.assertRaises(sqlite.OperationalError):
cx.execute(self._sql)

@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
def test_open_undecodable_uri(self):
path = TESTFN_UNDECODABLE
uri = "file:" + urllib.parse.quote(path)
self.assertFalse(os.path.exists(path))
try:
with managed_connect(uri, uri=True, in_mem=True) as cx:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the in_mem param is ill named. We can revisit this helper function in another PR.

self.assertTrue(os.path.exists(path))
cx.execute(self._sql)
finally:
unlink(path)

def test_factory_database_arg(self):
def factory(database, *args, **kwargs):
nonlocal database_arg
Expand Down