tiramisu/tests/test_choice_option.py

270 lines
9.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
2019-12-24 15:24:20 +01:00
import pytest
2018-03-19 08:33:53 +01:00
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2019-06-21 23:04:04 +02:00
from .config import config_type, get_config, value_list, global_owner
2014-06-19 23:22:39 +02:00
2019-10-27 11:09:15 +01:00
from tiramisu import ChoiceOption, StrOption, OptionDescription, Config, owners, Calculation, \
undefined, Params, ParamValue, ParamOption, list_sessions
from tiramisu.error import ConfigError
2018-10-31 08:00:19 +01:00
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')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_choiceoption(config_type):
2018-03-19 08:33:53 +01:00
choice = ChoiceOption('choice', '', values=('val1', 'val2'))
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
owner = await global_owner(cfg, config_type)
assert await cfg.option('choice').owner.get() == owners.default
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set('val1')
assert await cfg.option('choice').owner.get() == owner
assert not await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.get() == owners.default
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.get() == owners.default
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
assert value_list(await cfg.option('choice').value.list()) == ('val1', 'val2')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_choiceoption_function(config_type):
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_list))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
owner = await global_owner(cfg, config_type)
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set('val1')
assert await cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
assert value_list(await cfg.option('choice').value.list()) == ('val1', 'val2')
2014-06-19 23:22:39 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_choiceoption_function_error():
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_error))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
with pytest.raises(ConfigError):
await cfg.option('choice').value.set('val1')
2017-02-04 10:21:44 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_choiceoption_function_error_args():
2019-10-27 11:09:15 +01:00
choice = ChoiceOption('choice', '', values=Calculation(return_error, Params(ParamValue('val1'))))
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
with pytest.raises(ConfigError):
await cfg.option('choice').value.set('val1')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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')})))
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
with pytest.raises(ConfigError):
await cfg.option('choice').value.set('val1')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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'))))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
owner = await global_owner(cfg, config_type)
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set('val1')
assert await cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.isdefault()
2014-06-19 23:22:39 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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_))))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [str_, choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
owner = await cfg.owner.get()
cfg = await get_config(cfg, config_type)
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set('val1')
assert await cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.isdefault()
2014-06-19 23:22:39 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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_))))
2018-03-19 08:33:53 +01:00
odesc = OptionDescription('od', '', [str_, choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
with pytest.raises(ConfigError):
await cfg.option('choice').value.set('no')
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):
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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)
odesc = OptionDescription('od', '', [str_, choice, ch2])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
owner = await cfg.owner.get()
cfg = await get_config(cfg, config_type, True)
assert await cfg.option('choice').owner.isdefault()
assert await cfg.option('choice').value.get() == []
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set(['val1'])
assert await cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set([undefined])
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set(['val1'])
assert await cfg.option('choice').owner.get() == owner
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.isdefault()
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.isdefault()
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('ch2').value.get()
2018-09-15 22:44:49 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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)
odesc = OptionDescription('od', '', [str_, choice, ch2])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
owner = await cfg.owner.get()
# FIXME cfg = await get_config(cfg, config_type)
assert await cfg.option('choice').owner.isdefault()
assert await cfg.option('choice').value.get() == []
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set(['val1'])
assert await cfg.option('choice').owner.get() == owner
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set([undefined])
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.set(['val1'])
assert await cfg.option('choice').owner.get() == owner
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('choice').value.reset()
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('choice').value.set('no')
assert await cfg.option('choice').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('ch2').value.get()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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)
odesc = OptionDescription('od', '', [str_, choice])
2019-12-24 15:24:20 +01:00
cfg = await Config(odesc)
await cfg.property.read_write()
with pytest.raises(ConfigError):
await cfg.option('choice').value.set(['val1'])