tiramisu/test/test_option.py

228 lines
8.0 KiB
Python
Raw 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()
2013-12-09 17:55:52 +01:00
from py.test import raises
2013-05-05 21:43:19 +02:00
2018-09-12 21:05:14 +02:00
from tiramisu.error import APIError, ConfigError
2018-08-14 23:07:07 +02:00
from tiramisu import IntOption, OptionDescription, Config
2018-09-12 21:05:14 +02:00
from tiramisu.setting import groups
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__)
2013-12-09 18:56:29 +01:00
def a_func():
return None
2013-05-05 21:43:19 +02:00
2013-12-09 17:55:52 +01:00
def test_option_valid_name():
IntOption('test', '')
raises(ValueError, 'IntOption(1, "")')
2014-06-19 23:22:39 +02:00
raises(ValueError, 'IntOption("1test", "")')
IntOption("test1", "")
2013-12-09 17:55:52 +01:00
raises(ValueError, 'IntOption("impl_test", "")')
raises(ValueError, 'IntOption("_test", "")')
2014-06-19 23:22:39 +02:00
raises(ValueError, 'IntOption(" ", "")')
2013-12-09 17:55:52 +01:00
2013-12-09 18:56:29 +01:00
def test_option_with_callback():
#no default value with callback
raises(ValueError, "IntOption('test', '', default=1, callback=a_func)")
2017-02-03 23:39:24 +01:00
def test_option_get_information():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
raises(ValueError, "i.impl_get_information('noinfo')")
i.impl_set_information('info', string)
assert i.impl_get_information('info') == string
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_option_get_information_config():
description = "it's ok"
string = 'some informations'
string
i = IntOption('test', description)
od = OptionDescription('od', '', [i])
Config(od)
raises(ValueError, "i.impl_get_information('noinfo')")
raises(AttributeError, "i.impl_set_information('info', string)")
2015-12-17 22:41:57 +01:00
# assert i.impl_get_information('info') == string
2017-02-03 23:39:24 +01:00
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_option_get_information_config2():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
i.impl_set_information('info', string)
od = OptionDescription('od', '', [i])
Config(od)
raises(ValueError, "i.impl_get_information('noinfo')")
raises(AttributeError, "i.impl_set_information('info', 'hello')")
assert i.impl_get_information('info') == string
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_optiondescription_get_information():
description = "it's ok"
string = 'some informations'
o = OptionDescription('test', description, [])
o.impl_set_information('info', string)
assert o.impl_get_information('info') == string
raises(ValueError, "o.impl_get_information('noinfo')")
assert o.impl_get_information('noinfo', 'default') == 'default'
assert o.impl_get_information('doc') == description
assert o.impl_getdoc() == description
2013-12-09 18:56:29 +01:00
2013-12-09 17:55:52 +01:00
2018-09-22 17:45:52 +02:00
def test_option_isoptiondescription():
i = IntOption('test', '')
od = OptionDescription('od', '', [i])
od = OptionDescription('od', '', [od])
cfg = Config(od)
assert cfg.option('od').option.isoptiondescription()
assert not cfg.option('od.test').option.isoptiondescription()
2013-12-09 17:55:52 +01:00
def test_option_multi():
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
2013-12-09 17:55:52 +01:00
raises(ValueError, "IntOption('test', '', default_multi=1)")
2013-12-09 18:56:29 +01:00
#unvalid default_multi
raises(ValueError, "IntOption('test', '', multi=True, default_multi='yes')")
#not default_multi with callback
raises(ValueError, "IntOption('test', '', multi=True, default_multi=1, callback=a_func)")
2018-04-06 23:51:25 +02:00
def test_unknown_option():
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
2018-08-14 23:07:07 +02:00
api = Config(od2)
2018-04-06 23:51:25 +02:00
# test is an option, not an optiondescription
raises(TypeError, "api.option('od.test.unknown').value.get()")
2018-04-06 23:51:25 +02:00
# unknown is an unknown option
raises(AttributeError, "api.option('unknown').value.get()")
# unknown is an unknown option
raises(AttributeError, "api.option('od.unknown').value.get()")
# unknown is an unknown optiondescription
raises(AttributeError, "api.option('od.unknown.suboption').value.get()")
2018-09-29 20:05:28 +02:00
def test_optiondescription_list():
groups.notfamily1 = groups.GroupType('notfamily1')
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])
api = Config(od4)
assert len(list(api.option('od').list('option'))) == 0
assert len(list(api.option('od').list('optiondescription'))) == 2
assert len(list(api.option('od').list('optiondescription', group_type=groups.family))) == 1
assert len(list(api.option('od').list('optiondescription', group_type=groups.notfamily1))) == 1
assert len(list(api.option('od.od').list('option'))) == 1
assert len(list(api.option('od.od2').list('option'))) == 1
2018-10-31 08:00:19 +01:00
try:
list(api.option('od').list('unknown'))
except AssertionError:
pass
else:
raise Exception('must raise')
try:
list(api.option('od').list('option', group_type='toto'))
except AssertionError:
pass
else:
raise Exception('must raise')
2018-09-29 20:05:28 +02:00
2018-09-12 21:05:14 +02:00
def test_optiondescription_group():
groups.notfamily = groups.GroupType('notfamily')
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])
api = Config(od2)
2018-09-29 20:05:28 +02:00
assert len(list(api.option.list('option'))) == 0
2018-09-12 21:05:14 +02:00
assert len(list(api.option.list('optiondescription'))) == 2
assert len(list(api.option.list('optiondescription', group_type=groups.family))) == 1
assert len(list(api.option.list('optiondescription', group_type=groups.notfamily))) == 1
2018-10-31 08:00:19 +01:00
try:
list(api.option.list('unknown'))
except AssertionError:
pass
else:
raise Exception('must raise')
try:
list(api.option.list('option', group_type='toto'))
except AssertionError:
pass
else:
raise Exception('must raise')
2018-09-12 21:05:14 +02:00
def test_optiondescription_group_redefined():
try:
groups.notfamily = groups.GroupType('notfamily')
except:
pass
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od1.impl_set_group_type(groups.family)
raises(ValueError, "od1.impl_set_group_type(groups.notfamily)")
def test_optiondescription_group_masterslave():
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
raises(ConfigError, "od1.impl_set_group_type(groups.master)")
2018-04-06 23:51:25 +02:00
def test_asign_optiondescription():
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
2018-08-14 23:07:07 +02:00
api = Config(od2)
2018-04-06 23:51:25 +02:00
raises(APIError, "api.option('od').value.set('test')")
raises(APIError, "api.option('od').value.reset()")
2018-09-11 20:12:06 +02:00
def test_intoption():
i1 = IntOption('test1', 'description', min_number=3)
i2 = IntOption('test2', 'description', max_number=3)
od = OptionDescription('od', '', [i1, i2])
cfg = Config(od)
raises(ValueError, "cfg.option('test1').value.set(2)")
cfg.option('test1').value.set(3)
cfg.option('test1').value.set(4)
cfg.option('test2').value.set(2)
cfg.option('test2').value.set(3)
raises(ValueError, "cfg.option('test2').value.set(4)")