diff options
| author | Yuval Adam <_@yuv.al> | 2024-05-07 21:19:10 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2024-05-07 21:19:10 +0200 |
| commit | 48f499507187e76f02acb563b46e6b32328b0777 (patch) | |
| tree | 6ef24bae957a49d1367a0d2c4375b7dc6efc81b9 | |
| parent | e5ccbaf85a8865eaaec20ff5c1ef11ff7058d7a2 (diff) | |
Test for invalid paths
| -rw-r--r-- | tests/test_stash.py | 19 | ||||
| -rw-r--r-- | ymlstash/stash.py | 3 |
2 files changed, 16 insertions, 6 deletions
diff --git a/tests/test_stash.py b/tests/test_stash.py index 6b9e47f..88cc466 100644 --- a/tests/test_stash.py +++ b/tests/test_stash.py @@ -5,6 +5,8 @@ from ymlstash import YmlStash from dataclasses import dataclass from typing import ClassVar +TEST_STASH_PATH = "/tmp" + @dataclass class User: @@ -13,14 +15,19 @@ class User: def test_stash_path(): - stash = YmlStash(User, "foo") - assert stash.path == Path("foo") - stash = YmlStash(User, Path("foo")) - assert stash.path == Path("foo") + stash = YmlStash(User, ".") + assert stash.path == Path(".") + stash = YmlStash(User, Path(".")) + assert stash.path == Path(".") + + +def test_invalid_path(): + with pytest.raises(Exception): + YmlStash(User, "/tmp/does/not/exist") def test_stash(): - stash = YmlStash(User, "/tmp/") + stash = YmlStash(User, TEST_STASH_PATH) yuval = User(name="yuval", age=42) stash.save(yuval, "foo") assert stash.list_keys() == ["foo"] @@ -43,7 +50,7 @@ def test_key_field(): name: str key: ClassVar[str] = "name" - stash = YmlStash(Dog, "/tmp/") + stash = YmlStash(Dog, TEST_STASH_PATH) terra = Dog(name="terra") stash.save(terra) assert stash.list_keys() == ["terra"] diff --git a/ymlstash/stash.py b/ymlstash/stash.py index a0c585a..0279fd3 100644 --- a/ymlstash/stash.py +++ b/ymlstash/stash.py @@ -14,6 +14,9 @@ class YmlStash: self._validate() def _validate(self): + if not self.path.exists(): + raise Exception(f"Path {self.path} does not exist and cannot be used") + key_field = getattr(self.clazz, "key", None) field_names = [f.name for f in fields(self.clazz)] if key_field and key_field not in field_names: |
