1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import pytest
from pathlib import Path
from ymlstash import YmlStash
from dataclasses import dataclass
from typing import ClassVar
TEST_STASH_PATH = "/tmp"
@dataclass
class User:
name: str
age: int
def test_stash_path():
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, TEST_STASH_PATH)
yuval = User(name="yuval", age=42)
stash.save(yuval, "foo")
assert stash.exists("foo")
assert not stash.exists("goo")
assert stash.list_keys() == ["foo"]
obj = stash.load("foo")
assert obj == yuval
goo = User(name="goo", age=10)
stash.save(goo, "goo")
assert stash.list_keys() == ["foo", "goo"]
stash.delete("foo")
assert stash.list_keys() == ["goo"]
with pytest.raises(Exception):
stash.delete("foo")
stash.drop()
assert stash.list_keys() == []
def test_key_field():
@dataclass
class Rat:
key: ClassVar[str] = "name"
with pytest.raises(Exception):
YmlStash(Rat, ".")
@dataclass
class Dog:
name: str
key: ClassVar[str] = "name"
stash = YmlStash(Dog, TEST_STASH_PATH)
terra = Dog(name="terra")
stash.save(terra)
assert stash.list_keys() == ["terra"]
terra = Dog(name="terra")
stash.save(terra, key="dupe") # override key
assert stash.list_keys() == ["terra", "dupe"]
stash.drop()
|