tiramisu-api-python/test/test_json.py

149 lines
4.6 KiB
Python

# -*- 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
# warnings.simplefilter("always", ValueWarning)
def list_data(ext='.json'):
datadir = join(normpath(dirname(abspath(__file__))), 'data')
filenames = listdir(datadir)
filenames.sort()
for filename in filenames:
if filename.endswith(ext):
yield join(datadir, filename)
def test_dict():
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:
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():
for filename in list_data():
if 'master' in filename:
continue
with open(filename, 'r') as fh:
json = loads(fh.read())
config = Config(json)
with open(filename[:-4] + 'dict', 'r') as fh:
dico = loads(fh.read())
for key, value in dico.items():
assert value == config.option(key).value.get()
def test_owner():
for filename in list_data():
if 'master' in filename:
continue
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():
assert value == config.option(key).owner.get()
def test_prop():
for filename in list_data():
if 'master' in filename:
continue
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():
props = set(config.option(key).property.get())
if 'clearable' in props:
props.remove('clearable')
assert set(value) == props, 'error in {}'.format(filename)
def test_prop2():
for filename in list_data():
if 'master' in filename:
continue
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():
props = set(config.option(key).property.get(True))
assert set(value) == props, 'error in {}'.format(filename)
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)
with open(filename[:-4] + 'info', 'r') as fh:
dico = loads(fh.read())
for key, values in dico.items():
for info, value in values.items():
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()