rougail/tests/test_makedict.py

72 lines
2.0 KiB
Python
Raw Normal View History

2019-11-26 20:33:24 +01:00
from lxml import etree
from os.path import isfile, join, isdir
2020-02-14 17:59:39 +01:00
from pytest import fixture, mark
2019-11-26 20:33:24 +01:00
from os import listdir, mkdir
from json import dump, load, dumps, loads
2019-12-02 10:31:55 +01:00
from tiramisu import Config
from rougail import load as rougail_load
from rougail.xml_compare import xml_compare
from rougail.error import CreoleDictConsistencyError
from rougail.config import dtdfilename
2019-11-26 20:33:24 +01:00
dico_dirs = 'tests/flattener_dicos'
test_ok = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
if isdir(join(dico_dirs, test, 'result')):
test_ok.add(test)
excludes = set([])
2020-02-14 17:59:39 +01:00
#excludes = set(['70container_services'])
2019-11-26 20:33:24 +01:00
test_ok -= excludes
2020-07-16 09:50:01 +02:00
#test_ok = ['10check_valid_ipnetmask']
2019-11-26 20:33:24 +01:00
test_ok = list(test_ok)
test_ok.sort()
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
2020-02-14 17:59:39 +01:00
async def launch_flattener(test_dir):
2019-11-26 20:33:24 +01:00
eosfunc = join(dico_dirs, '../eosfunc/test.py')
cache_file = test_dir + '/result/00-base.xml'
fileio = open(cache_file)
xmlroot = etree.parse(fileio).getroot()
2019-12-02 10:31:55 +01:00
tiramisu_objects = rougail_load(xmlroot,
dtdfilename,
eosfunc)
2019-11-26 20:33:24 +01:00
makedict_dir = join(test_dir, 'makedict')
makedict_file = join(makedict_dir, 'base.json')
2020-02-14 17:59:39 +01:00
config = await Config(tiramisu_objects)
2020-07-06 19:47:45 +02:00
await config.property.read_only()
await config.property.pop('mandatory')
2020-02-14 17:59:39 +01:00
config_dict = await config.value.dict()
2019-11-26 20:33:24 +01:00
# if not isfile(makedict_file) and config_dict:
if config_dict:
if not isdir(makedict_dir):
mkdir(makedict_dir)
2020-07-07 18:12:16 +02:00
#with open(makedict_file, 'w') as fh:
# dump(config_dict, fh)
# fh.write('\n')
2019-11-26 20:33:24 +01:00
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))
2020-02-14 17:59:39 +01:00
@mark.asyncio
async def test_dictionary(test_dir):
2019-11-26 20:33:24 +01:00
test_dir = join(dico_dirs, test_dir)
2020-02-14 17:59:39 +01:00
await launch_flattener(test_dir)