tiramisu/test/test_choice_option.py

255 lines
8.8 KiB
Python
Raw Normal View History

2014-06-19 23:22:39 +02:00
# coding: utf-8
2018-03-19 08:33:53 +01:00
from py.test import raises
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2014-06-19 23:22:39 +02:00
from tiramisu.setting import owners
from tiramisu.option import ChoiceOption, StrOption, OptionDescription
2018-08-14 22:15:40 +02:00
from tiramisu import Config
from tiramisu.error import ConfigError
2018-08-15 08:35:22 +02:00
from tiramisu import undefined, Params, ParamValue, ParamOption
2018-03-19 08:33:53 +01:00
from tiramisu.api import TIRAMISU_VERSION
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
def teardown_function(function):
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
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():
2018-03-19 08:33:53 +01:00
choice = ChoiceOption('choice', '', values=('val1', 'val2'))
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
owner = cfg.owner.get()
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
assert not cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.get() == owners.default
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
assert cfg.option('choice').value.list() == ('val1', 'val2')
2014-06-19 23:22:39 +02:00
def test_choiceoption_function():
2018-03-19 08:33:53 +01:00
choice = ChoiceOption('choice', '', values=return_list)
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
owner = cfg.owner.get()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
assert cfg.option('choice').value.list() == ['val1', 'val2']
2014-06-19 23:22:39 +02:00
2017-02-04 10:21:44 +01:00
def test_choiceoption_function_error():
2018-03-19 08:33:53 +01:00
choice = ChoiceOption('choice', '', values=return_error)
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
raises(ConfigError, "cfg.option('choice').value.set('val1')")
2017-02-04 10:21:44 +01:00
def test_choiceoption_function_error_args():
choice = ChoiceOption('choice', '', values=return_error, values_params=Params((ParamValue('val1'),)))
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
raises(ConfigError, "cfg.option('choice').value.set('val1')")
def test_choiceoption_function_error_kwargs():
choice = ChoiceOption('choice', '', values=return_error, values_params=Params(kwargs={'kwargs': ParamValue('val1')}))
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
raises(ConfigError, "cfg.option('choice').value.set('val1')")
2014-06-19 23:22:39 +02:00
def test_choiceoption_calc_function():
choice = ChoiceOption('choice', "", values=return_calc_list, values_params=Params((ParamValue('val1'),)))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
owner = cfg.owner.get()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.isdefault()
2014-06-19 23:22:39 +02:00
def test_choiceoption_calc_opt_function():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
values=return_calc_list,
values_params=Params((ParamOption(str_),)))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
owner = cfg.owner.get()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set('val1')
assert cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.isdefault()
2014-06-19 23:22:39 +02:00
2017-02-04 10:21:44 +01:00
def test_choiceoption_calc_opt_function_propertyerror():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1', properties=('disabled',))
choice = ChoiceOption('choice',
"",
values=return_calc_list,
values_params=Params((ParamOption(str_),)))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
2018-03-19 08:33:53 +01:00
if TIRAMISU_VERSION == 2:
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
2018-03-19 08:33:53 +01:00
else:
2018-08-15 08:35:22 +02:00
raises(ConfigError, "cfg.option('choice').value.set('no')")
2017-02-04 10:21:44 +01:00
2014-06-19 23:22:39 +02:00
def test_choiceoption_calc_opt_multi_function():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', ['val1'], multi=True)
choice = ChoiceOption('choice',
"",
default_multi='val2',
values=return_val,
values_params=Params((ParamOption(str_),)),
2018-03-19 08:33:53 +01:00
multi=True)
ch2 = ChoiceOption('ch2',
"",
default=['val2'],
values=return_val,
values_params=Params((ParamOption(str_),)),
2018-03-19 08:33:53 +01:00
multi=True)
odesc = OptionDescription('od', '', [str_, choice, ch2])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
owner = cfg.owner.get()
assert cfg.option('choice').owner.isdefault()
assert cfg.option('choice').value.get() == []
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set([undefined])")
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.set(['val1'])
assert cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
cfg.option('choice').value.reset()
assert cfg.option('choice').owner.isdefault()
2018-09-15 22:44:49 +02:00
#
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.isdefault()
#
raises(ValueError, "cfg.option('ch2').value.get()")
def test_choiceoption_calc_opt_multi_function_kwargs():
str_ = StrOption('str', '', ['val1'], multi=True)
choice = ChoiceOption('choice',
"",
default_multi='val2',
values=return_val,
values_params=Params(kwargs={'val': ParamOption(str_)}),
multi=True)
ch2 = ChoiceOption('ch2',
"",
default=['val2'],
values=return_val,
values_params=Params(kwargs={'val': ParamOption(str_)}),
multi=True)
odesc = OptionDescription('od', '', [str_, choice, ch2])
cfg = Config(odesc)
cfg.property.read_write()
owner = cfg.owner.get()
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
#
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()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('choice').value.set('no')")
assert cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2018-08-15 08:35:22 +02:00
raises(ValueError, "cfg.option('ch2').value.get()")
def test_choiceoption_calc_invalid():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', ['val1'], multi=True)
str_
raises(ValueError,
"choice = ChoiceOption('choice', '', default_multi='val2', values=[1, 2, 3], \
values_params=Params((ParamOption(str_),)), multi=True)")
def test_choiceoption_calc_not_list():
2018-03-19 08:33:53 +01:00
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
default_multi='val2',
values=return_val,
values_params=Params((ParamOption(str_),)),
2018-03-19 08:33:53 +01:00
multi=True)
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
2018-08-15 08:35:22 +02:00
cfg.property.read_write()
raises(ConfigError, "cfg.option('choice').value.set(['val1'])")