blob: 8fd8f4dad3000191174addf239b57465bcc8d465 [file] [log] [blame]
koder aka kdanilov39e449e2016-12-17 15:15:26 +02001import array
koder aka kdanilov70227062016-11-26 23:23:21 +02002import shutil
3import tempfile
4import contextlib
5
6
koder aka kdanilov39e449e2016-12-17 15:15:26 +02007import pytest
8from oktest import ok
koder aka kdanilov70227062016-11-26 23:23:21 +02009
10
11from wally.storage import make_storage
12
13
14@contextlib.contextmanager
15def in_temp_dir():
16 dname = tempfile.mkdtemp()
17 try:
18 yield dname
19 finally:
20 shutil.rmtree(dname)
21
22
23def test_basic():
24 with in_temp_dir() as root:
25 values = {
26 "int": 1,
27 "str/1": "test",
28 "bytes/2": b"test",
29 "none/s/1": None,
30 "bool/xx/1/2/1": None,
31 "float/s/1": 1.234,
32 "list": [1, 2, "3"],
33 "dict": {1: 3, "2": "4", "1.2": 1.3}
34 }
35
36 with make_storage(root, existing=False) as storage:
37 for path, val in values.items():
38 storage[path] = val
39
koder aka kdanilov39e449e2016-12-17 15:15:26 +020040 with make_storage(root, existing=True) as storage:
41 for path, val in values.items():
42 ok(storage[path]) == val
43 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():
61 storage[path.split('/')] = val
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 kdanilov39e449e2016-12-17 15:15:26 +020065 ok(storage[path.split('/')]) == val
66 ok(storage.get(path.split('/'))) == val
67
68 with in_temp_dir() as root:
69 with make_storage(root, existing=False) as storage:
70 for path, val in values.items():
71 storage[path] = val
72
73 with make_storage(root, existing=True) as storage:
74 for path, val in values.items():
75 ok(storage[path.split('/')]) == 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_list():
80 with in_temp_dir() as root:
81 with make_storage(root, existing=False) as storage:
82 storage["x/some_path1"] = "1"
83 storage["x/some_path2"] = [1, 2, 3]
84 storage["x/some_path3"] = [1, 2, 3, 4]
85
86 storage["x/y/some_path11"] = "1"
87 storage["x/y/some_path22"] = [1, 2, 3]
88
89 with make_storage(root, existing=True) as storage:
90 assert 'x' in storage
91 assert 'x/y' in storage
92
93 assert {(False, 'x')} == set(storage.list())
94
95 assert {(True, 'some_path1'),
96 (True, 'some_path2'),
97 (True, 'some_path3'),
98 (False, "y")} == set(storage.list("x"))
99
100 assert {(True, 'some_path11'), (True, 'some_path22')} == set(storage.list("x/y"))
koder aka kdanilov70227062016-11-26 23:23:21 +0200101
102
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200103def test_overwrite():
104 with in_temp_dir() as root:
105 with make_storage(root, existing=False) as storage:
106 storage["some_path"] = "1"
107 storage["some_path"] = [1, 2, 3]
108
109 with make_storage(root, existing=True) as storage:
110 assert storage["some_path"] == [1, 2, 3]
koder aka kdanilov70227062016-11-26 23:23:21 +0200111
112
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200113def test_multy_level():
114 with in_temp_dir() as root:
115 values = {
116 "dict1": {1: {3: 4, 6: [12, {123, 3}, {4: 3}]}, "2": "4", "1.2": 1.3}
117 }
118
119 with make_storage(root, existing=False) as storage:
120 for path, val in values.items():
121 storage[path] = val
122
123 with make_storage(root, existing=True) as storage:
124 for path, val in values.items():
125 ok(storage[path]) == val
126
127
128def test_arrays():
129 with in_temp_dir() as root:
130 val_l = list(range(10000)) * 10
131 val_i = array.array("i", val_l)
132 val_f = array.array("f", map(float, val_l))
133 val_2f = val_f + val_f
134
135 with make_storage(root, existing=False) as storage:
136 storage.set_array(val_i, "array_i")
137 storage.set_array(val_f, "array_f")
138 storage.set_array(val_f, "array_x2")
139 storage.append(val_f, "array_x2")
140
141 with make_storage(root, existing=True) as storage:
142 ok(val_i) == storage.get_array("i", "array_i")
143 ok(val_f) == storage.get_array("f", "array_f")
144 ok(val_2f) == storage.get_array("f", "array_x2")
145
146
147class LoadMe:
148 def __init__(self, **vals):
149 self.__dict__.update(vals)
150
151
152def test_load_user_obj():
153 obj = LoadMe(x=1, y=12, z=[1,2,3], t="asdad", gg={"a": 1, "g": [["x"]]})
154
155 with in_temp_dir() as root:
156 with make_storage(root, existing=False) as storage:
157 storage["obj"] = obj
158
159 with make_storage(root, existing=True) as storage:
160 obj2 = storage.load(LoadMe, "obj")
161 assert isinstance(obj2, LoadMe)
162 ok(obj2.__dict__) == obj.__dict__
163
164
165def test_path_not_exists():
166 with in_temp_dir() as root:
167 pass
168
169 with pytest.raises(IOError):
170 with make_storage(root, existing=True) as storage:
171 pass
172
173 with in_temp_dir() as root:
174 pass
175
176 with make_storage(root, existing=False) as storage:
177 with pytest.raises(IOError):
178 storage["x"]
179
180
181def test_incorrect_user_object():
182 obj = LoadMe(x=1, y=LoadMe(t=12))
183
184 with in_temp_dir() as root:
185 with make_storage(root, existing=False) as storage:
186 with pytest.raises(ValueError):
187 storage["obj"] = obj
188
189
190def test_substorage():
191 with in_temp_dir() as root:
192 with make_storage(root, existing=False) as storage:
193 storage["x/y"] = "data"
194 storage.sub_storage("t")["r"] = "sub_data"
195
196 with make_storage(root, existing=True) as storage:
197 ok(storage["t/r"]) == "sub_data"
198 ok(storage.sub_storage("x")["y"]) == "data"