koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 1 | import yaml |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 2 | import array |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 3 | import shutil |
| 4 | import tempfile |
| 5 | import contextlib |
| 6 | |
| 7 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 8 | import pytest |
| 9 | from oktest import ok |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 10 | |
| 11 | |
| 12 | from wally.storage import make_storage |
| 13 | |
| 14 | |
| 15 | @contextlib.contextmanager |
| 16 | def in_temp_dir(): |
| 17 | dname = tempfile.mkdtemp() |
| 18 | try: |
| 19 | yield dname |
| 20 | finally: |
| 21 | shutil.rmtree(dname) |
| 22 | |
| 23 | |
| 24 | def 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 39 | storage.put(val, path) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 40 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 41 | with make_storage(root, existing=True) as storage: |
| 42 | for path, val in values.items(): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 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(): |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 61 | storage.put(val, *path.split('/')) |
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 | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 65 | ok(storage.get(*path.split('/'))) == val |
| 66 | ok(storage.get(path)) == val |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 67 | |
| 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 71 | storage.put(val, path) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 72 | |
| 73 | with make_storage(root, existing=True) as storage: |
| 74 | for path, val in values.items(): |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 75 | ok(storage.get(path)) == 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_overwrite(): |
| 80 | with in_temp_dir() as root: |
| 81 | with make_storage(root, existing=False) as storage: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 82 | storage.put("1", "some_path") |
| 83 | storage.put([1, 2, 3], "some_path") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 84 | |
| 85 | with make_storage(root, existing=True) as storage: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 86 | assert storage.get("some_path") == [1, 2, 3] |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 87 | |
| 88 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 89 | def 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 97 | storage.put(val, path) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 98 | |
| 99 | with make_storage(root, existing=True) as storage: |
| 100 | for path, val in values.items(): |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 101 | ok(storage.get(path)) == val |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 112 | 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 kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 115 | 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 123 | class LoadMe(yaml.YAMLObject): |
| 124 | yaml_tag = '!LoadMe' |
| 125 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 126 | def __init__(self, **vals): |
| 127 | self.__dict__.update(vals) |
| 128 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 129 | @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 kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 137 | |
| 138 | def 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 kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 143 | storage.put(obj, "obj") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 144 | |
| 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 | |
| 151 | def test_path_not_exists(): |
| 152 | with in_temp_dir() as root: |
| 153 | pass |
| 154 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 155 | with make_storage(root, existing=False) as storage: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 156 | with pytest.raises(KeyError): |
| 157 | storage.get("x") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 158 | |
| 159 | |
| 160 | def test_substorage(): |
| 161 | with in_temp_dir() as root: |
| 162 | with make_storage(root, existing=False) as storage: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 163 | storage.put("data", "x/y") |
| 164 | storage.sub_storage("t").put("sub_data", "r") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 165 | |
| 166 | with make_storage(root, existing=True) as storage: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame^] | 167 | ok(storage.get("t/r")) == "sub_data" |
| 168 | ok(storage.sub_storage("x").get("y")) == "data" |