tiramisu/test/test_choice_option.py

222 lines
7.3 KiB
Python

# coding: utf-8
from py.test import raises
from .autopath import do_autopath
do_autopath()
from tiramisu.setting import owners
from tiramisu.option import ChoiceOption, StrOption, OptionDescription
from tiramisu.config import Config
from tiramisu.error import ConfigError
from tiramisu import getapi, undefined
from tiramisu.api import TIRAMISU_VERSION
def return_val(val):
return val
def return_list():
return ['val1', 'val2']
def return_calc_list(val):
return [val]
def return_error(*args, **kwargs):
raise Exception('test')
def test_choiceoption():
choice = ChoiceOption('choice', '', values=('val1', 'val2'))
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
owner = api.owner.get()
assert api.option('choice').owner.get() == owners.default
assert api.option('choice').owner.isdefault()
#
api.option('choice').value.set('val1')
assert api.option('choice').owner.get() == owner
assert not api.option('choice').owner.isdefault()
#
api.option('choice').value.reset()
assert api.option('choice').owner.get() == owners.default
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('choice').value.set('no')")
assert api.option('choice').owner.get() == owners.default
assert api.option('choice').owner.isdefault()
#
assert api.option('choice').value.list() == ('val1', 'val2')
def test_choiceoption_function():
choice = ChoiceOption('choice', '', values=return_list)
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
owner = api.owner.get()
assert api.option('choice').owner.isdefault()
#
api.option('choice').value.set('val1')
assert api.option('choice').owner.get() == owner
#
api.option('choice').value.reset()
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('choice').value.set('no')")
assert api.option('choice').owner.isdefault()
#
assert api.option('choice').value.list() == ['val1', 'val2']
def test_choiceoption_function_error():
choice = ChoiceOption('choice', '', values=return_error)
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
raises(ConfigError, "api.option('choice').value.set('val1')")
def test_choiceoption_function_error_args():
choice = ChoiceOption('choice', '', values=return_error, values_params={'': ('val1',)})
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
raises(ConfigError, "api.option('choice').value.set('val1')")
def test_choiceoption_function_error_kwargs():
choice = ChoiceOption('choice', '', values=return_error, values_params={'kwargs': ('val1',)})
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
raises(ConfigError, "api.option('choice').value.set('val1')")
def test_choiceoption_calc_function():
choice = ChoiceOption('choice', "", values=return_calc_list, values_params={'': ('val1',)})
odesc = OptionDescription('od', '', [choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
owner = api.owner.get()
assert api.option('choice').owner.isdefault()
#
api.option('choice').value.set('val1')
assert api.option('choice').owner.get() == owner
#
api.option('choice').value.reset()
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('choice').value.set('no')")
assert api.option('choice').owner.isdefault()
def test_choiceoption_calc_opt_function():
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
values=return_calc_list,
values_params={'': ((str_, False),)})
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
owner = api.owner.get()
assert api.option('choice').owner.isdefault()
#
api.option('choice').value.set('val1')
assert api.option('choice').owner.get() == owner
#
api.option('choice').value.reset()
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('choice').value.set('no')")
assert api.option('choice').owner.isdefault()
def test_choiceoption_calc_opt_function_propertyerror():
str_ = StrOption('str', '', 'val1', properties=('disabled',))
choice = ChoiceOption('choice',
"",
values=return_calc_list,
values_params={'': ((str_, False),)})
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
if TIRAMISU_VERSION == 2:
raises(ValueError, "api.option('choice').value.set('no')")
else:
raises(ConfigError, "api.option('choice').value.set('no')")
def test_choiceoption_calc_opt_multi_function():
str_ = StrOption('str', '', ['val1'], multi=True)
choice = ChoiceOption('choice',
"",
default_multi='val2',
values=return_val,
values_params={'': ((str_, False),)},
multi=True)
ch2 = ChoiceOption('ch2',
"",
default=['val2'],
values=return_val,
values_params={'': ((str_, False),)},
multi=True)
odesc = OptionDescription('od', '', [str_, choice, ch2])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
owner = api.owner.get()
assert api.option('choice').owner.isdefault()
assert api.option('choice').value.get() == []
#
api.option('choice').value.set(['val1'])
assert api.option('choice').owner.get() == owner
#
raises(ValueError, "api.option('choice').value.set([undefined])")
#
api.option('choice').value.set(['val1'])
assert api.option('choice').owner.get() == owner
#
api.option('choice').value.reset()
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('choice').value.set('no')")
assert api.option('choice').owner.isdefault()
#
raises(ValueError, "api.option('ch2').value.get()")
def test_choiceoption_calc_invalid():
str_ = StrOption('str', '', ['val1'], multi=True)
str_
raises(ValueError,
"choice = ChoiceOption('choice', '', default_multi='val2', values=[1, 2, 3], \
values_params={'': ((str_, False),)}, multi=True)")
def test_choiceoption_calc_not_list():
str_ = StrOption('str', '', 'val1')
choice = ChoiceOption('choice',
"",
default_multi='val2',
values=return_val,
values_params={'': ((str_, False),)},
multi=True)
odesc = OptionDescription('od', '', [str_, choice])
cfg = Config(odesc)
api = getapi(cfg)
api.property.read_write()
raises(ConfigError, "api.option('choice').value.set(['val1'])")