2013-07-03 21:56:19 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-08-22 12:17:10 +02:00
|
|
|
|
2013-08-24 21:26:10 +02:00
|
|
|
"""theses tests are much more to test that config, option description, vs...
|
2013-08-22 12:17:10 +02:00
|
|
|
**it's there** and answers via attribute access"""
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
import autopath
|
|
|
|
from py.test import raises
|
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
from tiramisu.config import Config
|
|
|
|
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
|
2013-07-03 21:56:19 +02:00
|
|
|
BoolOption, UnicodeOption, OptionDescription
|
2013-08-28 11:33:43 +02:00
|
|
|
from tiramisu.error import ConflictError
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
def make_description():
|
2013-04-03 12:20:26 +02:00
|
|
|
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
|
2012-07-13 09:42:14 +02:00
|
|
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
|
|
objspaceoption = ChoiceOption('objspace', 'Object space',
|
2013-04-20 21:58:52 +02:00
|
|
|
('std', 'thunk'), 'std')
|
2012-07-13 09:42:14 +02:00
|
|
|
booloption = BoolOption('bool', 'Test boolean option', default=True)
|
|
|
|
intoption = IntOption('int', 'Test int option', default=0)
|
|
|
|
floatoption = FloatOption('float', 'Test float option', default=2.3)
|
2013-04-03 12:20:26 +02:00
|
|
|
stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', ))
|
2012-07-13 09:42:14 +02:00
|
|
|
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
|
|
|
|
wantref_option = BoolOption('wantref', 'Test requires', default=False)
|
|
|
|
wantframework_option = BoolOption('wantframework', 'Test requires',
|
|
|
|
default=False)
|
2012-11-12 12:06:58 +01:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
gcgroup = OptionDescription('gc', '', [gcoption, gcdummy, floatoption])
|
|
|
|
descr = OptionDescription('tiram', '', [gcgroup, booloption, objspaceoption,
|
2013-04-20 21:58:52 +02:00
|
|
|
wantref_option, stroption,
|
|
|
|
wantframework_option,
|
|
|
|
intoption, boolop])
|
2012-07-13 09:42:14 +02:00
|
|
|
return descr
|
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_base_config():
|
2013-08-22 12:17:10 +02:00
|
|
|
"""making a :class:`tiramisu.config.Config()` object
|
|
|
|
and a :class:`tiramisu.option.OptionDescription()` object
|
|
|
|
"""
|
2012-07-13 09:42:14 +02:00
|
|
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
|
|
descr = OptionDescription('tiramisu', '', [gcdummy])
|
|
|
|
cfg = Config(descr)
|
2013-04-20 21:58:52 +02:00
|
|
|
assert cfg.dummy is False
|
2012-07-13 09:42:14 +02:00
|
|
|
dm = cfg.unwrap_from_path('dummy')
|
2013-11-23 23:34:17 +01:00
|
|
|
assert dm.impl_getname() == 'dummy'
|
2012-07-13 09:42:14 +02:00
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2013-08-24 21:26:10 +02:00
|
|
|
def test_not_config():
|
|
|
|
assert raises(TypeError, "Config('str')")
|
|
|
|
|
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def test_base_path():
|
|
|
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
|
|
descr = OptionDescription('tiramisu', '', [gcdummy])
|
|
|
|
cfg = Config(descr)
|
|
|
|
assert cfg._impl_path is None
|
|
|
|
base = OptionDescription('config', '', [descr])
|
|
|
|
cfg = Config(base)
|
|
|
|
assert cfg._impl_path is None
|
|
|
|
assert cfg.tiramisu._impl_path == 'tiramisu'
|
|
|
|
nbase = OptionDescription('baseconfig', '', [base])
|
|
|
|
cfg = Config(nbase)
|
|
|
|
assert cfg._impl_path is None
|
|
|
|
assert cfg.config._impl_path == 'config'
|
|
|
|
assert cfg.config.tiramisu._impl_path == 'config.tiramisu'
|
|
|
|
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_reset_value():
|
|
|
|
descr = make_description()
|
|
|
|
cfg = Config(descr)
|
2013-04-20 21:58:52 +02:00
|
|
|
assert cfg.gc.dummy is False
|
2012-07-13 09:42:14 +02:00
|
|
|
cfg.gc.dummy = True
|
2013-04-20 21:58:52 +02:00
|
|
|
assert cfg.gc.dummy is True
|
|
|
|
|
2012-11-12 12:06:58 +01:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_base_config_and_groups():
|
|
|
|
descr = make_description()
|
|
|
|
# overrides the booloption default value
|
2012-11-12 12:06:58 +01:00
|
|
|
config = Config(descr)
|
|
|
|
config.bool = False
|
2012-07-13 09:42:14 +02:00
|
|
|
assert config.gc.name == 'ref'
|
2013-04-20 21:58:52 +02:00
|
|
|
assert config.bool is False
|
2012-07-13 09:42:14 +02:00
|
|
|
nm = config.unwrap_from_path('gc.name')
|
2013-11-23 23:34:17 +01:00
|
|
|
assert nm.impl_getname() == 'name'
|
2012-07-13 09:42:14 +02:00
|
|
|
gc = config.unwrap_from_path('gc')
|
2013-11-23 23:34:17 +01:00
|
|
|
assert gc.impl_getname() == 'gc'
|
|
|
|
#nm = config.unwrap_fromimpl_getname()('name')
|
|
|
|
#assert nm.impl_getname() == 'name'
|
2012-11-12 12:06:58 +01:00
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_base_config_in_a_tree():
|
2013-08-22 12:17:10 +02:00
|
|
|
"how options are organized into a tree, see :ref:`tree`"
|
2012-07-13 09:42:14 +02:00
|
|
|
descr = make_description()
|
2012-11-12 12:06:58 +01:00
|
|
|
config = Config(descr)
|
|
|
|
config.bool = False
|
2012-07-13 09:42:14 +02:00
|
|
|
assert config.gc.name == 'ref'
|
|
|
|
config.gc.name = 'framework'
|
|
|
|
assert config.gc.name == 'framework'
|
|
|
|
assert getattr(config, "gc.name") == 'framework'
|
|
|
|
|
|
|
|
assert config.objspace == 'std'
|
|
|
|
config.objspace = 'thunk'
|
|
|
|
assert config.objspace == 'thunk'
|
2012-11-12 12:06:58 +01:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
assert config.gc.float == 2.3
|
|
|
|
assert config.int == 0
|
|
|
|
config.gc.float = 3.4
|
|
|
|
config.int = 123
|
|
|
|
assert config.gc.float == 3.4
|
|
|
|
assert config.int == 123
|
|
|
|
|
|
|
|
assert not config.wantref
|
|
|
|
|
|
|
|
assert config.str == "abc"
|
|
|
|
config.str = "def"
|
|
|
|
assert config.str == "def"
|
|
|
|
|
|
|
|
raises(AttributeError, 'config.gc.foo = "bar"')
|
|
|
|
|
2012-11-12 12:06:58 +01:00
|
|
|
config = Config(descr)
|
|
|
|
config.bool = False
|
2012-07-13 09:42:14 +02:00
|
|
|
assert config.gc.name == 'ref'
|
|
|
|
config.wantframework = True
|
|
|
|
|
|
|
|
|
|
|
|
def test_cfgimpl_get_home_by_path():
|
2013-08-22 12:17:10 +02:00
|
|
|
" :meth:`tiramisu.config.SubConfig.cfgimpl_get_home_by_path()` to retrieve a path"
|
2012-07-13 09:42:14 +02:00
|
|
|
descr = make_description()
|
2012-11-12 12:06:58 +01:00
|
|
|
config = Config(descr)
|
|
|
|
config.bool = False
|
2013-03-15 09:24:43 +01:00
|
|
|
assert config.cfgimpl_get_home_by_path('gc.dummy')[1] == 'dummy'
|
|
|
|
assert config.cfgimpl_get_home_by_path('dummy')[1] == 'dummy'
|
2013-12-09 15:43:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_valid_properties():
|
|
|
|
raises(TypeError, "stroption = StrOption('str', 'Test string option', default='abc', properties=['mandatory',])")
|
2013-05-08 18:14:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_information_config():
|
|
|
|
descr = make_description()
|
|
|
|
config = Config(descr)
|
|
|
|
string = 'some informations'
|
|
|
|
config.impl_set_information('info', string)
|
|
|
|
assert config.impl_get_information('info') == string
|
2013-09-03 10:38:28 +02:00
|
|
|
raises(ValueError, "config.impl_get_information('noinfo')")
|
2013-12-09 15:43:45 +01:00
|
|
|
assert config.impl_get_information('noinfo', 'default') == 'default'
|
2013-07-03 21:56:19 +02:00
|
|
|
|
|
|
|
|
2013-11-23 23:34:17 +01:00
|
|
|
#FIXME test impl_get_xxx pour OD ou ne pas cacher
|
2013-08-29 09:29:28 +02:00
|
|
|
def test_config_impl_get_path_by_opt():
|
|
|
|
descr = make_description()
|
|
|
|
config = Config(descr)
|
|
|
|
dummy = config.unwrap_from_path('gc.dummy')
|
|
|
|
boo = config.unwrap_from_path('bool')
|
2013-12-09 18:56:29 +01:00
|
|
|
unknown = IntOption('test', '')
|
2013-08-29 09:29:28 +02:00
|
|
|
assert config.cfgimpl_get_description().impl_get_path_by_opt(boo) == 'bool'
|
|
|
|
assert config.cfgimpl_get_description().impl_get_path_by_opt(dummy) == 'gc.dummy'
|
2013-12-09 18:56:29 +01:00
|
|
|
raises(AttributeError, "config.cfgimpl_get_description().impl_get_path_by_opt(unknown)")
|
2013-08-29 09:29:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_impl_get_opt_by_path():
|
|
|
|
descr = make_description()
|
|
|
|
config = Config(descr)
|
|
|
|
dummy = config.unwrap_from_path('gc.dummy')
|
|
|
|
boo = config.unwrap_from_path('bool')
|
|
|
|
assert config.cfgimpl_get_description().impl_get_opt_by_path('bool') == boo
|
|
|
|
assert config.cfgimpl_get_description().impl_get_opt_by_path('gc.dummy') == dummy
|
2013-12-09 18:56:29 +01:00
|
|
|
raises(AttributeError, "config.cfgimpl_get_description().impl_get_opt_by_path('gc.unknown')")
|
2013-08-29 09:29:28 +02:00
|
|
|
|
|
|
|
|
2013-07-03 21:56:19 +02:00
|
|
|
def test_information_display():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
|
|
|
g2 = StrOption('g2', '', 'héhé')
|
|
|
|
g3 = UnicodeOption('g3', '', u'héhé')
|
|
|
|
g4 = BoolOption('g4', '', True)
|
|
|
|
g5 = StrOption('g5', '')
|
|
|
|
d1 = OptionDescription('od', '', [g1, g2, g3, g4, g5])
|
|
|
|
root = OptionDescription('root', '', [d1])
|
|
|
|
config = Config(root)
|
2013-08-24 21:26:10 +02:00
|
|
|
config.od == """g1 = 1
|
|
|
|
g2 = héhé
|
|
|
|
g3 = héhé
|
|
|
|
g4 = True
|
|
|
|
g5 = None"""
|
|
|
|
config == '[od]'
|
2013-08-24 22:54:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_modified_values():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
|
|
|
g2 = StrOption('g2', '', 'héhé')
|
|
|
|
g3 = UnicodeOption('g3', '', u'héhé')
|
|
|
|
g4 = BoolOption('g4', '', True)
|
|
|
|
g5 = StrOption('g5', '')
|
|
|
|
d1 = OptionDescription('od', '', [g1, g2, g3, g4, g5])
|
|
|
|
root = OptionDescription('root', '', [d1])
|
|
|
|
config = Config(root)
|
|
|
|
assert config.cfgimpl_get_values().get_modified_values() == {}
|
|
|
|
config.od.g5 = 'yes'
|
|
|
|
assert config.cfgimpl_get_values().get_modified_values() == {'od.g5': ('user', 'yes')}
|
|
|
|
config.od.g4 = True
|
|
|
|
assert config.cfgimpl_get_values().get_modified_values() == {'od.g5': ('user', 'yes'), 'od.g4': ('user', True)}
|
2013-08-24 23:10:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_has_value():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
|
|
|
g2 = StrOption('g2', '', 'héhé')
|
|
|
|
g3 = UnicodeOption('g3', '', u'héhé')
|
|
|
|
g4 = BoolOption('g4', '', True)
|
|
|
|
g5 = StrOption('g5', '')
|
|
|
|
d1 = OptionDescription('od', '', [g1, g2, g3, g4, g5])
|
|
|
|
root = OptionDescription('root', '', [d1])
|
|
|
|
config = Config(root)
|
|
|
|
assert not g5 in config.cfgimpl_get_values()
|
|
|
|
config.od.g5 = 'yes'
|
|
|
|
assert g5 in config.cfgimpl_get_values()
|
2013-08-25 18:06:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_values_not_setitem():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
|
|
|
g2 = StrOption('g2', '', 'héhé')
|
|
|
|
g3 = UnicodeOption('g3', '', u'héhé')
|
|
|
|
g4 = BoolOption('g4', '', True)
|
|
|
|
g5 = StrOption('g5', '')
|
|
|
|
d1 = OptionDescription('od', '', [g1, g2, g3, g4, g5])
|
|
|
|
root = OptionDescription('root', '', [d1])
|
|
|
|
config = Config(root)
|
|
|
|
raises(ValueError, "config.cfgimpl_get_values()[g1] = 2")
|
2013-08-28 11:33:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_duplicated_option():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
|
|
|
#in same OptionDescription
|
|
|
|
raises(ConflictError, "d1 = OptionDescription('od', '', [g1, g1])")
|
2013-11-23 23:34:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_duplicated_option_diff_od():
|
|
|
|
g1 = IntOption('g1', '', 1)
|
2013-08-28 11:33:43 +02:00
|
|
|
d1 = OptionDescription('od1', '', [g1])
|
|
|
|
#in different OptionDescription
|
2013-11-23 23:34:17 +01:00
|
|
|
raises(ConflictError, "d2 = OptionDescription('od2', '', [g1])")
|
|
|
|
|
2013-09-27 09:52:18 +02:00
|
|
|
|
2013-12-09 15:43:45 +01:00
|
|
|
|
2013-09-27 09:52:18 +02:00
|
|
|
def test_cannot_assign_value_to_option_description():
|
|
|
|
descr = make_description()
|
|
|
|
cfg = Config(descr)
|
|
|
|
raises(TypeError, "cfg.gc = 3")
|
2013-12-09 17:59:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_multi():
|
|
|
|
i1 = IntOption('test1', '', multi=True)
|
|
|
|
i2 = IntOption('test2', '', multi=True, default_multi=1)
|
|
|
|
i3 = IntOption('test3', '', default=[2], multi=True, default_multi=1)
|
|
|
|
od = OptionDescription('test', '', [i1, i2, i3])
|
|
|
|
config = Config(od)
|
|
|
|
assert config.test1 == []
|
|
|
|
assert config.test2 == []
|
|
|
|
config.test2.append()
|
|
|
|
assert config.test2 == [1]
|
|
|
|
assert config.test3 == [2]
|
|
|
|
config.test3.append()
|
|
|
|
assert config.test3 == [2, 1]
|
2013-12-09 18:56:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_validation():
|
|
|
|
i1 = IntOption('test1', '')
|
|
|
|
od = OptionDescription('test', '', [i1])
|
|
|
|
c = Config(od)
|
|
|
|
setting = c.cfgimpl_get_settings()
|
|
|
|
c.test1 = 1
|
|
|
|
raises(ValueError, 'c.test1 = "yes"')
|
|
|
|
assert c.test1 == 1
|
|
|
|
setting.remove('validator')
|
|
|
|
c.test1 = "yes"
|
|
|
|
assert c.test1 == "yes"
|
|
|
|
setting.append('validator')
|
|
|
|
raises(ValueError, 'c.test1')
|
|
|
|
del(c.test1)
|
|
|
|
assert c.test1 == None
|