update tests

This commit is contained in:
Emmanuel Garette 2019-02-28 22:12:46 +01:00
parent f96a59680a
commit 13dc94a687
13 changed files with 127 additions and 32 deletions

View File

@ -1 +1 @@
{"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.unicode": [], "unicode1_master_slaves_requires.options.unicode.unicode1": [], "unicode1_master_slaves_requires.options.unicode.unicode2": [], "unicode1_master_slaves_requires.options.unicode.unicode3": []}

View File

@ -1 +1 @@
{"unicode1_master_slaves_requires_slave.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode2": []} {"unicode1_master_slaves_requires_slave.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode2": [], "unicode1_master_slaves_requires_slave.options.unicode1.unicode3": []}

View File

@ -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]} {"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]}

View File

@ -1 +1 @@
{"unicode1_master_slaves_requires_slaves.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode2": []} {"unicode1_master_slaves_requires_slaves.options.unicode1.unicode1": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode2": [], "unicode1_master_slaves_requires_slaves.options.unicode1.unicode3": []}

1
test/data/unicode1.dict1 Normal file
View File

@ -0,0 +1 @@
{"options.unicode": "val"}

5
test/data/unicode1.mod1 Normal file
View File

@ -0,0 +1,5 @@
{"cmd": "config.option('options.unicode').value.set('val')",
"body": {"updates": [{"action": "modify",
"name": "options.unicode",
"value": "val"}]}
}

View File

@ -0,0 +1,4 @@
{"updates": ["options.unicode"],
"model": [{"key": "options.unicode",
"owner": "user",
"value": "val"}]}

View File

@ -1 +1 @@
{"options.unicode.unicode": [], "options.unicode.unicode1": [], "options.unicode.unicode2": []} {"options.unicode.unicode": [], "options.unicode.unicode1": [], "options.unicode.unicode2": [], "options.unicode.unicode3": []}

View File

@ -1 +1 @@
{"options.unicode1.unicode1": [], "options.unicode1.unicode2": []} {"options.unicode1.unicode1": [], "options.unicode1.unicode2": [], "options.unicode1.unicode3": []}

View File

@ -1 +1 @@
{"options.unicode1.unicode1": ["test", "pas test"], "options.unicode1.unicode2": ["super1", null]} {"options.unicode1.unicode1": ["test", "pas test"], "options.unicode1.unicode2": ["super1", null], "options.unicode1.unicode3": ["super1", null]}

View File

@ -1 +1 @@
{"options.unicode1.unicode1": [], "options.unicode1.unicode2": []} {"options.unicode1.unicode1": [], "options.unicode1.unicode2": [], "options.unicode1.unicode3": []}

View File

@ -11,12 +11,12 @@ from tiramisu_json_api import Config
# warnings.simplefilter("always", ValueWarning) # warnings.simplefilter("always", ValueWarning)
def list_data(): def list_data(ext='.json'):
datadir = join(normpath(dirname(abspath(__file__))), 'data') datadir = join(normpath(dirname(abspath(__file__))), 'data')
filenames = listdir(datadir) filenames = listdir(datadir)
filenames.sort() filenames.sort()
for filename in filenames: for filename in filenames:
if filename.endswith('.json'): if filename.endswith(ext):
yield join(datadir, filename) yield join(datadir, filename)
@ -24,8 +24,6 @@ def test_dict():
debug = False debug = False
# debug = True # debug = True
for filename in list_data(): for filename in list_data():
if 'master' in filename:
continue
if debug: if debug:
print('test_jsons', filename) print('test_jsons', filename)
with open(filename, 'r') as fh: with open(filename, 'r') as fh:
@ -33,7 +31,13 @@ def test_dict():
# #
config = Config(json) config = Config(json)
with open(filename[:-4] + 'dict', 'r') as fh: 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(): def test_get():
@ -106,3 +110,39 @@ def test_info():
if info == 'type' and value in ['filename', 'ip', 'email', 'username']: if info == 'type' and value in ['filename', 'ip', 'email', 'username']:
continue continue
assert getattr(config.option(key).option, info)() == value, 'error for {} in {}'.format(info, filename) 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()

View File

@ -1,4 +1,5 @@
from typing import Optional, Dict, List, Any from typing import Optional, Dict, List, Any
from copy import copy
import warnings import warnings
import re import re
@ -126,22 +127,30 @@ class _Value:
schema: Dict, schema: Dict,
root: str, root: str,
fullpath: bool, fullpath: bool,
withwarning: bool): withwarning: bool,
parent_key: str=None) -> None:
len_master = None
for key, option in schema['properties'].items(): for key, option in schema['properties'].items():
hidden = self.temp.get(key, {}).get('hidden', None) hidden = self.temp.get(key, {}).get('hidden', None)
model_display = not self.model.get(key, {}).get('hidden', False) and \ model_display = not self.model.get(key, {}).get('hidden', False) and \
self.model.get(key, {}).get('display', True) self.model.get(key, {}).get('display', True)
if hidden is False or (hidden is None and model_display is 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, self._dict_walk(ret,
option, option,
root, root,
fullpath, fullpath,
withwarning) withwarning,
key)
else: 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) self._display_warnings(key, value, option['type'], option['name'], withwarning)
ret[key] = value ret[key] = value
elif parent_key is not None and schema.get('type') == 'array' and len_master is None:
break
def dict(self, def dict(self,
fullpath: bool=False, fullpath: bool=False,
@ -470,7 +479,7 @@ class Config:
def __init__(self, def __init__(self,
json): json):
self.model_ori = json['model'] self.model_ori = json['model']
self.model = {option['key']: option for option in json['model']} self.model = self.gen_model(json['model'])
self.form = {} self.form = {}
for option in json['form']: for option in json['form']:
if 'key' in option: if 'key' in option:
@ -486,6 +495,23 @@ class Config:
else: else:
self.root = '' 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, def __getattr__(self,
subfunc: str) -> Any: subfunc: str) -> Any:
if subfunc == 'property': if subfunc == 'property':
@ -603,13 +629,29 @@ class Config:
return value return value
def get_value(self, def get_value(self,
path: str) -> Any: path: str,
if 'value' in self.temp.get(path, {}): len_master: int=None) -> Any:
value = self.temp[path]['value'] 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: else:
value = self.model.get(path, {}).get('value') value = self.temp.get(path)
if value is None and self.get_schema(path).get('isMulti', False): if value is None:
value = [] 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 return value
def get_owner(self, def get_owner(self,
@ -676,14 +718,9 @@ class Config:
match = True match = True
if match: if match:
if remote: if remote:
ret = self.send_data({'updates': self.updates, self.updates_data(self.send_data({'updates': self.updates,
'model': self.model_ori}) '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']
else: else:
self.temp.setdefault(path, {})['owner'] = 'tmp' self.temp.setdefault(path, {})['owner'] = 'tmp'
self.temp[path]['value'] = value self.temp[path]['value'] = value
@ -691,6 +728,14 @@ class Config:
self.set_not_equal(path, value) self.set_not_equal(path, value)
self.do_copy(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, def test_value(self,
path: str, path: str,
value: Any, value: Any,