feat: auto create tested

This commit is contained in:
egarette@silique.fr 2024-09-30 14:13:42 +02:00
parent ca6b933453
commit 76a4f18bec

View file

@ -36,6 +36,9 @@ def _test_dictionaries(test_dir):
def test_dictionaries_warning(test_dir):
"tests the '00_6string' folder"
rougailconfig = _test_dictionaries(test_dir)
# populate tests if not already exists
dest_dir = Path('tests') / 'envvars' / test_dir.name
populate(dest_dir, rougailconfig)
# loads the config in the tiramisu's meaning
config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"])
config_dict = dict(config.value.get())
@ -43,7 +46,7 @@ def test_dictionaries_warning(test_dir):
'warnings': [],
}
# loading the env file
envfile = Path('tests') / 'envvars' / env_test_folder / 'env'
envfile = dest_dir / 'env'
load_dotenv(envfile)
# loads the environment variables in the tiramisu config
environment = RougailUserDataEnvironment(config,
@ -59,3 +62,82 @@ def test_dictionaries_warning(test_dir):
# here is the effective test
for key, value in new_config_dict.items():
assert expected["rougail."+key.name()] == value
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'
if not environment_file.is_file() or not makedict.is_file():
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')
if not makedict.is_file():
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