From a859c871c6049b21bae321fd79f1706c198afe4e Mon Sep 17 00:00:00 2001 From: thorbjoernl Date: Tue, 28 May 2024 13:18:41 +0200 Subject: [PATCH] fix: Better exceptions and types --- src/aerovaldb/__init__.py | 2 ++ src/aerovaldb/exceptions.py | 6 ++++++ src/aerovaldb/jsonfiledb.py | 6 ++++-- src/aerovaldb/types.py | 4 ++++ tests/test_jsonfiledb.py | 8 +++++++- 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/aerovaldb/exceptions.py create mode 100644 src/aerovaldb/types.py diff --git a/src/aerovaldb/__init__.py b/src/aerovaldb/__init__.py index c503de7..cc74636 100644 --- a/src/aerovaldb/__init__.py +++ b/src/aerovaldb/__init__.py @@ -3,3 +3,5 @@ __version__ = metadata.version(__package__) from .plugins import list_engines, open +from .types import * +from .exceptions import * \ No newline at end of file diff --git a/src/aerovaldb/exceptions.py b/src/aerovaldb/exceptions.py new file mode 100644 index 0000000..a324111 --- /dev/null +++ b/src/aerovaldb/exceptions.py @@ -0,0 +1,6 @@ +class FileDoesNotExist(IOError): + """ + Exception raised by jsondb in FILE_PATH mode, if the resulting file + does not exist. + """ + \ No newline at end of file diff --git a/src/aerovaldb/jsonfiledb.py b/src/aerovaldb/jsonfiledb.py index 418e40c..d6f52ae 100644 --- a/src/aerovaldb/jsonfiledb.py +++ b/src/aerovaldb/jsonfiledb.py @@ -7,8 +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__) @@ -112,6 +112,8 @@ async def _get(self, route, route_args, *args, **kwargs): ) if access_type == AccessType.FILE_PATH: + if not os.path.exists(file_path): + raise FileDoesNotExist(f"File {file_path} does not exist.") return str(file_path) if access_type == AccessType.JSON_STR: diff --git a/src/aerovaldb/types.py b/src/aerovaldb/types.py new file mode 100644 index 0000000..919a82b --- /dev/null +++ b/src/aerovaldb/types.py @@ -0,0 +1,4 @@ +from enum import Enum + + +AccessType = Enum("AccessType", ["JSON_STR", "FILE_PATH", "OBJ"]) diff --git a/tests/test_jsonfiledb.py b/tests/test_jsonfiledb.py index 8640d4a..6556f6f 100644 --- a/tests/test_jsonfiledb.py +++ b/tests/test_jsonfiledb.py @@ -128,7 +128,6 @@ ), ] - @pytest.mark.asyncio @pytest.mark.parametrize("resource", (("json_files:./tests/test-db/json",))) @pytest.mark.parametrize(*get_parameters) @@ -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)