add tests
This commit is contained in:
parent
26158fc3c4
commit
8d10ad4002
|
@ -6,10 +6,11 @@
|
||||||
import autopath
|
import autopath
|
||||||
from py.test import raises
|
from py.test import raises
|
||||||
|
|
||||||
from tiramisu.config import Config
|
from tiramisu.config import Config, SubConfig
|
||||||
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
|
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
|
||||||
BoolOption, UnicodeOption, OptionDescription
|
BoolOption, UnicodeOption, OptionDescription
|
||||||
from tiramisu.error import ConflictError, ConfigError
|
from tiramisu.error import ConflictError, ConfigError
|
||||||
|
import weakref
|
||||||
|
|
||||||
|
|
||||||
def make_description():
|
def make_description():
|
||||||
|
@ -294,3 +295,26 @@ def test_delete_config_with_subconfig():
|
||||||
raises(ConfigError, 'val[multi]')
|
raises(ConfigError, 'val[multi]')
|
||||||
raises(ConfigError, 'setting[test]')
|
raises(ConfigError, 'setting[test]')
|
||||||
raises(ConfigError, 'sub.make_dict()')
|
raises(ConfigError, 'sub.make_dict()')
|
||||||
|
|
||||||
|
|
||||||
|
def test_config_weakref():
|
||||||
|
o = OptionDescription('val', '', [])
|
||||||
|
o2 = OptionDescription('val', '', [o])
|
||||||
|
c = Config(o2)
|
||||||
|
SubConfig(o, weakref.ref(c))
|
||||||
|
raises(ValueError, "SubConfig(o, c)")
|
||||||
|
s = SubConfig(o, weakref.ref(c))
|
||||||
|
assert s._cfgimpl_get_context() == c
|
||||||
|
del(c)
|
||||||
|
raises(ConfigError, "s._cfgimpl_get_context()")
|
||||||
|
|
||||||
|
|
||||||
|
def test_config_str():
|
||||||
|
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
||||||
|
gcdummy1 = BoolOption('dummy1', 'dummy', default=False, properties=('disabled',))
|
||||||
|
o = OptionDescription('o', '', [gcdummy, gcdummy1])
|
||||||
|
descr = OptionDescription('tiramisu', '', [o])
|
||||||
|
cfg = Config(descr)
|
||||||
|
cfg.read_only()
|
||||||
|
str(cfg)
|
||||||
|
str(cfg.o)
|
||||||
|
|
|
@ -67,6 +67,17 @@ def test_iter_config():
|
||||||
[('string', 'string'), ('string2', 'string2')]
|
[('string', 'string'), ('string2', 'string2')]
|
||||||
|
|
||||||
|
|
||||||
|
def test_iter_config_property():
|
||||||
|
"iteration on config object"
|
||||||
|
s = StrOption("string", "", default="string", properties=('disabled',))
|
||||||
|
s2 = StrOption("string2", "", default="string2")
|
||||||
|
descr = OptionDescription("options", "", [s, s2])
|
||||||
|
config = Config(descr)
|
||||||
|
config.read_only()
|
||||||
|
assert [(name, value) for name, value in config] == \
|
||||||
|
[('string2', 'string2')]
|
||||||
|
|
||||||
|
|
||||||
def test_iter_subconfig():
|
def test_iter_subconfig():
|
||||||
"iteration on config sub object"
|
"iteration on config sub object"
|
||||||
descr = make_description()
|
descr = make_description()
|
||||||
|
@ -177,3 +188,23 @@ def test_filename():
|
||||||
c.a = u'tmp/text.txt'
|
c.a = u'tmp/text.txt'
|
||||||
raises(ValueError, "c.a = u'/tmp/with space.txt'")
|
raises(ValueError, "c.a = u'/tmp/with space.txt'")
|
||||||
raises(ValueError, "c.a = u'/tmp/with$.txt'")
|
raises(ValueError, "c.a = u'/tmp/with$.txt'")
|
||||||
|
|
||||||
|
|
||||||
|
def test_iter_all():
|
||||||
|
s = StrOption("string", "", default="string")
|
||||||
|
s2 = StrOption("string2", "", default="string2")
|
||||||
|
descr = OptionDescription("options", "", [s, s2])
|
||||||
|
config = Config(descr)
|
||||||
|
assert list(config.iter_all()) == [('string', 'string'), ('string2', 'string2')]
|
||||||
|
for i in config.iter_all():
|
||||||
|
#test StopIteration
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def test_iter_all_prop():
|
||||||
|
s = StrOption("string", "", default="string", properties=('disabled',))
|
||||||
|
s2 = StrOption("string2", "", default="string2")
|
||||||
|
descr = OptionDescription("options", "", [s, s2])
|
||||||
|
config = Config(descr)
|
||||||
|
config.read_only()
|
||||||
|
assert list(config.iter_all()) == [('string2', 'string2')]
|
||||||
|
|
|
@ -171,3 +171,21 @@ def test_meta_path():
|
||||||
meta = make_description()
|
meta = make_description()
|
||||||
assert meta._impl_path is None
|
assert meta._impl_path is None
|
||||||
assert meta.od1._impl_path == 'od1'
|
assert meta.od1._impl_path == 'od1'
|
||||||
|
|
||||||
|
|
||||||
|
def test_meta_unconsistent():
|
||||||
|
i1 = IntOption('i1', '')
|
||||||
|
i2 = IntOption('i2', '', default=1)
|
||||||
|
i3 = IntOption('i3', '')
|
||||||
|
i4 = IntOption('i4', '', default=2)
|
||||||
|
od1 = OptionDescription('od1', '', [i1, i2, i3, i4])
|
||||||
|
od2 = OptionDescription('od2', '', [od1])
|
||||||
|
conf1 = Config(od2)
|
||||||
|
conf2 = Config(od2)
|
||||||
|
conf3 = Config(od2)
|
||||||
|
conf4 = Config(od1)
|
||||||
|
meta = MetaConfig([conf1, conf2])
|
||||||
|
meta.cfgimpl_get_settings().setowner(owners.meta)
|
||||||
|
raises(TypeError, 'MetaConfig("string")')
|
||||||
|
raises(ValueError, "MetaConfig([conf1, conf3])")
|
||||||
|
raises(ValueError, "MetaConfig([conf3, conf4])")
|
||||||
|
|
|
@ -94,6 +94,19 @@ def test_iter_on_groups():
|
||||||
result = list(config.creole.iter_groups(group_type=groups.family))
|
result = list(config.creole.iter_groups(group_type=groups.family))
|
||||||
group_names = [res[0] for res in result]
|
group_names = [res[0] for res in result]
|
||||||
assert group_names == ['general', 'interface1']
|
assert group_names == ['general', 'interface1']
|
||||||
|
for i in config.creole.iter_groups(group_type=groups.family):
|
||||||
|
#test StopIteration
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def test_iter_on_groups_props():
|
||||||
|
descr = make_description()
|
||||||
|
config = Config(descr)
|
||||||
|
config.read_write()
|
||||||
|
config.cfgimpl_get_settings()[descr.creole.interface1].append('disabled')
|
||||||
|
result = list(config.creole.iter_groups(group_type=groups.family))
|
||||||
|
group_names = [res[0] for res in result]
|
||||||
|
assert group_names == ['general']
|
||||||
|
|
||||||
|
|
||||||
def test_iter_on_empty_group():
|
def test_iter_on_empty_group():
|
||||||
|
|
|
@ -149,8 +149,6 @@ class SubConfig(object):
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
lines.append("{0} = {1}".format(name,
|
lines.append("{0} = {1}".format(name,
|
||||||
value.encode(default_encoding)))
|
value.encode(default_encoding)))
|
||||||
except PropertiesOptionError:
|
|
||||||
pass
|
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|
||||||
__repr__ = __str__
|
__repr__ = __str__
|
||||||
|
@ -540,7 +538,7 @@ class _CommonConfig(SubConfig):
|
||||||
# ----- state
|
# ----- state
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
if self._impl_meta is not None:
|
if self._impl_meta is not None:
|
||||||
raise ConfigError('cannot serialize Config with MetaConfig')
|
raise ConfigError(_('cannot serialize Config with MetaConfig'))
|
||||||
slots = set()
|
slots = set()
|
||||||
for subclass in self.__class__.__mro__:
|
for subclass in self.__class__.__mro__:
|
||||||
if subclass is not object:
|
if subclass is not object:
|
||||||
|
@ -554,8 +552,8 @@ class _CommonConfig(SubConfig):
|
||||||
pass
|
pass
|
||||||
storage = self._impl_values._p_._storage
|
storage = self._impl_values._p_._storage
|
||||||
if not storage.serializable:
|
if not storage.serializable:
|
||||||
raise ConfigError('this storage is not serialisable, could be a '
|
raise ConfigError(_('this storage is not serialisable, could be a '
|
||||||
'none persistent storage')
|
'none persistent storage'))
|
||||||
state['_storage'] = {'session_id': storage.session_id,
|
state['_storage'] = {'session_id': storage.session_id,
|
||||||
'persistent': storage.persistent}
|
'persistent': storage.persistent}
|
||||||
state['_impl_setting'] = _impl_getstate_setting()
|
state['_impl_setting'] = _impl_getstate_setting()
|
||||||
|
@ -742,6 +740,4 @@ def mandatory_warnings(config):
|
||||||
except PropertiesOptionError as err:
|
except PropertiesOptionError as err:
|
||||||
if err.proptype == ['mandatory']:
|
if err.proptype == ['mandatory']:
|
||||||
yield path
|
yield path
|
||||||
except ConfigError:
|
|
||||||
pass
|
|
||||||
config.cfgimpl_reset_cache(only=('values',))
|
config.cfgimpl_reset_cache(only=('values',))
|
||||||
|
|
Loading…
Reference in New Issue