tiramisu-api-python/test/test_json.py

273 lines
9.4 KiB
Python
Raw Normal View History

2019-02-14 21:25:00 +01:00
# -*- coding: utf-8 -*-
from json import loads
from os import listdir
from os.path import dirname, abspath, join, normpath, splitext
# import warnings
# from tiramisu.error import ValueWarning
from tiramisu_json_api import Config
from tiramisu_json_api.error import PropertiesOptionError
2019-03-16 22:51:39 +01:00
from tiramisu_json_api.setting import undefined
2019-02-14 21:25:00 +01:00
# warnings.simplefilter("always", ValueWarning)
2019-02-28 22:12:46 +01:00
def list_data(ext='.json'):
2019-02-14 21:25:00 +01:00
datadir = join(normpath(dirname(abspath(__file__))), 'data')
filenames = listdir(datadir)
filenames.sort()
for filename in filenames:
2019-02-28 22:12:46 +01:00
if filename.endswith(ext):
2019-02-14 21:25:00 +01:00
yield join(datadir, filename)
def error_to_str(dico):
for key, value in dico.items():
if isinstance(value, list):
for idx, val in enumerate(value):
if (isinstance(val, str) and val.startswith('cannot access to')) or isinstance(val, PropertiesOptionError):
dico[key][idx] = "PropertiesOptionError"
return dico
2019-03-28 07:53:29 +01:00
# config.option().value.dict()
2019-02-16 20:23:50 +01:00
def test_dict():
2019-02-14 21:25:00 +01:00
debug = False
# debug = True
for filename in list_data():
if debug:
print('test_jsons', filename)
with open(filename, 'r') as fh:
json = loads(fh.read())
#
config = Config(json)
with open(filename[:-4] + 'dict', 'r') as fh:
2019-02-28 22:12:46 +01:00
dico = loads(fh.read())
2019-02-28 22:12:46 +01:00
if debug:
from pprint import pprint
pprint(dico)
print('-----')
pprint(config.value.dict())
assert error_to_str(dico) == error_to_str(config.value.dict())
2019-02-16 20:23:50 +01:00
2019-03-28 07:53:29 +01:00
# config.option().value.get()
2019-02-16 20:23:50 +01:00
def test_get():
debug = False
# debug = True
2019-02-16 20:23:50 +01:00
for filename in list_data():
if debug:
print(filename)
2019-02-16 20:23:50 +01:00
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'dict', 'r') as fh:
dico = error_to_str(loads(fh.read()))
2019-02-16 20:23:50 +01:00
for key, value in dico.items():
if config.option(key).option.isleader():
leader_len = len(value)
if config.option(key).option.isfollower():
values = []
for index in range(leader_len):
val = config.option(key, index).value.get()
if isinstance(val, PropertiesOptionError):
val = "PropertiesOptionError"
values.append(val)
assert value == values
else:
assert value == config.option(key).value.get()
2019-02-16 20:23:50 +01:00
2019-03-28 07:53:29 +01:00
# config.option().owner.get()
2019-02-16 20:23:50 +01:00
def test_owner():
debug = False
# debug = True
2019-02-16 20:23:50 +01:00
for filename in list_data():
if debug:
print(filename)
2019-02-16 20:23:50 +01:00
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'owner', 'r') as fh:
dico = loads(fh.read())
for key, value in dico.items():
if debug:
print('key', key)
if config.option(key).option.isleader():
leader_len = len(config.option(key).value.get())
if config.option(key).option.isfollower():
values = {}
for index in range(leader_len):
try:
values[str(index)] = config.option(key, index).owner.get()
except PropertiesOptionError:
pass
2019-04-03 19:42:59 +02:00
if debug:
print(value)
print('------------------')
print(values)
assert value == values
else:
2019-04-03 19:42:59 +02:00
if debug:
print(value)
print('------------------')
print({'null': config.option(key).owner.get()})
assert value == {'null': config.option(key).owner.get()}
2019-02-16 20:23:50 +01:00
2019-03-28 07:53:29 +01:00
# config.option().property.get()
2019-02-16 20:23:50 +01:00
def test_prop():
debug = False
# debug = True
2019-02-16 20:23:50 +01:00
for filename in list_data():
if debug:
print(filename)
2019-02-16 20:23:50 +01:00
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'prop', 'r') as fh:
dico = loads(fh.read())
for key, value in dico.items():
if debug:
print('key', key)
for key_, val in value.items():
value[key_] = set(val)
if config.option(key).option.isleader():
leader_len = len(config.option(key).value.get())
if config.option(key).option.isfollower():
props = {}
for index in range(leader_len):
try:
props[str(index)] = set(config.option(key, index).property.get())
except PropertiesOptionError:
pass
if 'clearable' in props[str(index)]:
props[str(index)].remove('clearable')
else:
props = {'null': set(config.option(key).property.get())}
if 'clearable' in props['null']:
props['null'].remove('clearable')
assert value == props
2019-02-16 20:23:50 +01:00
2019-03-28 07:53:29 +01:00
# config.option().property.get(True)
2019-02-16 20:23:50 +01:00
def test_prop2():
debug = False
# debug = True
2019-02-16 20:23:50 +01:00
for filename in list_data():
2019-03-28 07:53:29 +01:00
if debug:
print(filename)
2019-02-16 20:23:50 +01:00
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'prop2', 'r') as fh:
dico = loads(fh.read())
for key, value in dico.items():
if debug:
print('key', key)
for key_, val in value.items():
value[key_] = set(val)
if config.option(key).option.isleader():
leader_len = len(config.option(key).value.get())
if config.option(key).option.isfollower():
props = {}
for index in range(leader_len):
try:
props[str(index)] = set(config.option(key, index).property.get(True))
except PropertiesOptionError:
pass
if 'clearable' in props[str(index)]:
props[str(index)].remove('clearable')
else:
props = {'null': set(config.option(key).property.get(True))}
if 'clearable' in props['null']:
props['null'].remove('clearable')
assert value == props
2019-02-16 20:23:50 +01:00
def test_info():
for filename in list_data():
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'info', 'r') as fh:
dico = loads(fh.read())
for key, values in dico.items():
for info, value in values.items():
assert getattr(config.option(key).option, info)() == value, 'error for {} in {}'.format(info, filename)
2019-02-28 22:12:46 +01:00
2019-03-16 22:51:39 +01:00
def test_mod():
debug = False
2019-03-28 07:53:29 +01:00
# debug = True
2019-03-16 22:51:39 +01:00
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(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()
2019-03-28 07:53:29 +01:00
def test_mod2():
2019-02-28 22:12:46 +01:00
debug = False
# debug = True
2019-03-16 22:51:39 +01:00
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())
2019-02-28 22:12:46 +01:00
if debug:
from pprint import pprint
2019-03-16 22:51:39 +01:00
pprint(dico1)
print('----------------')
pprint(config.value.dict())
assert dico1 == config.value.dict()