From 13dc94a6877bc7a292008db3faa7328b907a8847 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 28 Feb 2019 22:12:46 +0100 Subject: [PATCH] update tests --- ...onfig_unicode1_master_slaves_requires.dict | 2 +- ...unicode1_master_slaves_requires_slave.dict | 2 +- ...e1_master_slaves_requires_slave_value.dict | 2 +- ...nicode1_master_slaves_requires_slaves.dict | 2 +- test/data/unicode1.dict1 | 1 + test/data/unicode1.mod1 | 5 ++ test/data/unicode1.updates1 | 4 + .../data/unicode1_master_slaves_requires.dict | 2 +- ...unicode1_master_slaves_requires_slave.dict | 2 +- ...e1_master_slaves_requires_slave_value.dict | 2 +- ...nicode1_master_slaves_requires_slaves.dict | 2 +- test/test_json.py | 50 +++++++++-- tiramisu_json_api/api.py | 83 ++++++++++++++----- 13 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 test/data/unicode1.dict1 create mode 100644 test/data/unicode1.mod1 create mode 100644 test/data/unicode1.updates1 diff --git a/test/data/subconfig_unicode1_master_slaves_requires.dict b/test/data/subconfig_unicode1_master_slaves_requires.dict index 005ca1a..e1304a1 100644 --- a/test/data/subconfig_unicode1_master_slaves_requires.dict +++ b/test/data/subconfig_unicode1_master_slaves_requires.dict @@ -1 +1 @@ -{"unicode1_master_slaves_requires.options.unicode.unicode": [], "unicode1_master_slaves_requires.options.unicode.unicode1": [], "unicode1_master_slaves_requires.options.unicode.unicode2": []} \ No newline at end of file +{"unicode1_master_slaves_requires.options.unicode.unicode": [], "unicode1_master_slaves_requires.options.unicode.unicode1": [], "unicode1_master_slaves_requires.options.unicode.unicode2": [], "unicode1_master_slaves_requires.options.unicode.unicode3": []} diff --git a/test/data/subconfig_unicode1_master_slaves_requires_slave.dict b/test/data/subconfig_unicode1_master_slaves_requires_slave.dict index fd50f77..e83e58c 100644 --- a/test/data/subconfig_unicode1_master_slaves_requires_slave.dict +++ b/test/data/subconfig_unicode1_master_slaves_requires_slave.dict @@ -1 +1 @@ -{"unicode1_master_slaves_requires_slave.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode2": []} \ No newline at end of file +{"unicode1_master_slaves_requires_slave.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode2": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode3": []} diff --git a/test/data/subconfig_unicode1_master_slaves_requires_slave_value.dict b/test/data/subconfig_unicode1_master_slaves_requires_slave_value.dict index d6e98b0..734d37e 100644 --- a/test/data/subconfig_unicode1_master_slaves_requires_slave_value.dict +++ b/test/data/subconfig_unicode1_master_slaves_requires_slave_value.dict @@ -1 +1 @@ -{"unicode1_master_slaves_requires_slave_value.options.unicode1.unicode1": ["test", "pas test"], "unicode1_master_slaves_requires_slave_value.options.unicode1.unicode2": ["super1", null]} \ No newline at end of file +{"unicode1_master_slaves_requires_slave_value.options.unicode1.unicode1": ["test", "pas test"], "unicode1_master_slaves_requires_slave_value.options.unicode1.unicode2": ["super1", null], "unicode1_master_slaves_requires_slave_value.options.unicode1.unicode3": ["super1", null]} diff --git a/test/data/subconfig_unicode1_master_slaves_requires_slaves.dict b/test/data/subconfig_unicode1_master_slaves_requires_slaves.dict index a692910..7fec689 100644 --- a/test/data/subconfig_unicode1_master_slaves_requires_slaves.dict +++ b/test/data/subconfig_unicode1_master_slaves_requires_slaves.dict @@ -1 +1 @@ -{"unicode1_master_slaves_requires_slaves.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode2": []} \ No newline at end of file +{"unicode1_master_slaves_requires_slaves.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode2": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode3": []} diff --git a/test/data/unicode1.dict1 b/test/data/unicode1.dict1 new file mode 100644 index 0000000..674e06e --- /dev/null +++ b/test/data/unicode1.dict1 @@ -0,0 +1 @@ +{"options.unicode": "val"} diff --git a/test/data/unicode1.mod1 b/test/data/unicode1.mod1 new file mode 100644 index 0000000..f58f1b8 --- /dev/null +++ b/test/data/unicode1.mod1 @@ -0,0 +1,5 @@ +{"cmd": "config.option('options.unicode').value.set('val')", + "body": {"updates": [{"action": "modify", + "name": "options.unicode", + "value": "val"}]} +} diff --git a/test/data/unicode1.updates1 b/test/data/unicode1.updates1 new file mode 100644 index 0000000..c072aab --- /dev/null +++ b/test/data/unicode1.updates1 @@ -0,0 +1,4 @@ +{"updates": ["options.unicode"], + "model": [{"key": "options.unicode", + "owner": "user", + "value": "val"}]} diff --git a/test/data/unicode1_master_slaves_requires.dict b/test/data/unicode1_master_slaves_requires.dict index 105f009..e039126 100644 --- a/test/data/unicode1_master_slaves_requires.dict +++ b/test/data/unicode1_master_slaves_requires.dict @@ -1 +1 @@ -{"options.unicode.unicode": [], "options.unicode.unicode1": [], "options.unicode.unicode2": []} \ No newline at end of file +{"options.unicode.unicode": [], "options.unicode.unicode1": [], "options.unicode.unicode2": [], "options.unicode.unicode3": []} diff --git a/test/data/unicode1_master_slaves_requires_slave.dict b/test/data/unicode1_master_slaves_requires_slave.dict index ae4c715..94373bf 100644 --- a/test/data/unicode1_master_slaves_requires_slave.dict +++ b/test/data/unicode1_master_slaves_requires_slave.dict @@ -1 +1 @@ -{"options.unicode1.unicode1": [], "options.unicode1.unicode2": []} \ No newline at end of file +{"options.unicode1.unicode1": [], "options.unicode1.unicode2": [], "options.unicode1.unicode3": []} diff --git a/test/data/unicode1_master_slaves_requires_slave_value.dict b/test/data/unicode1_master_slaves_requires_slave_value.dict index 8bb3b55..895d77b 100644 --- a/test/data/unicode1_master_slaves_requires_slave_value.dict +++ b/test/data/unicode1_master_slaves_requires_slave_value.dict @@ -1 +1 @@ -{"options.unicode1.unicode1": ["test", "pas test"], "options.unicode1.unicode2": ["super1", null]} \ No newline at end of file +{"options.unicode1.unicode1": ["test", "pas test"], "options.unicode1.unicode2": ["super1", null], "options.unicode1.unicode3": ["super1", null]} diff --git a/test/data/unicode1_master_slaves_requires_slaves.dict b/test/data/unicode1_master_slaves_requires_slaves.dict index ae4c715..94373bf 100644 --- a/test/data/unicode1_master_slaves_requires_slaves.dict +++ b/test/data/unicode1_master_slaves_requires_slaves.dict @@ -1 +1 @@ -{"options.unicode1.unicode1": [], "options.unicode1.unicode2": []} \ No newline at end of file +{"options.unicode1.unicode1": [], "options.unicode1.unicode2": [], "options.unicode1.unicode3": []} diff --git a/test/test_json.py b/test/test_json.py index ef7127e..d511d36 100644 --- a/test/test_json.py +++ b/test/test_json.py @@ -11,12 +11,12 @@ from tiramisu_json_api import Config # warnings.simplefilter("always", ValueWarning) -def list_data(): +def list_data(ext='.json'): datadir = join(normpath(dirname(abspath(__file__))), 'data') filenames = listdir(datadir) filenames.sort() for filename in filenames: - if filename.endswith('.json'): + if filename.endswith(ext): yield join(datadir, filename) @@ -24,8 +24,6 @@ def test_dict(): debug = False # debug = True for filename in list_data(): - if 'master' in filename: - continue if debug: print('test_jsons', filename) with open(filename, 'r') as fh: @@ -33,7 +31,13 @@ def test_dict(): # config = Config(json) with open(filename[:-4] + 'dict', 'r') as fh: - assert loads(fh.read()) == config.value.dict() + dico = loads(fh.read()) + if debug: + from pprint import pprint + pprint(dico) + print('-----') + pprint(config.value.dict()) + assert dico == config.value.dict() def test_get(): @@ -106,3 +110,39 @@ def test_info(): if info == 'type' and value in ['filename', 'ip', 'email', 'username']: continue assert getattr(config.option(key).option, info)() == value, 'error for {} in {}'.format(info, filename) + + +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()) + if debug: + from pprint import pprint + pprint(data) + config.updates_data(data) + assert dico1 == config.value.dict() diff --git a/tiramisu_json_api/api.py b/tiramisu_json_api/api.py index c42ca6c..36ca75c 100644 --- a/tiramisu_json_api/api.py +++ b/tiramisu_json_api/api.py @@ -1,4 +1,5 @@ from typing import Optional, Dict, List, Any +from copy import copy import warnings import re @@ -126,22 +127,30 @@ class _Value: schema: Dict, root: str, fullpath: bool, - withwarning: bool): + withwarning: bool, + parent_key: str=None) -> None: + len_master = None for key, option in schema['properties'].items(): hidden = self.temp.get(key, {}).get('hidden', None) model_display = not self.model.get(key, {}).get('hidden', False) and \ self.model.get(key, {}).get('display', True) + if hidden is False or (hidden is None and model_display is True): - if option['type'] == 'object': + if option['type'] in ['object', 'array']: self._dict_walk(ret, option, root, fullpath, - withwarning) + withwarning, + key) else: - value = self.config.get_value(key) + value = self.config.get_value(key, len_master) + if parent_key is not None and schema.get('type') == 'array' and len_master is None: + len_master = len(value) self._display_warnings(key, value, option['type'], option['name'], withwarning) ret[key] = value + elif parent_key is not None and schema.get('type') == 'array' and len_master is None: + break def dict(self, fullpath: bool=False, @@ -470,7 +479,7 @@ class Config: def __init__(self, json): self.model_ori = json['model'] - self.model = {option['key']: option for option in json['model']} + self.model = self.gen_model(json['model']) self.form = {} for option in json['form']: if 'key' in option: @@ -486,6 +495,23 @@ class Config: else: self.root = '' + def gen_model(self, + model) -> List[Dict]: + ret = {} + for option in model: + key = option['key'] + if 'index' in option: + if key not in ret: + ret[key] = copy(option) + ret[key]['value'] = {ret[key]['index']: (ret[key]['value'], ret[key]['owner'])} + del ret[key]['index'] + del ret[key]['owner'] + elif option.get('hidden') is not True: + ret[key]['value'][option['index']] = (option['value'], option['owner']) + else: + ret[key] = option + return ret + def __getattr__(self, subfunc: str) -> Any: if subfunc == 'property': @@ -603,13 +629,29 @@ class Config: return value def get_value(self, - path: str) -> Any: - if 'value' in self.temp.get(path, {}): - value = self.temp[path]['value'] + path: str, + len_master: int=None) -> Any: + if len_master is None: + if 'value' in self.temp.get(path, {}): + value = self.temp[path]['value'] + else: + value = self.model.get(path, {}).get('value') + if value is None and self.get_schema(path).get('isMulti', False): + value = [] else: - value = self.model.get(path, {}).get('value') - if value is None and self.get_schema(path).get('isMulti', False): - value = [] + value = self.temp.get(path) + if value is None: + value = self.model.get(path) + if value is None: + value = [None] * len_master + else: + val = [] + for index in range(len_master): + if index in value['value']: + val.append(value['value'][index][0]) + else: + val.append(None) + value = val return value def get_owner(self, @@ -676,14 +718,9 @@ class Config: match = True if match: if remote: - ret = self.send_data({'updates': self.updates, - 'model': self.model_ori}) - self.updates = [] - self.temp.clear() - # FIXME remove old key ? - for model in ret['model']: - self.model[model['key']] = model - self.model_ori = ret['model'] + self.updates_data(self.send_data({'updates': self.updates, + 'model': self.model_ori})) + else: self.temp.setdefault(path, {})['owner'] = 'tmp' self.temp[path]['value'] = value @@ -691,6 +728,14 @@ class Config: self.set_not_equal(path, value) self.do_copy(path, 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 + self.model_ori = data['model'] + def test_value(self, path: str, value: Any,