From 829247e79fad1a040608208ab7118355add91f08 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 17 Apr 2019 19:13:17 +0200 Subject: [PATCH] better symlink support for option.dict() --- tests/dict/data/unicode2_symlink.dict | 4 + tests/dict/data/unicode2_symlink.dict1 | 4 + tests/dict/data/unicode2_symlink.json | 42 ++++++++++ tests/dict/data/unicode2_symlink.mod1 | 4 + tests/dict/data/unicode2_symlink.py | 12 +++ tests/dict/data/unicode2_symlink.updates1 | 11 +++ tests/dict/test_json.py | 96 ++++++++++++++--------- 7 files changed, 138 insertions(+), 35 deletions(-) create mode 100644 tests/dict/data/unicode2_symlink.dict create mode 100644 tests/dict/data/unicode2_symlink.dict1 create mode 100644 tests/dict/data/unicode2_symlink.json create mode 100644 tests/dict/data/unicode2_symlink.mod1 create mode 100644 tests/dict/data/unicode2_symlink.py create mode 100644 tests/dict/data/unicode2_symlink.updates1 diff --git a/tests/dict/data/unicode2_symlink.dict b/tests/dict/data/unicode2_symlink.dict new file mode 100644 index 0000000..6fbce04 --- /dev/null +++ b/tests/dict/data/unicode2_symlink.dict @@ -0,0 +1,4 @@ +{ + "options.unicode1": "test", + "options.unicode2": "test" +} \ No newline at end of file diff --git a/tests/dict/data/unicode2_symlink.dict1 b/tests/dict/data/unicode2_symlink.dict1 new file mode 100644 index 0000000..749974e --- /dev/null +++ b/tests/dict/data/unicode2_symlink.dict1 @@ -0,0 +1,4 @@ +{ + "options.unicode1": "val", + "options.unicode2": "val" +} \ No newline at end of file diff --git a/tests/dict/data/unicode2_symlink.json b/tests/dict/data/unicode2_symlink.json new file mode 100644 index 0000000..1341926 --- /dev/null +++ b/tests/dict/data/unicode2_symlink.json @@ -0,0 +1,42 @@ +{ + "schema": { + "options": { + "name": "options", + "properties": { + "options.unicode1": { + "name": "options.unicode1", + "type": "string", + "value": "test", + "title": "Unicode 1" + }, + "options.unicode2": { + "name": "options.unicode2", + "type": "symlink", + "opt_path": "options.unicode1", + "title": "Unicode 1" + } + }, + "type": "object", + "title": "Common configuration" + } + }, + "model": { + "options.unicode1": { + "value": "test", + "owner": "default" + } + }, + "form": { + "options.unicode1": { + "clearable": true, + "type": "input" + }, + "null": [ + { + "title": "Configurer", + "type": "submit" + } + ] + }, + "version": "1.0" +} diff --git a/tests/dict/data/unicode2_symlink.mod1 b/tests/dict/data/unicode2_symlink.mod1 new file mode 100644 index 0000000..8bd7c14 --- /dev/null +++ b/tests/dict/data/unicode2_symlink.mod1 @@ -0,0 +1,4 @@ +{"cmd": "config.option('options.unicode1').value.set('val')", + "body": {"updates": [{"action": "modify", + "name": "options.unicode1", + "value": "val"}]}} diff --git a/tests/dict/data/unicode2_symlink.py b/tests/dict/data/unicode2_symlink.py new file mode 100644 index 0000000..b4d5712 --- /dev/null +++ b/tests/dict/data/unicode2_symlink.py @@ -0,0 +1,12 @@ +"""two unicode options +""" +from tiramisu.option import StrOption, OptionDescription, SymLinkOption + +def get_description(): + """generate description for this test + """ + option1 = StrOption('unicode1', "Unicode 1", 'test') + option2 = SymLinkOption('unicode2', option1) + descr1 = OptionDescription("options", "Common configuration", [option1, option2]) + descr = OptionDescription("unicode2_symlink", "One unicode, one symlink", [descr1]) + return descr diff --git a/tests/dict/data/unicode2_symlink.updates1 b/tests/dict/data/unicode2_symlink.updates1 new file mode 100644 index 0000000..5dc2136 --- /dev/null +++ b/tests/dict/data/unicode2_symlink.updates1 @@ -0,0 +1,11 @@ +{ + "updates": [ + "options.unicode1" + ], + "model": { + "options.unicode1": { + "value": "val", + "owner": "user" + } + } +} diff --git a/tests/dict/test_json.py b/tests/dict/test_json.py index 566340c..e909a97 100644 --- a/tests/dict/test_json.py +++ b/tests/dict/test_json.py @@ -64,7 +64,7 @@ def parse_expected(schema, all_options): for key, value in schema['properties'].items(): if 'properties' in value: parse_expected(value, all_options) - else: + elif value.get('type') != 'symlink': all_options.append(key) @@ -206,6 +206,8 @@ def test_jsons_subconfig(): for key_schema, val_schema in schema.items(): key = modulepath + '.' + key_schema val_schema['name'] = key + if 'opt_path' in val_schema: + val_schema['opt_path'] = modulepath + '.' + val_schema['opt_path'] if 'properties' in val_schema: val_schema['properties'] = change_key(val_schema['properties']) new_schema[key] = val_schema @@ -273,14 +275,17 @@ def test_updates(): else: root = None # dict before modification - with open(join(datadir, modulepath + '.dict'), 'r') as fh: - dico_ori = loads(fh.read()) - if issub: - new_dico_ori = {} - for key, value in dico_ori.items(): - key = modulepath + '.' + key - new_dico_ori[key] = value - dico_ori = new_dico_ori + if not isfile(join(datadir, modulepath + '.dict')): + dico_ori = None + else: + with open(join(datadir, modulepath + '.dict'), 'r') as fh: + dico_ori = loads(fh.read()) + if issub: + new_dico_ori = {} + for key, value in dico_ori.items(): + key = modulepath + '.' + key + new_dico_ori[key] = value + dico_ori = new_dico_ori # modify config with open(join(datadir, modulepath + '.mod{}'.format(idx)), 'r') as fh: body = loads(fh.read())['body'] @@ -288,25 +293,31 @@ def test_updates(): for value in body['updates']: value['name'] = modulepath + '.' + value['name'] # returns of set_updates - with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'r') as fh: - values = loads(fh.read()) - if issub: - for lidx, key in enumerate(values['updates']): - values['updates'][lidx] = modulepath + '.' + key - if 'model' in values: - new_model = {} - for key, value in values['model'].items(): - new_model[modulepath + '.' + key] = value - values['model'] = new_model + if not isfile(join(datadir, modulepath + '.updates{}'.format(idx))): + values = None + else: + with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'r') as fh: + values = loads(fh.read()) + if issub: + for lidx, key in enumerate(values['updates']): + values['updates'][lidx] = modulepath + '.' + key + if 'model' in values: + new_model = {} + for key, value in values['model'].items(): + new_model[modulepath + '.' + key] = value + values['model'] = new_model # dict after modification - with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'r') as fh: - dico_mod = loads(fh.read()) - if issub: - new_dico = {} - for key, value in dico_mod.items(): - key = modulepath + '.' + key - new_dico[key] = value - dico_mod = new_dico + if not isfile(join(datadir, modulepath + '.dict{}'.format(idx))): + dico_mod = None + else: + with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'r') as fh: + dico_mod = loads(fh.read()) + if issub: + new_dico = {} + for key, value in dico_mod.items(): + key = modulepath + '.' + key + new_dico[key] = value + dico_mod = new_dico if root is None: root_path = '' else: @@ -323,7 +334,12 @@ def test_updates(): if isfile(join(datadir, modulepath + '.mod')): with open(join(datadir, modulepath + '.mod'), 'r') as fh: eval(fh.read()) - assert config.value.dict() == dico_ori, "clearable {}, remote: {}, filename: {}".format(clearable, remote, filename) + if dico_ori is None: + if clearable == 'minimum' and remote == 'minimum': + with open(join(datadir, modulepath + '.dict'), 'w') as fh: + dump(config.value.dict(), fh, indent=2) + else: + assert config.value.dict() == dico_ori, "clearable {}, remote: {}, filename: {}".format(clearable, remote, filename) if root is None: suboption = config.option else: @@ -336,12 +352,22 @@ def test_updates(): bodym = body if with_model: cal_values = suboption.updates(bodym) - if debug: - from pprint import pprint - pprint(cal_values) - print('------------') - pprint(values) - assert cal_values == values + if values is None: + if clearable == 'minimum' and remote == 'minimum': + with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'w') as fh: + dump(cal_values, fh, indent=2) + else: + if debug: + from pprint import pprint + pprint(cal_values) + print('------------') + pprint(values) + assert cal_values == values else: assert suboption.updates(bodym) is None - assert config.value.dict() == dico_mod + if dico_mod is None: + if clearable == 'minimum' and remote == 'minimum': + with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'w') as fh: + dump(config.value.dict(), fh, indent=2) + else: + assert config.value.dict() == dico_mod