summaryrefslogtreecommitdiff
path: root/tests/test_stash.py
blob: e7e573bd35e8e6f390b305329fe380337eb8a381 (plain)
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
from pathlib import Path
from ymlstash import YmlStash
from dataclasses import dataclass
from typing import ClassVar


@dataclass
class User:
    name: str
    age: int


def test_stash_path():
    stash = YmlStash(User, "foo")
    assert stash.path == Path("foo")
    stash = YmlStash(User, Path("foo"))
    assert stash.path == Path("foo")


def test_stash():
    stash = YmlStash(User, "/tmp/")
    yuval = User(name="yuval", age=42)
    stash.save("foo", yuval)
    assert stash.list_all_keys() == ["foo"]
    obj = stash.load("foo")
    assert obj == yuval
    stash.drop()
    assert stash.list_all_keys() == []


@dataclass
class Dog:
    name: str
    key: ClassVar[str] = "name"


def test_auto_key():
    stash = YmlStash(Dog, "/tmp/")
    terra = Dog(name="terra")
    stash.save(None, terra)
    assert stash.list_all_keys() == ["terra"]
    stash.drop()