From df7fc3fc8fdd633c31d3256b32f738cfbb7b3cb0 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 16 Mar 2019 22:51:39 +0100 Subject: [PATCH] leadership support --- test/data/unicode1.dict1 | 1 + test/data/unicode1.mod1 | 1 + test/data/unicode1.updates1 | 1 + test/data/unicode1_leadership_value.dict1 | 1 + test/data/unicode1_leadership_value.dict2 | 1 + test/data/unicode1_leadership_value.dict3 | 1 + test/data/unicode1_leadership_value.dict4 | 1 + test/data/unicode1_leadership_value.dict5 | 1 + test/data/unicode1_leadership_value.dict6 | 1 + test/data/unicode1_leadership_value.mod1 | 1 + test/data/unicode1_leadership_value.mod2 | 1 + test/data/unicode1_leadership_value.mod3 | 1 + test/data/unicode1_leadership_value.mod4 | 1 + test/data/unicode1_leadership_value.mod5 | 1 + test/data/unicode1_leadership_value.mod6 | 1 + test/data/unicode1_leadership_value.updates1 | 1 + test/data/unicode1_leadership_value.updates2 | 1 + test/data/unicode1_leadership_value.updates3 | 1 + test/data/unicode1_leadership_value.updates4 | 1 + test/data/unicode1_leadership_value.updates5 | 1 + test/data/unicode1_leadership_value.updates6 | 1 + test/data/unicode1_mod_value.dict1 | 1 + test/data/unicode1_mod_value.dict2 | 1 + test/data/unicode1_mod_value.mod1 | 1 + test/data/unicode1_mod_value.mod2 | 1 + test/data/unicode1_mod_value.updates1 | 1 + test/data/unicode1_mod_value.updates2 | 1 + test/data/unicode1_multi.dict1 | 1 + test/data/unicode1_multi.mod1 | 1 + test/data/unicode1_multi.updates1 | 1 + test/data/unicode1_multi_mod_value.dict1 | 1 + test/data/unicode1_multi_mod_value.dict2 | 1 + test/data/unicode1_multi_mod_value.mod1 | 1 + test/data/unicode1_multi_mod_value.mod2 | 1 + test/data/unicode1_multi_mod_value.updates1 | 1 + test/data/unicode1_multi_mod_value.updates2 | 1 + test/test_json.py | 102 ++++++++++------ tiramisu_json_api/api.py | 120 +++++++++++++------ 38 files changed, 186 insertions(+), 72 deletions(-) create mode 100644 test/data/unicode1.dict1 create mode 100644 test/data/unicode1.mod1 create mode 100644 test/data/unicode1.updates1 create mode 100644 test/data/unicode1_leadership_value.dict1 create mode 100644 test/data/unicode1_leadership_value.dict2 create mode 100644 test/data/unicode1_leadership_value.dict3 create mode 100644 test/data/unicode1_leadership_value.dict4 create mode 100644 test/data/unicode1_leadership_value.dict5 create mode 100644 test/data/unicode1_leadership_value.dict6 create mode 100644 test/data/unicode1_leadership_value.mod1 create mode 100644 test/data/unicode1_leadership_value.mod2 create mode 100644 test/data/unicode1_leadership_value.mod3 create mode 100644 test/data/unicode1_leadership_value.mod4 create mode 100644 test/data/unicode1_leadership_value.mod5 create mode 100644 test/data/unicode1_leadership_value.mod6 create mode 100644 test/data/unicode1_leadership_value.updates1 create mode 100644 test/data/unicode1_leadership_value.updates2 create mode 100644 test/data/unicode1_leadership_value.updates3 create mode 100644 test/data/unicode1_leadership_value.updates4 create mode 100644 test/data/unicode1_leadership_value.updates5 create mode 100644 test/data/unicode1_leadership_value.updates6 create mode 100644 test/data/unicode1_mod_value.dict1 create mode 100644 test/data/unicode1_mod_value.dict2 create mode 100644 test/data/unicode1_mod_value.mod1 create mode 100644 test/data/unicode1_mod_value.mod2 create mode 100644 test/data/unicode1_mod_value.updates1 create mode 100644 test/data/unicode1_mod_value.updates2 create mode 100644 test/data/unicode1_multi.dict1 create mode 100644 test/data/unicode1_multi.mod1 create mode 100644 test/data/unicode1_multi.updates1 create mode 100644 test/data/unicode1_multi_mod_value.dict1 create mode 100644 test/data/unicode1_multi_mod_value.dict2 create mode 100644 test/data/unicode1_multi_mod_value.mod1 create mode 100644 test/data/unicode1_multi_mod_value.mod2 create mode 100644 test/data/unicode1_multi_mod_value.updates1 create mode 100644 test/data/unicode1_multi_mod_value.updates2 diff --git a/test/data/unicode1.dict1 b/test/data/unicode1.dict1 new file mode 100644 index 0000000..3ffec07 --- /dev/null +++ b/test/data/unicode1.dict1 @@ -0,0 +1 @@ +{"options.unicode": "val"} \ No newline at end of file diff --git a/test/data/unicode1.mod1 b/test/data/unicode1.mod1 new file mode 100644 index 0000000..2d9850a --- /dev/null +++ b/test/data/unicode1.mod1 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode').value.set('val')", "body": {"updates": [{"action": "modify", "name": "options.unicode", "value": "val"}]}} \ No newline at end of file diff --git a/test/data/unicode1.updates1 b/test/data/unicode1.updates1 new file mode 100644 index 0000000..19c59da --- /dev/null +++ b/test/data/unicode1.updates1 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": [{"key": "options.unicode", "owner": "user", "value": "val"}]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict1 b/test/data/unicode1_leadership_value.dict1 new file mode 100644 index 0000000..795e813 --- /dev/null +++ b/test/data/unicode1_leadership_value.dict1 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val1", "val2"], "options.unicode.unicode1": [null, null], "options.unicode.unicode2": ["follower2", "follower2"], "options.unicode.unicode3": [null, null]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict2 b/test/data/unicode1_leadership_value.dict2 new file mode 100644 index 0000000..9286dfd --- /dev/null +++ b/test/data/unicode1_leadership_value.dict2 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val3"], "options.unicode.unicode1": ["super1"], "options.unicode.unicode2": ["pas test"], "options.unicode.unicode3": [null]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict3 b/test/data/unicode1_leadership_value.dict3 new file mode 100644 index 0000000..5fa74c6 --- /dev/null +++ b/test/data/unicode1_leadership_value.dict3 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val3", "val4", "val5"], "options.unicode.unicode1": ["super1", "super2", null], "options.unicode.unicode2": ["pas test", "test", "follower2"], "options.unicode.unicode3": [null, "super", null]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict4 b/test/data/unicode1_leadership_value.dict4 new file mode 100644 index 0000000..5fa74c6 --- /dev/null +++ b/test/data/unicode1_leadership_value.dict4 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val3", "val4", "val5"], "options.unicode.unicode1": ["super1", "super2", null], "options.unicode.unicode2": ["pas test", "test", "follower2"], "options.unicode.unicode3": [null, "super", null]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict5 b/test/data/unicode1_leadership_value.dict5 new file mode 100644 index 0000000..1ed93d9 --- /dev/null +++ b/test/data/unicode1_leadership_value.dict5 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val3", "val4"], "options.unicode.unicode1": ["super1", "super2"], "options.unicode.unicode2": ["pas test", "follower2"], "options.unicode.unicode3": [null, "super"]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.dict6 b/test/data/unicode1_leadership_value.dict6 new file mode 100644 index 0000000..51586f6 --- /dev/null +++ b/test/data/unicode1_leadership_value.dict6 @@ -0,0 +1 @@ +{"options.unicode.unicode": ["val3", "val4"], "options.unicode.unicode1": ["super1", "super2"], "options.unicode.unicode2": ["pas test", "test2"], "options.unicode.unicode3": [null, "super"]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod1 b/test/data/unicode1_leadership_value.mod1 new file mode 100644 index 0000000..6ef0031 --- /dev/null +++ b/test/data/unicode1_leadership_value.mod1 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode').value.reset()", "body": {"updates": [{"action": "delete", "name": "options.unicode.unicode"}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod2 b/test/data/unicode1_leadership_value.mod2 new file mode 100644 index 0000000..fd30783 --- /dev/null +++ b/test/data/unicode1_leadership_value.mod2 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode', 1).value.reset()", "body": {"updates": [{"action": "delete", "index": 1, "name": "options.unicode.unicode"}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod3 b/test/data/unicode1_leadership_value.mod3 new file mode 100644 index 0000000..aea13dd --- /dev/null +++ b/test/data/unicode1_leadership_value.mod3 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode').value.set(['val3', 'val4', 'val5'])", "body": {"updates": [{"action": "modify", "name": "options.unicode.unicode", "value": ["val3", "val4", "val5"]}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod4 b/test/data/unicode1_leadership_value.mod4 new file mode 100644 index 0000000..aea13dd --- /dev/null +++ b/test/data/unicode1_leadership_value.mod4 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode').value.set(['val3', 'val4', 'val5'])", "body": {"updates": [{"action": "modify", "name": "options.unicode.unicode", "value": ["val3", "val4", "val5"]}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod5 b/test/data/unicode1_leadership_value.mod5 new file mode 100644 index 0000000..787624d --- /dev/null +++ b/test/data/unicode1_leadership_value.mod5 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode2', 1).value.reset()", "body": {"updates": [{"action": "delete", "name": "options.unicode.unicode2", "index": 1}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.mod6 b/test/data/unicode1_leadership_value.mod6 new file mode 100644 index 0000000..c5ccfc2 --- /dev/null +++ b/test/data/unicode1_leadership_value.mod6 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode.unicode2', 1).value.set('test2')", "body": {"updates": [{"action": "modify", "name": "options.unicode.unicode2", "index": 1, "value": "test2"}]}} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates1 b/test/data/unicode1_leadership_value.updates1 new file mode 100644 index 0000000..dfb9822 --- /dev/null +++ b/test/data/unicode1_leadership_value.updates1 @@ -0,0 +1 @@ +{"updates": ["options.unicode.unicode", "options.unicode.unicode1", "options.unicode.unicode2", "options.unicode.unicode3"], "model": [{"key": "options.unicode.unicode", "owner": "default", "required": true, "value": ["val1", "val2"]}, {"index": 0, "key": "options.unicode.unicode2", "owner": "default", "value": "follower2"}, {"index": 1, "key": "options.unicode.unicode2", "owner": "default", "value": "follower2"}]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates2 b/test/data/unicode1_leadership_value.updates2 new file mode 100644 index 0000000..79bbd82 --- /dev/null +++ b/test/data/unicode1_leadership_value.updates2 @@ -0,0 +1 @@ +{"updates": ["options.unicode.unicode", "options.unicode.unicode1", "options.unicode.unicode2", "options.unicode.unicode3"], "model": [{"key": "options.unicode.unicode", "required": true, "value": ["val3"], "owner": "user"}, {"key": "options.unicode.unicode1", "index": 0, "value": "super1", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 0, "value": "pas test", "owner": "user"}]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates3 b/test/data/unicode1_leadership_value.updates3 new file mode 100644 index 0000000..0f423a0 --- /dev/null +++ b/test/data/unicode1_leadership_value.updates3 @@ -0,0 +1 @@ +{"updates": ["options.unicode.unicode", "options.unicode.unicode2"], "model": [{"key": "options.unicode.unicode", "required": true, "value": ["val3", "val4", "val5"], "owner": "user"}, {"key": "options.unicode.unicode1", "index": 0, "value": "super1", "owner": "user"}, {"key": "options.unicode.unicode1", "index": 1, "value": "super2", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 0, "value": "pas test", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 1, "value": "test", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 2, "value": "follower2", "owner": "default"}, {"key": "options.unicode.unicode3", "index": 1, "value": "super", "owner": "user"}]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates4 b/test/data/unicode1_leadership_value.updates4 new file mode 100644 index 0000000..809f022 --- /dev/null +++ b/test/data/unicode1_leadership_value.updates4 @@ -0,0 +1 @@ +{"model": [{"key": "options.unicode.unicode", "owner": "user", "required": true, "value": ["val3", "val4", "val5"]}, {"index": 0, "key": "options.unicode.unicode1", "owner": "user", "value": "super1"}, {"index": 1, "key": "options.unicode.unicode1", "owner": "user", "value": "super2"}, {"index": 0, "key": "options.unicode.unicode2", "owner": "user", "value": "pas test"}, {"index": 1, "key": "options.unicode.unicode2", "owner": "user", "value": "test"}, {"index": 2, "key": "options.unicode.unicode2", "owner": "default", "value": "follower2"}, {"index": 1, "key": "options.unicode.unicode3", "owner": "user", "value": "super"}], "updates": ["options.unicode.unicode", "options.unicode.unicode2"]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates5 b/test/data/unicode1_leadership_value.updates5 new file mode 100644 index 0000000..a0d1111 --- /dev/null +++ b/test/data/unicode1_leadership_value.updates5 @@ -0,0 +1 @@ +{"updates": ["options.unicode.unicode2"], "model": [{"key": "options.unicode.unicode", "required": true, "value": ["val3", "val4"], "owner": "user"}, {"key": "options.unicode.unicode1", "index": 0, "value": "super1", "owner": "user"}, {"key": "options.unicode.unicode1", "index": 1, "value": "super2", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 0, "value": "pas test", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 1, "value": "follower2", "owner": "default"}, {"key": "options.unicode.unicode3", "index": 1, "value": "super", "owner": "user"}]} \ No newline at end of file diff --git a/test/data/unicode1_leadership_value.updates6 b/test/data/unicode1_leadership_value.updates6 new file mode 100644 index 0000000..e5346fb --- /dev/null +++ b/test/data/unicode1_leadership_value.updates6 @@ -0,0 +1 @@ +{"updates": ["options.unicode.unicode2"], "model": [{"key": "options.unicode.unicode", "required": true, "value": ["val3", "val4"], "owner": "user"}, {"key": "options.unicode.unicode1", "index": 0, "value": "super1", "owner": "user"}, {"key": "options.unicode.unicode1", "index": 1, "value": "super2", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 0, "value": "pas test", "owner": "user"}, {"key": "options.unicode.unicode2", "index": 1, "value": "test2", "owner": "user"}, {"key": "options.unicode.unicode3", "index": 1, "value": "super", "owner": "user"}]} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.dict1 b/test/data/unicode1_mod_value.dict1 new file mode 100644 index 0000000..3ffec07 --- /dev/null +++ b/test/data/unicode1_mod_value.dict1 @@ -0,0 +1 @@ +{"options.unicode": "val"} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.dict2 b/test/data/unicode1_mod_value.dict2 new file mode 100644 index 0000000..f37d92a --- /dev/null +++ b/test/data/unicode1_mod_value.dict2 @@ -0,0 +1 @@ +{"options.unicode": null} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.mod1 b/test/data/unicode1_mod_value.mod1 new file mode 100644 index 0000000..2d9850a --- /dev/null +++ b/test/data/unicode1_mod_value.mod1 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode').value.set('val')", "body": {"updates": [{"action": "modify", "name": "options.unicode", "value": "val"}]}} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.mod2 b/test/data/unicode1_mod_value.mod2 new file mode 100644 index 0000000..6b7ce86 --- /dev/null +++ b/test/data/unicode1_mod_value.mod2 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode').value.reset()", "body": {"updates": [{"action": "delete", "name": "options.unicode"}]}} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.updates1 b/test/data/unicode1_mod_value.updates1 new file mode 100644 index 0000000..19c59da --- /dev/null +++ b/test/data/unicode1_mod_value.updates1 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": [{"key": "options.unicode", "owner": "user", "value": "val"}]} \ No newline at end of file diff --git a/test/data/unicode1_mod_value.updates2 b/test/data/unicode1_mod_value.updates2 new file mode 100644 index 0000000..0fd1958 --- /dev/null +++ b/test/data/unicode1_mod_value.updates2 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": []} \ No newline at end of file diff --git a/test/data/unicode1_multi.dict1 b/test/data/unicode1_multi.dict1 new file mode 100644 index 0000000..07ec5a9 --- /dev/null +++ b/test/data/unicode1_multi.dict1 @@ -0,0 +1 @@ +{"options.unicode": ["val"]} \ No newline at end of file diff --git a/test/data/unicode1_multi.mod1 b/test/data/unicode1_multi.mod1 new file mode 100644 index 0000000..f884614 --- /dev/null +++ b/test/data/unicode1_multi.mod1 @@ -0,0 +1 @@ +{"cmd": ["config.option('options.unicode').value.set([undefined])", "config.option('options.unicode').value.set(['val'])"], "body": {"updates": [{"action": "modify", "name": "options.unicode", "value": ["val"]}]}} \ No newline at end of file diff --git a/test/data/unicode1_multi.updates1 b/test/data/unicode1_multi.updates1 new file mode 100644 index 0000000..d013eba --- /dev/null +++ b/test/data/unicode1_multi.updates1 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": [{"key": "options.unicode", "owner": "user", "required": true, "value": ["val"]}]} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.dict1 b/test/data/unicode1_multi_mod_value.dict1 new file mode 100644 index 0000000..90041ac --- /dev/null +++ b/test/data/unicode1_multi_mod_value.dict1 @@ -0,0 +1 @@ +{"options.unicode": ["a", "b"]} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.dict2 b/test/data/unicode1_multi_mod_value.dict2 new file mode 100644 index 0000000..6c027a4 --- /dev/null +++ b/test/data/unicode1_multi_mod_value.dict2 @@ -0,0 +1 @@ +{"options.unicode": ["c", "f", "e"]} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.mod1 b/test/data/unicode1_multi_mod_value.mod1 new file mode 100644 index 0000000..6b7ce86 --- /dev/null +++ b/test/data/unicode1_multi_mod_value.mod1 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode').value.reset()", "body": {"updates": [{"action": "delete", "name": "options.unicode"}]}} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.mod2 b/test/data/unicode1_multi_mod_value.mod2 new file mode 100644 index 0000000..0e5bec6 --- /dev/null +++ b/test/data/unicode1_multi_mod_value.mod2 @@ -0,0 +1 @@ +{"cmd": "config.option('options.unicode').value.set(['c', 'f', 'e'])", "body": {"updates": [{"action": "modify", "name": "options.unicode", "value": ["c", "f", "e"]}]}} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.updates1 b/test/data/unicode1_multi_mod_value.updates1 new file mode 100644 index 0000000..8134e66 --- /dev/null +++ b/test/data/unicode1_multi_mod_value.updates1 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": [{"key": "options.unicode", "owner": "default", "required": true, "value": ["a", "b"]}]} \ No newline at end of file diff --git a/test/data/unicode1_multi_mod_value.updates2 b/test/data/unicode1_multi_mod_value.updates2 new file mode 100644 index 0000000..e304aac --- /dev/null +++ b/test/data/unicode1_multi_mod_value.updates2 @@ -0,0 +1 @@ +{"updates": ["options.unicode"], "model": [{"key": "options.unicode", "owner": "user", "required": true, "value": ["c", "f", "e"]}]} \ No newline at end of file diff --git a/test/test_json.py b/test/test_json.py index cd0308c..df2c019 100644 --- a/test/test_json.py +++ b/test/test_json.py @@ -7,6 +7,7 @@ from os.path import dirname, abspath, join, normpath, splitext # from tiramisu.error import ValueWarning from tiramisu_json_api import Config from tiramisu_json_api.error import PropertiesOptionError +from tiramisu_json_api.setting import undefined # warnings.simplefilter("always", ValueWarning) @@ -57,8 +58,6 @@ def test_get(): for filename in list_data(): if debug: print(filename) - if 'master' in filename: - continue with open(filename, 'r') as fh: json = loads(fh.read()) config = Config(json) @@ -175,8 +174,6 @@ def test_prop2(): def test_info(): for filename in list_data(): - if 'master' in filename: - continue with open(filename, 'r') as fh: json = loads(fh.read()) config = Config(json) @@ -189,35 +186,72 @@ def test_info(): def test_mod(): debug = False - # debug = True - for filename in list_data('.mod1'): - if 'master' in filename: - continue - if debug: - print('test_mod', filename) - with open(filename[:-4] + 'json', 'r') as fh: - json = loads(fh.read()) - # - config = Config(json) - with open(filename) as fh: - mod = loads(fh.read()) - eval(mod['cmd']) - # - if debug: - from pprint import pprint - pprint(config.updates) - print('----------------') - pprint(mod['body']['updates']) - assert config.updates == mod['body']['updates'] - # - with open(filename[:-4] + 'dict1', 'r') as fh: - dico1 = loads(fh.read()) - assert dico1 == config.value.dict() - # - with open(filename[:-4] + 'updates1', 'r') as fh: - data = loads(fh.read()) + debug = True + i = 0 + while True: + i += 1 + lists = list(list_data('.mod{}'.format(i))) + if not lists: + break + for filename in lists: + if debug: + print('test_mod', filename) + with open(filename[:-4] + 'json', 'r') as fh: + json = loads(fh.read()) + # + config = Config(json) + with open(filename) as fh: + mod = loads(fh.read()) + if debug: + print(mod['cmd']) + if isinstance(mod['cmd'], list): + for cmd in mod['cmd']: + eval(cmd) + else: + eval(mod['cmd']) + # if debug: from pprint import pprint - pprint(data) - config.updates_data(data) - assert dico1 == config.value.dict() + pprint(config.updates) + print('----------------') + pprint(mod['body']['updates']) + assert config.updates == mod['body']['updates'] + + with open(filename[:-4] + 'dict{}'.format(i), 'r') as fh: + dico1 = loads(fh.read()) + if debug: + from pprint import pprint + pprint(dico1) + print('----------------') + pprint(config.value.dict()) + assert dico1 == config.value.dict() + + +def test_mod(): + debug = False + # debug = True + i = 0 + while True: + i += 1 + lists = list(list_data('.mod{}'.format(i))) + if not lists: + break + for filename in lists: + if debug: + print('test_mod', filename) + with open(filename[:-4] + 'json', 'r') as fh: + json = loads(fh.read()) + # + config = Config(json) + # + with open(filename[:-4] + 'updates{}'.format(i), 'r') as fh: + data = loads(fh.read()) + config.updates_data(data) + with open(filename[:-4] + 'dict{}'.format(i), 'r') as fh: + dico1 = loads(fh.read()) + if debug: + from pprint import pprint + pprint(dico1) + print('----------------') + pprint(config.value.dict()) + assert dico1 == config.value.dict() diff --git a/tiramisu_json_api/api.py b/tiramisu_json_api/api.py index 0627b44..0ca89f5 100644 --- a/tiramisu_json_api/api.py +++ b/tiramisu_json_api/api.py @@ -71,10 +71,9 @@ class TiramisuOptionOption: def isleader(self): if '.' in self._path: parent_schema = self.config.get_schema(self._path.rsplit('.', 1)[0]) - leader = next(iter(parent_schema['properties'].keys())) - if parent_schema['type'] == 'array' and \ - leader == self._path: - return True + if parent_schema['type'] == 'array': + leader = next(iter(parent_schema['properties'].keys())) + return leader == self._path return False def isfollower(self): @@ -264,14 +263,14 @@ class TiramisuOptionValue(_Value): def set(self, value): type_ = self.schema['type'] - if self.schema.get('isMulti', False): + remote = self.form.get(self.path, {}).get('remote', False) + if self.index is None and self.schema.get('isMulti', False): if not isinstance(value, list): raise Exception('value must be a list') for val in value: self._validate(type_, val) else: self._validate(type_, value) - remote = self.form.get(self.path, {}).get('remote', False) self.config.modify_value(self.path, self.index, value, @@ -501,7 +500,7 @@ class Config: def __init__(self, json): self.model_ori = json['model'] - self.model = self.gen_model(json['model']) + self.gen_model(json['model']) self.form = {} for option in json['form']: if 'key' in option: @@ -519,24 +518,27 @@ class Config: def gen_model(self, model) -> List[Dict]: - ret = {} + self.model = {} for option in model: - key = option['key'] - if 'index' in option: - if key not in ret: - ret[key] = copy(option) - ret[key]['value'] = {} - del ret[key]['index'] - del ret[key]['owner'] - if 'hidden' in ret[key]: - del ret[key]['hidden'] - if option.get('hidden') is True: - ret[key]['value'][option['index']] = () - else: - ret[key]['value'][option['index']] = (option['value'], option['owner']) + self.update_model(option) + + def update_model(self, + model): + key = model['key'] + if 'index' in model: + if key not in self.model: + self.model[key] = copy(model) + self.model[key]['value'] = {} + del self.model[key]['index'] + del self.model[key]['owner'] + if 'hidden' in self.model[key]: + del self.model[key]['hidden'] + if model.get('hidden') is True: + self.model[key]['value'][model['index']] = () else: - ret[key] = option - return ret + self.model[key]['value'][model['index']] = (model['value'], model['owner']) + else: + self.model[key] = model def __getattr__(self, subfunc: str) -> Any: @@ -594,14 +596,10 @@ class Config: path: str, index: Optional[int], remote: bool) -> None: - schema = self.get_schema(path) - value = schema.get('value'); - if value is None and schema.get('isMulti', False): - value = [] self.updates_value('delete', path, index, - value, + None, remote, None) @@ -682,8 +680,10 @@ class Config: if value is None and self.get_schema(path).get('isMulti', False): value = [] else: - model = self.model.get(path, {}) - value = model.get('value') + if index in self.temp.get(path, {}).get('value', {}): + value = self.temp[path].get('value') + else: + value = self.model.get(path, {}).get('value') if value is not None: if index in value: if len(value[index]): @@ -691,7 +691,7 @@ class Config: else: value = PropertiesOptionError(None, None, None, opt_type='option') else: - value = None + value = self.get_schema(path).get('default') else: value = None return value @@ -751,7 +751,7 @@ class Config: del self.updates[-1] if update_last_action: - if value is None: + if action == 'delete' and value is None: if 'value' in last_body: del last_body['value'] else: @@ -762,7 +762,7 @@ class Config: else: data = {'action': action, 'name': path} - if value is not None: + if action != 'delete' and value is not None: data['value'] = value if index is not None: data['index'] = index @@ -777,18 +777,62 @@ class Config: 'model': self.model_ori})) else: - self.temp.setdefault(path, {})['owner'] = 'tmp' - self.temp[path]['value'] = value + if action == 'delete': + self.temp.setdefault(path, {})['owner'] = 'tmp' + if index is None: + value = self.default_value(path) + self.temp[path]['value'] = value + if self.option(path).option.isleader(): + leadership_path = path.rsplit('.', 1)[0] + parent_schema = self.get_schema(leadership_path) + iter_leadership = list(parent_schema['properties'].keys()) + for follower in iter_leadership[1:]: + for idx in range(len(value)): + follower_value = self.get_schema(follower).get('default') + # FIXME PropertiesOptionError? + self.temp.setdefault(follower, {}).setdefault('value', {})[idx] = (follower_value, 'default') + elif self.option(path).option.isleader(): + old_value = self.option(path).value.get() + old_value.pop(index) + self.temp[path]['value'] = old_value + leadership_path = path.rsplit('.', 1)[0] + parent_schema = self.get_schema(leadership_path) + iter_leadership = list(parent_schema['properties'].keys()) + for follower in iter_leadership[1:]: + if index in self.temp.get(follower, {}).get('value', {}): + del self.temp[follower]['value'][index] + if index in self.model.get(follower, {}).get('value', {}): + del self.model[follower]['value'][index] + else: + if index in self.temp.get(path, {}).get('value', {}): + del self.temp[path]['value'][index] + if index in self.model.get(path, {}).get('value', {}): + del self.model[path]['value'][index] + elif index is None: + self.temp.setdefault(path, {})['owner'] = 'tmp' + self.temp[path]['value'] = value + else: + self.temp.setdefault(path, {}).setdefault('value', {})[index] = (value, 'tmp') self.set_dependencies(path, value) self.set_not_equal(path, value) self.do_copy(path, value) + def default_value(self, path): + schema = self.get_schema(path) + value = schema.get('value'); + if value is None and schema.get('isMulti', False): + value = [] + return value + def updates_data(self, data): self.updates = [] self.temp.clear() - # FIXME remove old key ? - for model in data['model']: - self.model[model['key']] = model + for key in data['updates']: + if key in self.model: + del self.model[key] + for model in data['model']: + if key == model['key']: + self.update_model(model) self.model_ori = data['model'] def test_value(self,