blob: def55f0aa629cb95ffa9a76624cbfbd1e4455c52 [file] [log] [blame]
koder aka kdanilov7f59d562016-12-26 01:34:23 +02001import yaml
koder aka kdanilov39e449e2016-12-17 15:15:26 +02002import array
koder aka kdanilov70227062016-11-26 23:23:21 +02003import shutil
4import tempfile
5import contextlib
6
7
koder aka kdanilov39e449e2016-12-17 15:15:26 +02008import pytest
9from oktest import ok
koder aka kdanilov70227062016-11-26 23:23:21 +020010
11
12from wally.storage import make_storage
13
14
15@contextlib.contextmanager
16def in_temp_dir():
17 dname = tempfile.mkdtemp()
18 try:
19 yield dname
20 finally:
21 shutil.rmtree(dname)
22
23
24def test_basic():
25 with in_temp_dir() as root:
26 values = {
27 "int": 1,
28 "str/1": "test",
29 "bytes/2": b"test",
30 "none/s/1": None,
31 "bool/xx/1/2/1": None,
32 "float/s/1": 1.234,
33 "list": [1, 2, "3"],
34 "dict": {1: 3, "2": "4", "1.2": 1.3}
35 }
36
37 with make_storage(root, existing=False) as storage:
38 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020039 storage.put(val, path)
koder aka kdanilov70227062016-11-26 23:23:21 +020040
koder aka kdanilov39e449e2016-12-17 15:15:26 +020041 with make_storage(root, existing=True) as storage:
42 for path, val in values.items():
koder aka kdanilov39e449e2016-12-17 15:15:26 +020043 ok(storage.get(path)) == val
44
45
46def test_path_list():
47 values = {
48 "int": 1,
49 "str/1": "test",
50 "bytes/2": b"test",
51 "none/s/1": None,
52 "bool/xx/1/2/1": None,
53 "float/s/1": 1.234,
54 "list": [1, 2, "3"],
55 "dict": {1: 3, "2": "4", "1.2": 1.3}
56 }
57
58 with in_temp_dir() as root:
59 with make_storage(root, existing=False) as storage:
60 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020061 storage.put(val, *path.split('/'))
koder aka kdanilov70227062016-11-26 23:23:21 +020062
63 with make_storage(root, existing=True) as storage:
64 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020065 ok(storage.get(*path.split('/'))) == val
66 ok(storage.get(path)) == val
koder aka kdanilov39e449e2016-12-17 15:15:26 +020067
68 with in_temp_dir() as root:
69 with make_storage(root, existing=False) as storage:
70 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020071 storage.put(val, path)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020072
73 with make_storage(root, existing=True) as storage:
74 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020075 ok(storage.get(path)) == val
76 ok(storage.get(*path.split('/'))) == val
koder aka kdanilov70227062016-11-26 23:23:21 +020077
78
koder aka kdanilov39e449e2016-12-17 15:15:26 +020079def test_overwrite():
80 with in_temp_dir() as root:
81 with make_storage(root, existing=False) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +020082 storage.put("1", "some_path")
83 storage.put([1, 2, 3], "some_path")
koder aka kdanilov39e449e2016-12-17 15:15:26 +020084
85 with make_storage(root, existing=True) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +020086 assert storage.get("some_path") == [1, 2, 3]
koder aka kdanilov70227062016-11-26 23:23:21 +020087
88
koder aka kdanilov39e449e2016-12-17 15:15:26 +020089def test_multy_level():
90 with in_temp_dir() as root:
91 values = {
92 "dict1": {1: {3: 4, 6: [12, {123, 3}, {4: 3}]}, "2": "4", "1.2": 1.3}
93 }
94
95 with make_storage(root, existing=False) as storage:
96 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +020097 storage.put(val, path)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020098
99 with make_storage(root, existing=True) as storage:
100 for path, val in values.items():
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200101 ok(storage.get(path)) == val
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200102
103
104def test_arrays():
105 with in_temp_dir() as root:
106 val_l = list(range(10000)) * 10
107 val_i = array.array("i", val_l)
108 val_f = array.array("f", map(float, val_l))
109 val_2f = val_f + val_f
110
111 with make_storage(root, existing=False) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200112 storage.put_array(val_i, "array_i")
113 storage.put_array(val_f, "array_f")
114 storage.put_array(val_f, "array_x2")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200115 storage.append(val_f, "array_x2")
116
117 with make_storage(root, existing=True) as storage:
118 ok(val_i) == storage.get_array("i", "array_i")
119 ok(val_f) == storage.get_array("f", "array_f")
120 ok(val_2f) == storage.get_array("f", "array_x2")
121
122
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200123class LoadMe(yaml.YAMLObject):
124 yaml_tag = '!LoadMe'
125
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200126 def __init__(self, **vals):
127 self.__dict__.update(vals)
128
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200129 @classmethod
130 def to_yaml(cls, dumper, data):
131 return dumper.represent_mapping(cls.yaml_tag, data.__dict__)
132
133 @classmethod
134 def from_yaml(cls, loader, node):
135 return LoadMe(**loader.construct_mapping(node))
136
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200137
138def test_load_user_obj():
139 obj = LoadMe(x=1, y=12, z=[1,2,3], t="asdad", gg={"a": 1, "g": [["x"]]})
140
141 with in_temp_dir() as root:
142 with make_storage(root, existing=False) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200143 storage.put(obj, "obj")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200144
145 with make_storage(root, existing=True) as storage:
146 obj2 = storage.load(LoadMe, "obj")
147 assert isinstance(obj2, LoadMe)
148 ok(obj2.__dict__) == obj.__dict__
149
150
151def test_path_not_exists():
152 with in_temp_dir() as root:
153 pass
154
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200155 with make_storage(root, existing=False) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200156 with pytest.raises(KeyError):
157 storage.get("x")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200158
159
160def test_substorage():
161 with in_temp_dir() as root:
162 with make_storage(root, existing=False) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200163 storage.put("data", "x/y")
164 storage.sub_storage("t").put("sub_data", "r")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200165
166 with make_storage(root, existing=True) as storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200167 ok(storage.get("t/r")) == "sub_data"
168 ok(storage.sub_storage("x").get("y")) == "data"