tiramisu/tests/test_option.py

302 lines
10 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()
2019-12-24 15:24:20 +01:00
import pytest
import warnings
2013-05-05 21:43:19 +02:00
2018-09-12 21:05:14 +02:00
from tiramisu.error import APIError, ConfigError
from tiramisu import IntOption, SymLinkOption, OptionDescription, Config, Calculation, groups, list_sessions
from tiramisu.i18n import _
2018-10-31 08:00:19 +01:00
try:
groups.family
except:
groups.family = groups.GroupType('family')
2018-10-31 08:00:19 +01:00
def teardown_function(function):
2019-12-24 15:24:20 +01:00
# some tests emit a warnings because of doesn't create a Config
with warnings.catch_warnings(record=True) as w:
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
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_option_valid_name():
2013-12-09 17:55:52 +01:00
IntOption('test', '')
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
IntOption(1, "")
2019-02-11 20:28:03 +01:00
i = IntOption("test1", "")
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
SymLinkOption(1, i)
2019-02-11 20:28:03 +01:00
i = SymLinkOption("test1", i)
2013-12-09 18:56:29 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_option_get_information():
2017-02-03 23:39:24 +01:00
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
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
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
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
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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])
2019-12-24 15:24:20 +01:00
await Config(od)
with pytest.raises(ValueError):
i.impl_get_information('noinfo')
with pytest.raises(AttributeError):
i.impl_set_information('info', string)
with pytest.raises(ValueError):
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
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
#
2019-12-24 15:24:20 +01:00
assert await cfg.option('test').information.get('noinfo', 'falsedefault') == 'optdefault'
#
2019-12-24 15:24:20 +01:00
await cfg.option('test').information.set('noinfo', 'notdefault')
assert await cfg.option('test').information.get('noinfo', 'falsedefault') == 'notdefault'
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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])
2019-12-24 15:24:20 +01:00
await Config(od)
with pytest.raises(ValueError):
i.impl_get_information('noinfo')
with pytest.raises(AttributeError):
i.impl_set_information('info', 'hello')
2017-02-03 23:39:24 +01:00
assert i.impl_get_information('info') == string
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
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
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
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
2013-12-09 18:56:29 +01:00
2013-12-09 17:55:52 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_option_isoptiondescription():
2018-09-22 17:45:52 +02:00
i = IntOption('test', '')
od = OptionDescription('od', '', [i])
od = OptionDescription('od', '', [od])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
assert await cfg.option('od').option.isoptiondescription()
assert not await cfg.option('od.test').option.isoptiondescription()
2018-09-22 17:45:52 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
assert await cfg.option('od2.od1.test').value.get() is None
assert await cfg.option('od2').option('od1').option('test').value.get() is None
2019-03-02 21:31:21 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
IntOption('test', '', default_multi=1)
2013-12-09 18:56:29 +01:00
#unvalid default_multi
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
IntOption('test', '', multi=True, default_multi='yes')
2019-09-28 16:32:48 +02:00
2019-12-24 15:24:20 +01:00
#@pytest.mark.asyncio
#async def test_option_multi_legacy():
2019-09-28 16:32:48 +02:00
# #not default_multi with callback
2019-12-24 15:24:20 +01:00
# #with pytest.raises(ValueError):
# IntOption('test', '', multi=True, default_multi=1, callback=a_func)")
2018-04-06 23:51:25 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_unknown_option():
2018-04-06 23:51:25 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
2019-12-24 15:24:20 +01:00
cfg = await Config(od2)
2018-04-06 23:51:25 +02:00
# test is an option, not an optiondescription
2019-12-24 15:24:20 +01:00
with pytest.raises(TypeError):
await cfg.option('od.test.unknown').value.get()
2018-04-06 23:51:25 +02:00
# unknown is an unknown option
2019-12-24 15:24:20 +01:00
with pytest.raises(AttributeError):
await cfg.option('unknown').value.get()
2018-04-06 23:51:25 +02:00
# unknown is an unknown option
2019-12-24 15:24:20 +01:00
with pytest.raises(AttributeError):
await cfg.option('od.unknown').value.get()
2018-04-06 23:51:25 +02:00
# unknown is an unknown optiondescription
2019-12-24 15:24:20 +01:00
with pytest.raises(AttributeError):
await cfg.option('od.unknown.suboption').value.get()
2018-04-06 23:51:25 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_optiondescription_list():
2018-09-29 20:05:28 +02:00
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])
2019-12-24 15:24:20 +01:00
cfg = await Config(od4)
assert len(list(await cfg.option('od').list('option'))) == 0
assert len(list(await cfg.option('od').list('optiondescription'))) == 2
assert len(list(await cfg.option('od').list('optiondescription', group_type=groups.family))) == 1
assert len(list(await cfg.option('od').list('optiondescription', group_type=groups.notfamily1))) == 1
assert len(list(await cfg.option('od.od').list('option'))) == 1
assert len(list(await cfg.option('od.od2').list('option'))) == 1
2018-10-31 08:00:19 +01:00
try:
2019-12-24 15:24:20 +01:00
list(await cfg.option('od').list('unknown'))
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
try:
2019-12-24 15:24:20 +01:00
list(await cfg.option('od').list('option', group_type='toto'))
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
2018-09-29 20:05:28 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_optiondescription_group():
2018-09-12 21:05:14 +02:00
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])
2019-12-24 15:24:20 +01:00
cfg = await Config(od2)
assert len(list(await cfg.option.list('option'))) == 0
assert len(list(await cfg.option.list('optiondescription'))) == 2
assert len(list(await cfg.option.list('optiondescription', group_type=groups.family))) == 1
assert len(list(await cfg.option.list('optiondescription', group_type=groups.notfamily))) == 1
2018-10-31 08:00:19 +01:00
try:
2019-12-24 15:24:20 +01:00
list(await cfg.option.list('unknown'))
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
try:
2019-12-24 15:24:20 +01:00
list(await cfg.option.list('option', group_type='toto'))
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
2018-09-12 21:05:14 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_optiondescription_group_redefined():
2018-09-12 21:05:14 +02:00
try:
groups.notfamily = groups.GroupType('notfamily')
except:
pass
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od1.impl_set_group_type(groups.family)
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
od1.impl_set_group_type(groups.notfamily)
2018-09-12 21:05:14 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_optiondescription_group_leadership():
2018-09-12 21:05:14 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
2019-12-24 15:24:20 +01:00
with pytest.raises(ConfigError):
od1.impl_set_group_type(groups.leadership)
2018-09-12 21:05:14 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_asign_optiondescription():
2018-04-06 23:51:25 +02:00
i = IntOption('test', '')
od1 = OptionDescription('od', '', [i])
od2 = OptionDescription('od', '', [od1])
2019-12-24 15:24:20 +01:00
cfg = await Config(od2)
with pytest.raises(APIError):
await cfg.option('od').value.set('test')
with pytest.raises(APIError):
await cfg.option('od').value.reset()
2018-09-11 20:12:06 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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)
od = OptionDescription('od', '', [i1, i2])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
with pytest.raises(ValueError):
await cfg.option('test1').value.set(2)
await cfg.option('test1').value.set(3)
await cfg.option('test1').value.set(4)
await cfg.option('test2').value.set(2)
await cfg.option('test2').value.set(3)
with pytest.raises(ValueError):
await cfg.option('test2').value.set(4)
@pytest.mark.asyncio
async def test_get_display_type():
2019-02-24 20:23:16 +01:00
i1 = IntOption('test1', 'description', min_number=3)
assert i1.get_display_type() == _('integer')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_option_not_in_config():
i1 = IntOption('test1', 'description', min_number=3)
2019-12-24 15:24:20 +01:00
with pytest.raises(AttributeError):
i1.impl_getpath()