rougail-output-json/tests/test_load.py
2024-11-30 15:50:28 +01:00

86 lines
2.6 KiB
Python

from pytest import fixture # , raises
from pathlib import Path
from rougail import Rougail
from rougail.output_json import RougailOutputJson as RougailOutput
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config
EXT = 'json'
excludes = []
#excludes = set([
# '60_5family_dynamic_unknown_suffix',
# '60_5family_dynamic_variable_outside_sub_suffix',
#])
test_ok = get_structures_list(excludes)
# test_ok = ['60_0family_dynamic_variable_suffix']
def idfn(fixture_value):
return fixture_value.name
@fixture(scope="module", params=test_ok, ids=idfn)
def test_dir(request):
return request.param
def _test_dictionaries(test_dir, namespace, ext, *, read_write=True, mandatory=False):
rougailconfig = get_rougail_config(test_dir, namespace)
if not rougailconfig:
return
##################################
rougailconfig['step.output'] = 'json'
rougailconfig["json.read_write"] = read_write
rougailconfig["json.mandatory"] = mandatory
##################################
dir_name = 'test'
if namespace:
dir_name += '_namespace'
elif (test_dir / 'force_namespace').is_file():
return
if read_write:
dir_name += '_read_write'
if mandatory:
dir_name += '_mandatory'
rougail = Rougail(rougailconfig)
config = rougail.run()
if mandatory or not read_write:
get_values_for_config(config)
generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()
output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + EXT)
if not output_file.is_file():
if not output_file.parent.is_dir():
output_file.parent.mkdir()
with output_file.open('w') as outfh:
outfh.write(generated_output)
with output_file.open() as outfh:
attented_output = outfh.read()
assert generated_output == attented_output
def test_dictionaries_json(test_dir):
_test_dictionaries(test_dir, True, EXT)
def test_dictionaries_json_mandatory(test_dir):
_test_dictionaries(test_dir, True, EXT, mandatory=True)
def test_dictionaries_json_mandatory_read_only(test_dir):
_test_dictionaries(test_dir, True, EXT, read_write=False, mandatory=True)
def test_dictionaries_json_namespace(test_dir):
_test_dictionaries(test_dir, False, EXT)
def test_dictionaries_json_namespace_mandatory(test_dir):
_test_dictionaries(test_dir, False, EXT, mandatory=True)
def test_dictionaries_json_namespace_mandatory_read_only(test_dir):
_test_dictionaries(test_dir, False, EXT, read_write=False, mandatory=True)