2012-07-13 09:42:14 +02:00
|
|
|
"configuration objects global API"
|
|
|
|
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-10-01 08:19:10 +02:00
|
|
|
BoolOption, FilenameOption, OptionDescription
|
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)
|
2014-02-01 16:49:16 +01:00
|
|
|
booloption2 = BoolOption('bool', 'Test boolean option', default=True)
|
2012-07-13 09:42:14 +02:00
|
|
|
intoption = IntOption('int', 'Test int option', default=0)
|
2014-02-01 16:49:16 +01:00
|
|
|
floatoption2 = FloatOption('float', 'Test float option', default=2.3)
|
2012-07-13 09:42:14 +02:00
|
|
|
floatoption = FloatOption('float', 'Test float option', default=2.3)
|
|
|
|
stroption = StrOption('str', 'Test string option', default="abc")
|
|
|
|
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
|
|
|
|
wantref_option = BoolOption('wantref', 'Tests', default=False)
|
|
|
|
wantframework_option = BoolOption('wantframework', 'Test', default=False)
|
2014-02-01 16:49:16 +01:00
|
|
|
gcgroup2 = OptionDescription('gc2', '', [booloption2])
|
|
|
|
gcgroup = OptionDescription('gc', '', [gcgroup2, gcoption, gcdummy, floatoption])
|
2012-07-13 09:42:14 +02:00
|
|
|
descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
|
2013-04-20 21:58:52 +02:00
|
|
|
wantref_option, stroption,
|
|
|
|
wantframework_option,
|
2014-02-01 16:49:16 +01:00
|
|
|
intoption, boolop, floatoption2])
|
2012-07-13 09:42:14 +02:00
|
|
|
return descr
|
|
|
|
|
|
|
|
|
|
|
|
def test_compare_configs():
|
|
|
|
"config object comparison"
|
|
|
|
descr = make_description()
|
|
|
|
conf1 = Config(descr)
|
2012-11-12 12:06:58 +01:00
|
|
|
conf2 = Config(descr)
|
|
|
|
conf2.wantref = True
|
2012-07-13 09:42:14 +02:00
|
|
|
assert conf1 != conf2
|
|
|
|
assert hash(conf1) != hash(conf2)
|
2013-04-20 21:58:52 +02:00
|
|
|
#assert conf1.getkey() != conf2.getkey()
|
2012-07-13 09:42:14 +02:00
|
|
|
conf1.wantref = True
|
|
|
|
assert conf1 == conf2
|
|
|
|
assert hash(conf1) == hash(conf2)
|
2013-04-20 21:58:52 +02:00
|
|
|
#assert conf1.getkey() == conf2.getkey()
|
|
|
|
conf2.gc.dummy = True
|
|
|
|
assert conf1 != conf2
|
|
|
|
assert hash(conf1) != hash(conf2)
|
|
|
|
#assert conf1.getkey() != conf2.getkey()
|
|
|
|
conf1.gc.dummy = True
|
|
|
|
assert conf1 == conf2
|
|
|
|
assert hash(conf1) == hash(conf2)
|
2013-08-24 21:26:10 +02:00
|
|
|
assert not conf1 == 'conf2'
|
|
|
|
assert conf1 != 'conf2'
|
2012-07-13 09:42:14 +02:00
|
|
|
# ____________________________________________________________
|
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_iter_config():
|
|
|
|
"iteration on config object"
|
|
|
|
s = StrOption("string", "", default="string")
|
|
|
|
s2 = StrOption("string2", "", default="string2")
|
2013-04-20 21:58:52 +02:00
|
|
|
descr = OptionDescription("options", "", [s, s2])
|
2012-07-13 09:42:14 +02:00
|
|
|
config = Config(descr)
|
|
|
|
assert [(name, value) for name, value in config] == \
|
2013-04-20 21:58:52 +02:00
|
|
|
[('string', 'string'), ('string2', 'string2')]
|
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
def test_iter_subconfig():
|
|
|
|
"iteration on config sub object"
|
|
|
|
descr = make_description()
|
|
|
|
conf = Config(descr)
|
|
|
|
for (name, value), (gname, gvalue) in \
|
2013-04-20 21:58:52 +02:00
|
|
|
zip(conf.gc, [("name", "ref"), ("dummy", False)]):
|
2012-07-13 09:42:14 +02:00
|
|
|
assert name == gname
|
|
|
|
assert value == gvalue
|
2013-02-27 11:09:13 +01:00
|
|
|
|
2012-10-11 16:16:43 +02:00
|
|
|
|
2012-07-13 09:42:14 +02:00
|
|
|
def test_str():
|
|
|
|
descr = make_description()
|
|
|
|
c = Config(descr)
|
2013-04-20 21:58:52 +02:00
|
|
|
c # does not crash
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_dict():
|
|
|
|
"serialization of the whole config to a dict"
|
|
|
|
descr = OptionDescription("opt", "", [
|
|
|
|
OptionDescription("s1", "", [
|
|
|
|
BoolOption("a", "", default=False)]),
|
|
|
|
IntOption("int", "", default=42)])
|
|
|
|
config = Config(descr)
|
2013-04-04 11:24:00 +02:00
|
|
|
d = config.make_dict()
|
2012-07-13 09:42:14 +02:00
|
|
|
assert d == {"s1.a": False, "int": 42}
|
|
|
|
config.int = 43
|
|
|
|
config.s1.a = True
|
2013-04-04 11:24:00 +02:00
|
|
|
d = config.make_dict()
|
2012-07-13 09:42:14 +02:00
|
|
|
assert d == {"s1.a": True, "int": 43}
|
2013-04-04 11:24:00 +02:00
|
|
|
d2 = config.make_dict(flatten=True)
|
2012-07-13 09:42:14 +02:00
|
|
|
assert d2 == {'a': True, 'int': 43}
|
2013-08-24 22:32:54 +02:00
|
|
|
raises(ValueError, 'd2 = config.make_dict(withvalue="3")')
|
2012-07-13 09:42:14 +02:00
|
|
|
|
|
|
|
|
2012-10-11 16:16:43 +02:00
|
|
|
def test_find_in_config():
|
|
|
|
"finds option in config"
|
|
|
|
descr = make_description()
|
|
|
|
conf = Config(descr)
|
2013-04-03 12:20:26 +02:00
|
|
|
assert conf.find(byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
2014-02-01 16:49:16 +01:00
|
|
|
assert conf.find(byname='float') == [conf.unwrap_from_path('gc.float'), conf.unwrap_from_path('float')]
|
|
|
|
assert conf.find_first(byname='bool') == conf.unwrap_from_path('gc.gc2.bool')
|
2013-04-03 12:20:26 +02:00
|
|
|
assert conf.find_first(byname='dummy') == conf.unwrap_from_path('gc.dummy')
|
2014-02-01 16:49:16 +01:00
|
|
|
assert conf.find_first(byname='float') == conf.unwrap_from_path('gc.float')
|
2013-04-03 12:20:26 +02:00
|
|
|
assert conf.find(bytype=ChoiceOption) == [conf.unwrap_from_path('gc.name'), conf.unwrap_from_path('objspace')]
|
|
|
|
assert conf.find_first(bytype=ChoiceOption) == conf.unwrap_from_path('gc.name')
|
|
|
|
assert conf.find(byvalue='ref') == [conf.unwrap_from_path('gc.name')]
|
|
|
|
assert conf.find_first(byvalue='ref') == conf.unwrap_from_path('gc.name')
|
2012-10-12 11:35:07 +02:00
|
|
|
# combinaison of filters
|
2013-04-03 12:20:26 +02:00
|
|
|
assert conf.find(bytype=BoolOption, byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
|
|
|
assert conf.find_first(bytype=BoolOption, byname='dummy') == conf.unwrap_from_path('gc.dummy')
|
|
|
|
assert conf.find(byvalue=False, byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
|
|
|
assert conf.find_first(byvalue=False, byname='dummy') == conf.unwrap_from_path('gc.dummy')
|
2014-02-01 16:49:16 +01:00
|
|
|
#subconfig
|
|
|
|
assert conf.gc.find(byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
|
|
|
assert conf.gc.find(byname='float') == [conf.unwrap_from_path('gc.float')]
|
|
|
|
assert conf.gc.find(byname='bool') == [conf.unwrap_from_path('gc.gc2.bool')]
|
|
|
|
raises(AttributeError, "conf.gc.find(byname='wantref').first()")
|
|
|
|
# not OptionDescription
|
|
|
|
raises(AttributeError, "conf.find_first(byname='gc')")
|
|
|
|
raises(AttributeError, "conf.gc.find_first(byname='gc2')")
|
2012-10-11 16:16:43 +02:00
|
|
|
|
2013-04-20 21:58:52 +02:00
|
|
|
|
2013-09-22 21:31:37 +02:00
|
|
|
def test_find_multi():
|
|
|
|
b = BoolOption('bool', '', multi=True)
|
|
|
|
o = OptionDescription('od', '', [b])
|
|
|
|
conf = Config(o)
|
|
|
|
raises(AttributeError, "conf.find(byvalue=True)")
|
|
|
|
raises(AttributeError, "conf.find_first(byvalue=True)")
|
|
|
|
conf.bool.append(False)
|
|
|
|
raises(AttributeError, "conf.find(byvalue=True)")
|
|
|
|
raises(AttributeError, "conf.find_first(byvalue=True)")
|
|
|
|
conf.bool.append(False)
|
|
|
|
raises(AttributeError, "conf.find(byvalue=True)")
|
|
|
|
raises(AttributeError, "conf.find_first(byvalue=True)")
|
|
|
|
conf.bool.append(True)
|
|
|
|
assert conf.find(byvalue=True) == [b]
|
|
|
|
assert conf.find_first(byvalue=True) == b
|
|
|
|
|
|
|
|
|
2013-01-28 09:55:51 +01:00
|
|
|
def test_does_not_find_in_config():
|
|
|
|
descr = make_description()
|
|
|
|
conf = Config(descr)
|
2013-04-14 12:01:32 +02:00
|
|
|
raises(AttributeError, "conf.find(byname='IDontExist')")
|
2013-09-30 21:21:47 +02:00
|
|
|
|
|
|
|
|
2013-10-01 08:19:10 +02:00
|
|
|
def test_filename():
|
|
|
|
a = FilenameOption('a', '')
|
2013-09-30 21:21:47 +02:00
|
|
|
o = OptionDescription('o', '', [a])
|
|
|
|
c = Config(o)
|
|
|
|
c.a = u'/'
|
|
|
|
c.a = u'/tmp'
|
|
|
|
c.a = u'/tmp/'
|
|
|
|
c.a = u'/tmp/text.txt'
|
|
|
|
c.a = u'tmp'
|
|
|
|
c.a = u'tmp/'
|
|
|
|
c.a = u'tmp/text.txt'
|
|
|
|
raises(ValueError, "c.a = u'/tmp/with space.txt'")
|
|
|
|
raises(ValueError, "c.a = u'/tmp/with$.txt'")
|