tiramisu/tests/test_option.py

355 lines
12 KiB
Python
Raw Permalink Normal View History

2013-08-22 12:17:10 +02:00
"""these tests are here to create some :class:`tiramisu.option.Option`'s
and to compare them
"""
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2019-12-24 15:24:20 +01:00
import pytest
import warnings
2023-04-27 11:44:52 +02:00
from tiramisu.error import ConfigError, ValueWarning
from tiramisu import IntOption, SymLinkOption, OptionDescription, Config, Calculation, groups
from tiramisu.i18n import _
2018-10-31 08:00:19 +01:00
try:
groups.family
except:
2023-05-11 15:44:48 +02:00
groups.addgroup('family')
2018-10-31 08:00:19 +01:00
2013-12-09 18:56:29 +01:00
def a_func():
return None
2013-05-05 21:43:19 +02:00
2023-05-11 15:44:48 +02:00
def display_name(*args):
return 'display_name'
def test_option_valid_name():
2013-12-09 17:55:52 +01:00
IntOption('test', '')
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
IntOption(1, "")
2019-02-11 20:28:03 +01:00
i = IntOption("test1", "")
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
SymLinkOption(1, i)
2019-02-11 20:28:03 +01:00
i = SymLinkOption("test1", i)
def test_option_get_information():
2017-02-03 23:39:24 +01:00
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2017-02-03 23:39:24 +01:00
i.impl_set_information('info', string)
assert i.impl_get_information('info') == string
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2017-02-03 23:39:24 +01:00
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
def test_option_get_information_config():
2017-02-03 23:39:24 +01:00
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
od = OptionDescription('od', '', [i])
cfg = Config(od)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
i.impl_set_information('info', string)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2017-02-03 23:39:24 +01:00
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
# assert not list_sessions()
def test_option_unknown():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
i.impl_set_information('noinfo', 'optdefault')
od = OptionDescription('od', '', [i])
cfg = Config(od)
#
2023-04-27 11:44:52 +02:00
with pytest.raises(ConfigError):
cfg.option('test').unknown.get()
2023-04-27 11:44:52 +02:00
with pytest.raises(ConfigError):
# only choice
cfg.option('test').value.list()
2017-02-03 23:39:24 +01:00
2023-05-11 15:44:48 +02:00
def test_option_description():
description = "it's ok"
i = IntOption('test', description)
od = OptionDescription('od', 'od', [i])
od2 = OptionDescription('od', '', [od])
cfg = Config(od2)
assert cfg.option('od').description() == 'od'
assert cfg.option('od.test').description() == description
2023-05-11 15:44:48 +02:00
def test_option_get_information_default():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
i.impl_set_information('noinfo', 'optdefault')
od = OptionDescription('od', '', [i])
cfg = Config(od)
#
assert cfg.option('test').information.get('noinfo', 'falsedefault') == 'optdefault'
#
cfg.option('test').information.set('noinfo', 'notdefault')
assert cfg.option('test').information.get('noinfo', 'falsedefault') == 'notdefault'
# assert not list_sessions()
def test_option_get_information_config2():
2017-02-03 23:39:24 +01:00
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
i.impl_set_information('info', string)
od = OptionDescription('od', '', [i])
cfg = Config(od)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
i.impl_set_information('info', 'hello')
2017-02-03 23:39:24 +01:00
assert i.impl_get_information('info') == string
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
i.impl_get_information('noinfo')
2017-02-03 23:39:24 +01:00
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
# assert not list_sessions()
2017-02-03 23:39:24 +01:00
def test_optiondescription_get_information():
2017-02-03 23:39:24 +01:00
description = "it's ok"
string = 'some informations'
o = OptionDescription('test', description, [])
o.impl_set_information('info', string)
assert o.impl_get_information('info') == string
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
o.impl_get_information('noinfo')
2017-02-03 23:39:24 +01:00
assert o.impl_get_information('noinfo', 'default') == 'default'
assert o.impl_get_information('doc') == description
# assert not list_sessions()
2013-12-09 18:56:29 +01:00
2013-12-09 17:55:52 +01:00
def test_option_isoptiondescription():
2018-09-22 17:45:52 +02:00
i = IntOption('test', '')
od = OptionDescription('od', '', [i])
od = OptionDescription('od', '', [od])
cfg = Config(od)
assert cfg.option('od').isoptiondescription()
assert not cfg.option('od.test').isoptiondescription()
# assert not list_sessions()
2018-09-22 17:45:52 +02:00
def test_option_double():
2019-03-02 21:31:21 +01:00
i = IntOption('test', '')
od = OptionDescription('od1', '', [i])
od = OptionDescription('od2', '', [od])
od = OptionDescription('od3', '', [od])
cfg = Config(od)
assert cfg.option('od2.od1.test').value.get() is None
assert cfg.option('od2').option('od1').option('test').value.get() is None
# assert not list_sessions()
2019-03-02 21:31:21 +01:00
def test_option_multi():
2013-12-09 17:55:52 +01:00
IntOption('test', '', multi=True)
IntOption('test', '', multi=True, default_multi=1)
IntOption('test', '', default=[1], multi=True, default_multi=1)
2013-12-09 18:56:29 +01:00
#add default_multi to not multi's option
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
IntOption('test', '', default_multi=1)
2013-12-09 18:56:29 +01:00
#unvalid default_multi
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
IntOption('test', '', multi=True, default_multi='yes')
# assert not list_sessions()
2019-09-28 16:32:48 +02:00
2018-04-06 23:51:25 +02:00
def test_unknown_option():
2018-04-06 23:51:25 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
cfg = Config(od2)
# test is an option, not an optiondescription
with pytest.raises(TypeError):
cfg.option('od.test.unknown').value.get()
# unknown is an unknown option
with pytest.raises(AttributeError):
cfg.option('unknown').value.get()
# unknown is an unknown option
with pytest.raises(AttributeError):
cfg.option('od.unknown').value.get()
# unknown is an unknown optiondescription
with pytest.raises(AttributeError):
cfg.option('od.unknown.suboption').value.get()
# assert not list_sessions()
def test_optiondescription_list():
2023-05-11 15:44:48 +02:00
groups.addgroup('notfamily1')
2018-09-29 20:05:28 +02:00
i = IntOption('test', '')
i2 = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od1.impl_set_group_type(groups.family)
od3 = OptionDescription('od2', '', [i2])
od3.impl_set_group_type(groups.notfamily1)
od2 = OptionDescription('od', '', [od1, od3])
od4 = OptionDescription('od', '', [od2])
cfg = Config(od4)
assert len(list(cfg.option('od').list('option'))) == 0
assert len(list(cfg.option('od').list('optiondescription'))) == 2
assert len(list(cfg.option('od').list('optiondescription', group_type=groups.family))) == 1
assert len(list(cfg.option('od').list('optiondescription', group_type=groups.notfamily1))) == 1
assert len(list(cfg.option('od.od').list('option'))) == 1
assert len(list(cfg.option('od.od2').list('option'))) == 1
try:
list(cfg.option('od').list('unknown'))
except AssertionError:
pass
else:
raise Exception('must raise')
try:
list(cfg.option('od').list('option', group_type='toto'))
except AssertionError:
pass
else:
raise Exception('must raise')
# assert not list_sessions()
def test_optiondescription_group():
2023-05-11 15:44:48 +02:00
groups.addgroup('notfamily')
2018-09-12 21:05:14 +02:00
i = IntOption('test', '')
i2 = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od1.impl_set_group_type(groups.family)
od3 = OptionDescription('od2', '', [i2])
od3.impl_set_group_type(groups.notfamily)
od2 = OptionDescription('od', '', [od1, od3])
cfg = Config(od2)
assert len(list(cfg.option.list('option'))) == 0
assert len(list(cfg.option.list('optiondescription'))) == 2
assert len(list(cfg.option.list('optiondescription', group_type=groups.family))) == 1
assert len(list(cfg.option.list('optiondescription', group_type=groups.notfamily))) == 1
try:
list(cfg.option.list('unknown'))
except AssertionError:
pass
else:
raise Exception('must raise')
try:
list(cfg.option.list('option', group_type='toto'))
except AssertionError:
pass
else:
raise Exception('must raise')
# assert not list_sessions()
def test_optiondescription_group_redefined():
2018-09-12 21:05:14 +02:00
try:
2023-05-11 15:44:48 +02:00
groups.addgroup('notfamily')
2018-09-12 21:05:14 +02:00
except:
pass
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od1.impl_set_group_type(groups.family)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
od1.impl_set_group_type(groups.notfamily)
# assert not list_sessions()
2018-09-12 21:05:14 +02:00
def test_optiondescription_group_leadership():
2018-09-12 21:05:14 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
2020-01-22 20:46:18 +01:00
with pytest.raises(ConfigError):
2019-12-24 15:24:20 +01:00
od1.impl_set_group_type(groups.leadership)
# assert not list_sessions()
2018-09-12 21:05:14 +02:00
def test_asign_optiondescription():
2018-04-06 23:51:25 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
cfg = Config(od2)
2023-04-27 11:44:52 +02:00
with pytest.raises(ConfigError):
cfg.option('od').value.set('test')
2023-04-27 11:44:52 +02:00
with pytest.raises(ConfigError):
cfg.option('od').value.reset()
# assert not list_sessions()
2018-09-11 20:12:06 +02:00
def test_intoption():
2018-09-11 20:12:06 +02:00
i1 = IntOption('test1', 'description', min_number=3)
i2 = IntOption('test2', 'description', max_number=3)
i3 = IntOption('test3', 'description', min_number=3, max_number=6, warnings_only=True)
od = OptionDescription('od', '', [i1, i2, i3])
cfg = Config(od)
with pytest.raises(ValueError):
cfg.option('test1').value.set(2)
cfg.option('test1').value.set(3)
assert cfg.option('test1').value.valid() is True
cfg.option('test1').value.set(4)
cfg.option('test2').value.set(2)
cfg.option('test2').value.set(3)
with pytest.raises(ValueError):
cfg.option('test2').value.set(4)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
cfg.option('test3').value.set(2)
assert cfg.option('test3').value.valid() is True
assert len(w) == 1
with warnings.catch_warnings(record=True) as w:
cfg.option('test3').value.set(7)
assert cfg.option('test3').value.valid() is True
cfg.option('test3').value.set(4)
assert cfg.option('test3').value.valid() is True
assert len(w) == 1
# assert not list_sessions()
def test_option_not_in_config():
i1 = IntOption('test1', 'description', min_number=3)
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
i1.impl_getpath()
# assert not list_sessions()
def test_option_unknown_func():
i1 = IntOption('test1', 'description', min_number=3)
i2 = IntOption('test2', 'description', max_number=3)
i3 = IntOption('test3', 'description', min_number=3, max_number=6, warnings_only=True)
od = OptionDescription('od', '', [i1, i2, i3])
cfg = Config(od)
2023-04-27 11:44:52 +02:00
with pytest.raises(ConfigError):
cfg.option('test1').value.unknown()
2023-05-11 15:44:48 +02:00
def test_option_with_index():
i1 = IntOption('test1', 'description', [4, 5], min_number=3, multi=True)
i2 = IntOption('test2', 'description', max_number=3)
i3 = IntOption('test3', 'description', min_number=3, max_number=6, warnings_only=True)
od = OptionDescription('od', '', [i1, i2, i3])
cfg = Config(od)
with pytest.raises(ConfigError):
cfg.option('test1', 0).value.get()
def test_option_display_name():
i1 = IntOption('test1', 'description', min_number=3)
i2 = IntOption('test2', 'description', max_number=3)
i3 = IntOption('test3', 'description', min_number=3, max_number=6, warnings_only=True)
od = OptionDescription('od', '', [i1, i2, i3])
cfg = Config(od,
display_name=display_name,
)
assert cfg.option('test1').name() == 'test1'
assert cfg.option('test1').doc() == 'display_name'