koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 1 | import array |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 2 | import shutil |
| 3 | import tempfile |
| 4 | import contextlib |
| 5 | |
| 6 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 7 | import pytest |
| 8 | from oktest import ok |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 9 | |
| 10 | |
| 11 | from wally.storage import make_storage |
| 12 | |
| 13 | |
| 14 | @contextlib.contextmanager |
| 15 | def in_temp_dir(): |
| 16 | dname = tempfile.mkdtemp() |
| 17 | try: |
| 18 | yield dname |
| 19 | finally: |
| 20 | shutil.rmtree(dname) |
| 21 | |
| 22 | |
| 23 | def 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 kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 40 | 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 | |
| 46 | def 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 62 | |
| 63 | with make_storage(root, existing=True) as storage: |
| 64 | for path, val in values.items(): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 65 | 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 77 | |
| 78 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 79 | def 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 101 | |
| 102 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 103 | def 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 111 | |
| 112 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 113 | def 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 | |
| 128 | def 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 | |
| 147 | class LoadMe: |
| 148 | def __init__(self, **vals): |
| 149 | self.__dict__.update(vals) |
| 150 | |
| 151 | |
| 152 | def 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 | |
| 165 | def 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 | |
| 181 | def 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 | |
| 190 | def 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" |