Skip to content

Commit

Permalink
Add __fspath__ method to SimpleSQLite class: #86
Browse files Browse the repository at this point in the history
Signed-off-by: Tsuyoshi Hombashi <[email protected]>
  • Loading branch information
thombashi committed Feb 23, 2025
1 parent 5439b5d commit e6f4156
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions simplesqlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ def __enter__(self): # type: ignore
def __exit__(self, exc_type, exc_value, traceback) -> None: # type: ignore
self.close()

def __fspath__(self) -> str:
if not self.database_path:
raise ValueError("database is not connected")

if self.database_path == MEMORY_DB_NAME:
raise ValueError("database is in-memory")

return self.database_path

def is_connected(self) -> bool:
"""
:return: |True| if the connection to a database is valid.
Expand Down
7 changes: 7 additions & 0 deletions test/test_simplesqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ def test_exception_invalid_file(self, tmpdir, mode, expected):
SimpleSQLite(str(p), mode).connection


class Test_SimpleSQLite_fpath:
def test_normal_path(self, tmpdir):
p = tmpdir.join("test.sqlite3")
db_path = str(p)
assert os.fspath(SimpleSQLite(db_path, "w"))


class Test_SimpleSQLite_is_connected:
def test_normal(self, con):
assert con.is_connected()
Expand Down

0 comments on commit e6f4156

Please sign in to comment.