From 8d10ad40022f35267a3edd7b2968cf784bfe93cf Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 2 Feb 2014 22:47:46 +0100 Subject: [PATCH] add tests --- test/test_config.py | 26 +++++++++++++++++++++++++- test/test_config_api.py | 31 +++++++++++++++++++++++++++++++ test/test_metaconfig.py | 18 ++++++++++++++++++ test/test_parsing_group.py | 13 +++++++++++++ tiramisu/config.py | 10 +++------- 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/test/test_config.py b/test/test_config.py index 6db3fcf..8de8a57 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -6,10 +6,11 @@ import autopath from py.test import raises -from tiramisu.config import Config +from tiramisu.config import Config, SubConfig from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \ BoolOption, UnicodeOption, OptionDescription from tiramisu.error import ConflictError, ConfigError +import weakref def make_description(): @@ -294,3 +295,26 @@ def test_delete_config_with_subconfig(): raises(ConfigError, 'val[multi]') raises(ConfigError, 'setting[test]') 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) diff --git a/test/test_config_api.py b/test/test_config_api.py index db40c57..75920eb 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -67,6 +67,17 @@ def test_iter_config(): [('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(): "iteration on config sub object" descr = make_description() @@ -177,3 +188,23 @@ def test_filename(): c.a = u'tmp/text.txt' raises(ValueError, "c.a = u'/tmp/with space.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')] diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index 6986edc..9409d85 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -171,3 +171,21 @@ def test_meta_path(): meta = make_description() assert meta._impl_path is None 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])") diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 7ecd860..c3b6ffc 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -94,6 +94,19 @@ def test_iter_on_groups(): result = list(config.creole.iter_groups(group_type=groups.family)) group_names = [res[0] for res in result] 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(): diff --git a/tiramisu/config.py b/tiramisu/config.py index e8fc3ca..e3ac94f 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -149,8 +149,6 @@ class SubConfig(object): except UnicodeEncodeError: lines.append("{0} = {1}".format(name, value.encode(default_encoding))) - except PropertiesOptionError: - pass return '\n'.join(lines) __repr__ = __str__ @@ -540,7 +538,7 @@ class _CommonConfig(SubConfig): # ----- state def __getstate__(self): if self._impl_meta is not None: - raise ConfigError('cannot serialize Config with MetaConfig') + raise ConfigError(_('cannot serialize Config with MetaConfig')) slots = set() for subclass in self.__class__.__mro__: if subclass is not object: @@ -554,8 +552,8 @@ class _CommonConfig(SubConfig): pass storage = self._impl_values._p_._storage if not storage.serializable: - raise ConfigError('this storage is not serialisable, could be a ' - 'none persistent storage') + raise ConfigError(_('this storage is not serialisable, could be a ' + 'none persistent storage')) state['_storage'] = {'session_id': storage.session_id, 'persistent': storage.persistent} state['_impl_setting'] = _impl_getstate_setting() @@ -742,6 +740,4 @@ def mandatory_warnings(config): except PropertiesOptionError as err: if err.proptype == ['mandatory']: yield path - except ConfigError: - pass config.cfgimpl_reset_cache(only=('values',))