Skip to content

Commit

Permalink
fix: Better exceptions and types
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed May 28, 2024
1 parent 774d544 commit df2a974
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/aerovaldb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
__version__ = metadata.version(__package__)

from .plugins import list_engines, open
from .types import *
from .exceptions import *
6 changes: 6 additions & 0 deletions src/aerovaldb/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class FileDoesNotExist(IOError):
"""
Exception raised by jsondb in FILE_PATH mode, if the resulting file
does not exist.
"""

6 changes: 4 additions & 2 deletions src/aerovaldb/jsonfiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import orjson
import aiofile
from enum import Enum
AccessType = Enum("AccessType", ["JSON_STR", "FILE_PATH", "OBJ"])
from .exceptions import FileDoesNotExist
from .types import AccessType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -116,7 +117,8 @@ async def _get(self, route, route_args, *args, **kwargs):
)

if access_type == AccessType.FILE_PATH:
logger.debug(file_path)
if not os.path.exists(file_path):
raise FileDoesNotExist(f"File {file_path} does not exist.")
return file_path

if access_type == AccessType.JSON_STR:
Expand Down
4 changes: 4 additions & 0 deletions src/aerovaldb/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from enum import Enum


AccessType = Enum("AccessType", ["JSON_STR", "FILE_PATH", "OBJ"])
8 changes: 7 additions & 1 deletion tests/test_jsonfiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
),
]


@pytest.mark.asyncio
@pytest.mark.parametrize("resource", (("json_files:./tests/test-db/json",)))
@pytest.mark.parametrize(*get_parameters)
Expand Down Expand Up @@ -191,3 +190,10 @@ async def test_put_timeseries():
)

assert obj["data"] == read_data["data"]


@pytest.mark.asyncio
async def test_file_does_not_exist():
with aerovaldb.open("json_files:./tests/test-db/json") as db:
with pytest.raises(aerovaldb.FileDoesNotExist):
await db.get_experiments("non-existent-project", access_type = aerovaldb.AccessType.FILE_PATH)

0 comments on commit df2a974

Please sign in to comment.