tiramisu/tests/test_choice_option.py

298 lines
10 KiB
Python
Raw Normal View History

2014-06-19 23:22:39 +02:00
# coding: utf-8
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2024-06-20 12:56:27 +02:00
from .config import config_type, get_config, value_list, global_owner, parse_od_get
2014-06-19 23:22:39 +02:00
2024-06-20 12:56:27 +02:00
from pytest import raises
2019-10-27 11:09:15 +01:00
from tiramisu import ChoiceOption, StrOption, OptionDescription, Config, owners, Calculation, \
undefined, Params, ParamValue, ParamOption
from tiramisu.error import ConfigError
2018-10-31 08:00:19 +01:00
2014-06-19 23:22:39 +02:00
def return_val(val):
return val
def return_list():
return ['val1', 'val2']
def return_calc_list(val):
return [val]
def return_error(*args, **kwargs):
2017-02-04 10:21:44 +01:00
raise Exception('test')
def test_choiceoption(config_type):
2018-03-19 08:33:53 +01:00
choice = ChoiceOption('choice', '', values=('val1', 'val2'))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
owner = global_owner(cfg, config_type)
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
assert not cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
#
assert value_list(cfg.option('choice').value.list()) == ('val1', 'val2')
# assert not list_sessions()
def test_choiceoption_function(config_type):
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_list))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
owner = global_owner(cfg, config_type)
assert cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
#
assert value_list(cfg.option('choice').value.list()) == ('val1', 'val2')
assert isinstance(cfg.option('choice').value.list(uncalculated=True), Calculation)
# assert not list_sessions()
2024-06-20 12:56:27 +02:00
def test_choiceoption_subfunction(config_type):
choice = ChoiceOption('choice', '', values=(Calculation(return_val, Params(ParamValue('val1'))), Calculation(return_val, Params(ParamValue('val2')))))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
owner = global_owner(cfg, config_type)
assert cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
#
assert value_list(cfg.option('choice').value.list()) == ('val1', 'val2')
# assert not list_sessions()
def test_choiceoption_function_error():
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_error))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
with raises(ConfigError):
cfg.option('choice').value.set('val1')
# assert not list_sessions()
2017-02-04 10:21:44 +01:00
def test_choiceoption_function_error_args():
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_error, Params(ParamValue('val1'))))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
with raises(ConfigError):
cfg.option('choice').value.set('val1')
# assert not list_sessions()
def test_choiceoption_function_error_kwargs():
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_error, Params(kwargs={'kwargs': ParamValue('val1')})))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
with raises(ConfigError):
cfg.option('choice').value.set('val1')
# assert not list_sessions()
def test_choiceoption_calc_function(config_type):
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', "", values=Calculation(return_calc_list, Params(ParamValue('val1'))))
od1 = OptionDescription('od', '', [choice])
cfg = Config(od1)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
owner = global_owner(cfg, config_type)
assert cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
# assert not list_sessions()
def test_choiceoption_calc_opt_function(config_type):
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
2019-10-27 11:09:15 +01:00
values=Calculation(return_calc_list, Params(ParamOption(str_))))
od1 = OptionDescription('od', '', [str_, choice])
cfg = Config(od1)
cfg.property.read_write()
owner = cfg.owner.get()
cfg = get_config(cfg, config_type)
assert cfg.option('choice').owner.isdefault()
#
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
# assert not list_sessions()
def test_choiceoption_calc_opt_function_propertyerror():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1', properties=('disabled',))
choice = ChoiceOption('choice',
"",
2019-10-27 11:09:15 +01:00
values=Calculation(return_calc_list, Params(ParamOption(str_))))
od1 = OptionDescription('od', '', [str_, choice])
cfg = Config(od1)
cfg.property.read_write()
with raises(ConfigError):
cfg.option('choice').value.set('no')
# assert not list_sessions()
2017-02-04 10:21:44 +01:00
2019-06-21 23:04:04 +02:00
#def test_choiceoption_calc_opt_multi_function(config_type):
def test_choiceoption_calc_opt_multi_function():
2019-06-21 23:04:04 +02:00
# FIXME
config_type = 'tiramisu'
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', ['val1'], multi=True)
choice = ChoiceOption('choice',
"",
default_multi='val2',
2019-10-27 11:09:15 +01:00
values=Calculation(return_val, Params(ParamOption(str_))),
2018-03-19 08:33:53 +01:00
multi=True)
ch2 = ChoiceOption('ch2',
"",
default=['val2'],
2019-10-27 11:09:15 +01:00
values=Calculation(return_val, Params(ParamOption(str_))),
2018-03-19 08:33:53 +01:00
multi=True)
od1 = OptionDescription('od', '', [str_, choice, ch2])
cfg = Config(od1)
cfg.property.read_write()
owner = cfg.owner.get()
cfg = get_config(cfg, config_type, True)
assert cfg.option('choice').owner.isdefault()
assert cfg.option('choice').value.get() == []
#
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
#
with raises(ValueError):
cfg.option('choice').value.set([undefined])
#
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('ch2').value.get()
# assert not list_sessions()
def test_choiceoption_calc_opt_multi_function_kwargs(config_type):
2018-09-15 22:44:49 +02:00
str_ = StrOption('str', '', ['val1'], multi=True)
choice = ChoiceOption('choice',
"",
default_multi='val2',
2019-10-27 11:09:15 +01:00
values=Calculation(return_val, Params(kwargs={'val': ParamOption(str_)})),
2018-09-15 22:44:49 +02:00
multi=True)
ch2 = ChoiceOption('ch2',
"",
default=['val2'],
2019-10-27 11:09:15 +01:00
values=Calculation(return_val, Params(kwargs={'val': ParamOption(str_)})),
2018-09-15 22:44:49 +02:00
multi=True)
od1 = OptionDescription('od', '', [str_, choice, ch2])
cfg = Config(od1)
cfg.property.read_write()
owner = cfg.owner.get()
# FIXME cfg = get_config(cfg, config_type)
assert cfg.option('choice').owner.isdefault()
assert cfg.option('choice').value.get() == []
#
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
#
with raises(ValueError):
cfg.option('choice').value.set([undefined])
#
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
#
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('choice').value.set('no')
assert cfg.option('choice').owner.isdefault()
#
with raises(ValueError):
cfg.option('ch2').value.get()
# assert not list_sessions()
def test_choiceoption_calc_not_list():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
default_multi='val2',
2019-10-27 11:09:15 +01:00
values=Calculation(return_val, Params(ParamOption(str_))),
2018-03-19 08:33:53 +01:00
multi=True)
od1 = OptionDescription('od', '', [str_, choice])
cfg = Config(od1)
cfg.property.read_write()
with raises(ConfigError):
cfg.option('choice').value.set(['val1'])
# assert not list_sessions()
2024-06-20 12:56:27 +02:00
def test_choiceoption_calc_default_value():
var1 = StrOption("var1", '', default="val1")
var2 = StrOption("var2", '', default="val2")
choice = ChoiceOption("choice", '', values=(Calculation(return_val, Params((ParamOption(var1)))), Calculation(return_val, Params((ParamOption(var2))))), default="val1")
od2 = OptionDescription("rougail", '', children=[var1, var2, choice])
od1 = OptionDescription("baseoption", "", children=[od2])
cfg = Config(od1)
assert parse_od_get(cfg.value.get()) == {'rougail.var1': 'val1', 'rougail.var2': 'val2', 'rougail.choice': 'val1'}