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

160 lines
5.6 KiB
Python
Raw Normal View History

2024-09-23 14:34:15 +02:00
from .custom import CustomOption
2024-09-18 11:43:22 +02:00
from pathlib import Path
2024-09-30 16:40:24 +02:00
from os import getcwd, listdir, environ, makedirs
from os.path import isfile, join, isdir, dirname
from pathlib import Path
2024-09-25 08:52:13 +02:00
import json
2024-09-30 15:23:47 +02:00
2024-09-30 16:40:24 +02:00
from pytest import fixture, raises
2024-09-18 11:43:22 +02:00
from ruamel.yaml import YAML
2024-09-24 16:42:03 +02:00
from dotenv import load_dotenv
2024-09-18 11:43:22 +02:00
2024-09-23 14:34:15 +02:00
from rougail import RougailConfig, Rougail
2024-09-24 16:42:03 +02:00
from rougail.user_data_environment.data import RougailUserDataEnvironment
2024-09-18 11:43:22 +02:00
dico_dirs = Path('../rougail/tests/dictionaries')
2024-09-30 16:40:24 +02:00
#env_test_folder = '00_6string'
# path configuration
_here = Path(__file__).resolve().parent
envtest = _here / 'tests' / 'envvars'
test_ok = set()
test_raise = set()
for test in listdir(dico_dirs):
if isdir(join(envtest, test)):
test_ok.add(test)
elif test != '__pycache__':
test_raise.add(test)
#test_ok = [dico_dirs / env_test_folder]
excludes = set()
test_ok -= excludes
2024-09-18 11:43:22 +02:00
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
2024-09-23 14:34:15 +02:00
def _test_dictionaries(test_dir):
2024-09-25 08:52:13 +02:00
"rougail config settings"
2024-09-18 11:43:22 +02:00
rougailconfig = RougailConfig.copy()
2024-09-24 16:42:03 +02:00
rougailconfig['step.user_data'] = ['environment']
rougailconfig['main_namespace'] = None
2024-09-18 11:43:22 +02:00
dirs = [str(test_dir / 'dictionaries' / 'rougail')]
rougailconfig['custom_types']['custom'] = CustomOption
2024-09-23 14:34:15 +02:00
rougailconfig['main_dictionaries'] = dirs
return rougailconfig
def test_dictionaries_warning(test_dir):
2024-09-25 08:52:13 +02:00
"tests the '00_6string' folder"
2024-09-23 14:34:15 +02:00
rougailconfig = _test_dictionaries(test_dir)
2024-09-30 14:13:42 +02:00
# populate tests if not already exists
dest_dir = Path('tests') / 'envvars' / test_dir.name
populate(dest_dir, rougailconfig)
2024-09-23 14:34:15 +02:00
# loads the config in the tiramisu's meaning
config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"])
2024-09-24 16:42:03 +02:00
config_dict = dict(config.value.get())
user_datas = {'errors': [],
'warnings': [],
}
# loading the env file
2024-09-30 15:23:47 +02:00
envfile = dest_dir / 'env'/ 'all.env'
2024-09-24 16:42:03 +02:00
load_dotenv(envfile)
2024-09-25 08:52:13 +02:00
# loads the environment variables in the tiramisu config
2024-09-24 16:42:03 +02:00
environment = RougailUserDataEnvironment(config,
rougailconfig=rougailconfig,
user_datas=user_datas,
)
environment.run()
new_config = environment.config
new_config_dict = dict(new_config.value.get())
2024-09-25 08:52:13 +02:00
# expected output
with open(Path('tests') / 'makedict' / env_test_folder / 'output.json') as json_file:
expected = json.load(json_file)
# here is the effective test
for key, value in new_config_dict.items():
assert expected["rougail."+key.name()] == value
2024-09-30 14:13:42 +02:00
def populate(dest_dir, rougailconfig):
for level in ['all', 'mandatories']:
environment_file = dest_dir / 'env' / f'{level}.env'
makedict_file = dest_dir / 'makedict' / f'{level}.env'
2024-09-30 15:23:47 +02:00
if not environment_file.is_file() or not makedict_file.is_file():
2024-09-30 14:13:42 +02:00
config = Rougail(rougailconfig).run()
if level == 'all':
root_config = config.unrestraint
else:
root_config = config.unrestraint.value.mandatory()
values = ['ROUGAIL_' + variable.path().upper() + '="' + get_value(variable) + '"' for variable in get_variables(root_config)]
if not environment_file.is_file():
with environment_file.open('w') as envfh:
envfh.write('\n'.join(values) + '\n')
2024-09-30 15:23:47 +02:00
if not makedict_file.is_file():
2024-09-30 14:13:42 +02:00
with environment_file.open('w') as envfh:
envfh.write('\n'.join(values) + '\n')
def get_value(variable):
tests = variable.information.get('test', None)
if not tests:
if variable.type() == 'integer':
tests = [1, 2, 3]
elif variable.type() == 'float':
tests = [1.1, 2.2, 3.3]
elif variable.type() == 'port':
tests = ['80', '443']
elif variable.type() == 'boolean':
tests = [True]
elif variable.type() == 'domain name':
tests = ['domain1.lan', 'domain2.lan']
elif variable.type() == 'choice':
tests = variable.value.list()
else:
tests = ['string1', 'string2', 'string3']
if not variable.ismulti() or (variable.isfollower() and variable.ismulti() is True):
tests = tests[0]
variable.value.set(tests)
if isinstance(tests, list):
tests = ','.join([str(test) for test in tests])
else:
tests = str(tests)
return tests
def get_variables(config):
for key in config:
if key.isoptiondescription():
yield from get_variables(key)
else:
yield key
def option_value(parent, key_is_option=False):
for option, value in parent.items():
if option.isoptiondescription():
if not key_is_option and option.isleadership():
ret = []
for idx, datas in enumerate(option_value(value, key_is_option=True)):
sub_option, sub_value = datas
if not idx:
sub_option = sub_option.path()
key = sub_option
for val in sub_value:
ret.append({sub_option: val})
else:
index = sub_option.index()
sub_option = sub_option.path()
ret[index][sub_option] = sub_value
yield key, ret
else:
yield from option_value(value, key_is_option)
elif key_is_option:
yield option, value
else:
yield option.path(), value