rougail/tests/test_flattener.py

123 lines
3.3 KiB
Python
Raw Normal View History

2019-11-23 08:17:35 +01:00
from lxml import etree
from os.path import isfile, join, isdir
from pytest import fixture, raises
from os import listdir
from json import load
2019-12-19 09:50:33 +01:00
from rougail import objspace, annotator
from rougail.xml_compare import xml_compare
from rougail.error import CreoleDictConsistencyError
2019-12-19 09:50:33 +01:00
from rougail.config import dtdfilename
2020-07-06 20:58:11 +02:00
from rougail.annotator import VARIABLE_NAMESPACE
2019-11-23 08:17:35 +01:00
destfile = '/tmp/test.xml'
2019-11-26 20:33:24 +01:00
dico_dirs = 'tests/flattener_dicos'
2019-11-23 08:17:35 +01:00
test_ok = set()
test_raise = set()
for test in listdir(dico_dirs):
if isdir(join(dico_dirs, test)):
if isdir(join(dico_dirs, test, 'result')):
test_ok.add(test)
else:
test_raise.add(test)
excludes = set([])
test_ok -= excludes
test_raise -= excludes
2020-07-08 11:14:47 +02:00
#test_ok = ['10check_valid_differ_removecheck']
2020-07-07 19:36:26 +02:00
#test_raise = []
2019-11-23 08:17:35 +01:00
test_ok = list(test_ok)
test_raise = list(test_raise)
test_ok.sort()
test_raise.sort()
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
@fixture(scope="module", params=test_raise)
def test_dir_error(request):
return request.param
def compare_xml(exported_xmlfile, expected_xmlfile):
exported_document = etree.parse(exported_xmlfile).getroot()
expected_document = etree.parse(expected_xmlfile).getroot()
try:
assert xml_compare(exported_document, expected_document)
except AssertionError as err:
print()
print('Le dictionnaire exporte :')
print()
2019-11-24 20:25:09 +01:00
print(etree.tostring(exported_document, pretty_print=True, encoding="UTF-8").decode())
2019-11-23 08:17:35 +01:00
print()
print('Le dictionnaire attendu :')
print()
2019-11-24 20:25:09 +01:00
print(etree.tostring(expected_document, pretty_print=True, encoding="UTF-8").decode())
2019-11-23 08:17:35 +01:00
raise err
2020-04-21 09:22:45 +02:00
def launch_flattener(test_dir, test_ok=False):
2019-11-26 20:33:24 +01:00
eolobj = objspace.CreoleObjSpace(dtdfilename)
2019-11-23 08:17:35 +01:00
dirs = [test_dir]
subfolder = join(test_dir, 'subfolder')
if isdir(subfolder):
dirs.append(subfolder)
2020-07-06 20:58:11 +02:00
eolobj.create_or_populate_from_xml(VARIABLE_NAMESPACE, dirs)
2019-11-23 08:17:35 +01:00
subfolder = join(test_dir, 'extra_dirs', 'extra')
if isdir(subfolder):
eolobj.create_or_populate_from_xml('extra', [subfolder])
subfolder = join(test_dir, 'extra_dirs', 'extra1')
if isdir(subfolder):
eolobj.create_or_populate_from_xml('extra1', [subfolder])
2019-11-24 20:25:09 +01:00
eosfunc = join(dico_dirs, '../eosfunc/test.py')
eolobj.space_visitor(eosfunc)
2019-11-23 08:17:35 +01:00
eolobj.save(destfile)
result_file = join(test_dir, 'result/00-base.xml')
if isfile(result_file):
2020-07-07 18:33:19 +02:00
#eolobj.save(result_file)
2019-11-23 08:17:35 +01:00
compare_xml(destfile, result_file)
2020-04-21 09:22:45 +02:00
elif test_ok:
raise Exception(f'no test found for {test_dir}')
2019-11-23 08:17:35 +01:00
def fake_traduc(txt):
return txt
def setup_module(module):
module.traduc_ori = objspace._
objspace._ = fake_traduc
annotator._ = fake_traduc
2020-02-14 17:59:39 +01:00
objspace.ServiceAnnotator = getattr(annotator, 'ServiceAnnotator')
2019-11-23 08:17:35 +01:00
def teardown_module(module):
objspace._ = module.traduc_ori
annotator._ = module.traduc_ori
def test_dictionary(test_dir):
test_dir = join(dico_dirs, test_dir)
2020-04-21 09:22:45 +02:00
launch_flattener(test_dir, True)
2019-11-23 08:17:35 +01:00
def test_error_dictionary(test_dir_error):
test_dir = join(dico_dirs, test_dir_error)
2020-02-14 17:59:39 +01:00
with raises(CreoleDictConsistencyError):
launch_flattener(test_dir)
2019-11-23 08:17:35 +01:00
def test_no_dtd():
2020-02-14 17:59:39 +01:00
with raises(IOError):
eolobj = objspace.CreoleObjSpace('notexists.dtd')