rougail/tests/test_2_makedict.py
2020-11-20 18:02:40 +01:00

68 lines
1.7 KiB
Python

from lxml import etree
from os.path import isfile, join, isdir
from pytest import fixture, mark
from os import listdir, mkdir
from json import dump, load, dumps, loads
from tiramisu import Config
dico_dirs = 'tests/dictionaries'
test_ok = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
if isdir(join(dico_dirs, test, 'tiramisu')):
test_ok.add(test)
debug = False
#debug = True
excludes = set([])
#excludes = set(['01base_file_utfchar'])
test_ok -= excludes
#test_ok = ['10check_valid_ipnetmask']
test_ok = list(test_ok)
test_ok.sort()
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
async def launch_flattener(test_dir):
makedict_dir = join(test_dir, 'makedict')
makedict_file = join(makedict_dir, 'base.json')
modulepath = test_dir.replace('/', '.') + '.tiramisu.base'
mod = __import__(modulepath)
for token in modulepath.split(".")[1:]:
mod = getattr(mod, token)
config = await Config(mod.option_0)
await config.property.read_only()
await config.property.pop('mandatory')
await config.information.set('info', 'value')
config_dict = await config.value.dict()
if config_dict:
if not isdir(makedict_dir):
mkdir(makedict_dir)
if debug:
with open(makedict_file, 'w') as fh:
dump(config_dict, fh)
fh.write('\n')
if not isfile(makedict_file):
if config_dict:
raise Exception('dict is not empty')
else:
with open(makedict_file, 'r') as fh:
assert load(fh) == loads(dumps(config_dict))
@mark.asyncio
async def test_dictionary(test_dir):
test_dir = join(dico_dirs, test_dir)
await launch_flattener(test_dir)