summaryrefslogtreecommitdiff
path: root/tests/test_stash.py
blob: d6f8ab98fa856dd7e2611b90135032e7a84ac2f3 (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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pytest

from pathlib import Path
from ymlstash import YmlStash
from dataclasses import dataclass
from typing import ClassVar

TEST_STASH_PATH = 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():
    with pytest.raises(Exception):
        YmlStash(User, "/tmp/does/not/exist")
    with pytest.raises(Exception) as e:
        YmlStash(object, ".")
    assert "is not a dataclass" in e.value.args[0]


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] = "foo"

    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()


def test_dump_order():
    @dataclass
    class Order:
        o: int
        r: int
        d: int
        e: int
        a: int
        z: int
        key: ClassVar[str] = "o"

    stash = YmlStash(Order, TEST_STASH_PATH)
    order = Order(1, 2, 3, 4, 5, 6)
    stash.save(order)

    with open(TEST_STASH_PATH / "1.yml", "r") as f:
        lines = f.readlines()
        keys = "".join([line[0] for line in lines])
        assert keys == "ordeaz"

    stash.drop()