rougail-user-data-yaml/tests/test_load.py

118 lines
4.1 KiB
Python
Raw Permalink Normal View History

2024-12-11 21:29:44 +01:00
import os
from pytest import fixture # , raises
from pathlib import Path
from rougail import Rougail
#########################
from rougail.user_data_yaml import RougailUserDataYaml as RougailUserData
from json import load, dump
from ruamel.yaml import YAML
#########################
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config, config_to_dict
EXT = "yml"
excludes = []
excludes = [
'40_0leadership_follower_default_submulti', # submulti is not allowed
'40_0leadership_follower_default_submulti_calculation', # submulti is not allowed
'40_6leadership_follower_multi', # submulti is not allowed
]
test_ok = get_structures_list(excludes)
# test_ok = [Path('../rougail-tests/structures/60_9extra_dynamic')]
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, *, level, need_exclude=False):
rougailconfig = get_rougail_config(test_dir, namespace)
if not rougailconfig:
return
##################################
rougailconfig['step.user_data'] = ['yaml']
##################################
dir_name = 'test'
if namespace:
dir_name += '_namespace'
elif (test_dir / 'force_namespace').is_file():
return
rougail = Rougail(rougailconfig)
config = rougail.run()
##################################
root_path = Path('tests') / 'results' / test_dir.name
makedict = root_path / 'makedict' / f'{level}.json'
filename = root_path / 'file'
if need_exclude:
filename = filename / f'{level}_exclude.{EXT}'
else:
filename = filename / f'{level}.{EXT}'
populate(filename, makedict, rougailconfig, level, need_exclude)
rougailconfig['yaml.filename'] = [str(filename)]
##################################
# loads variables in the tiramisu config
generated_user_data = RougailUserData(config, rougailconfig=rougailconfig).run()
errors = rougail.user_datas(generated_user_data)
#expected output
with open(Path('tests') / 'results' / test_dir.name / 'makedict' / f'{level}.json') as json_file:
expected = load(json_file)
# here is the effective test
errors_file = Path('tests') / 'results' / test_dir.name / 'errors' / f'{level}.json'
if not errors_file.is_file():
errors_file.parent.mkdir(parents=True, exist_ok=True)
with open(errors_file, 'a') as json_file:
dump(errors, json_file, indent=4)
with open(errors_file) as json_file:
expected_errors = load(json_file)
# expected_errors = {
# 'errors': [],
# 'warnings': [],
# }
assert expected_errors == errors
#
config.property.read_only()
config_dict = dict(config_to_dict(config.value.get()))
assert expected == config_dict
def populate(filename, makedict_file, rougailconfig, level, need_exclude):
if filename.is_file() and makedict_file.is_file():
return
config = Rougail(rougailconfig).run()
values = get_values_for_config(config, not need_exclude, level)
if not filename.is_file():
filename.parent.mkdir(parents=True, exist_ok=True)
with filename.open('w') as fh:
##############################################
dump(values, fh, indent=4)
##############################################
if not makedict_file.is_file():
makedict_file.parent.mkdir(parents=True, exist_ok=True)
config.property.read_only()
config_dict = dict(config_to_dict(config.value.get()))
with makedict_file.open('w') as fh:
dump(config_dict, fh, indent=4)
fh.write('\n')
def test_dictionaries_all(test_dir):
"tests the output"
_test_dictionaries(test_dir, True, EXT, level='all')
def test_dictionaries_all_exclude(test_dir):
"tests the output"
_test_dictionaries(test_dir, True, EXT, level='all', need_exclude=True)
def test_dictionaries_mandatories(test_dir):
"tests the output"
_test_dictionaries(test_dir, True, EXT, level='mandatories')