diff options
Diffstat (limited to 'ipalib/tests')
-rw-r--r-- | ipalib/tests/test_plugable.py | 738 | ||||
-rw-r--r-- | ipalib/tests/test_public.py | 274 | ||||
-rw-r--r-- | ipalib/tests/test_tstutil.py | 190 | ||||
-rw-r--r-- | ipalib/tests/tstutil.py | 96 |
4 files changed, 649 insertions, 649 deletions
diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py index 668d3406..232fbd71 100644 --- a/ipalib/tests/test_plugable.py +++ b/ipalib/tests/test_plugable.py @@ -26,396 +26,396 @@ from ipalib import plugable, errors def test_to_cli(): - f = plugable.to_cli - assert f('initialize') == 'initialize' - assert f('user_add') == 'user-add' + f = plugable.to_cli + assert f('initialize') == 'initialize' + assert f('user_add') == 'user-add' def test_from_cli(): - f = plugable.from_cli - assert f('initialize') == 'initialize' - assert f('user-add') == 'user_add' + f = plugable.from_cli + assert f('initialize') == 'initialize' + assert f('user-add') == 'user_add' def test_valid_identifier(): - f = plugable.check_identifier - okay = [ - 'user_add', - 'stuff2junk', - 'sixty9', - ] - nope = [ - '_user_add', - '__user_add', - 'user_add_', - 'user_add__', - '_user_add_', - '__user_add__', - '60nine', - ] - for name in okay: - f(name) - for name in nope: - raises(errors.NameSpaceError, f, name) - for name in okay: - raises(errors.NameSpaceError, f, name.upper()) + f = plugable.check_identifier + okay = [ + 'user_add', + 'stuff2junk', + 'sixty9', + ] + nope = [ + '_user_add', + '__user_add', + 'user_add_', + 'user_add__', + '_user_add_', + '__user_add__', + '60nine', + ] + for name in okay: + f(name) + for name in nope: + raises(errors.NameSpaceError, f, name) + for name in okay: + raises(errors.NameSpaceError, f, name.upper()) def test_Plugin(): - cls = plugable.Plugin - assert type(cls.name) is property - - api = 'the api instance' - p = plugable.Plugin() - assert read_only(p, 'name') == 'Plugin' - assert repr(p) == '%s.Plugin()' % plugable.__name__ - assert read_only(p, 'api') is None - raises(AssertionError, p.finalize, None) - p.finalize(api) - assert read_only(p, 'api') is api - raises(AssertionError, p.finalize, api) - - class some_plugin(plugable.Plugin): - pass - p = some_plugin() - assert read_only(p, 'name') == 'some_plugin' - assert repr(p) == '%s.some_plugin()' % __name__ - assert read_only(p, 'api') is None - raises(AssertionError, p.finalize, None) - p.finalize(api) - assert read_only(p, 'api') is api - raises(AssertionError, p.finalize, api) + cls = plugable.Plugin + assert type(cls.name) is property + + api = 'the api instance' + p = plugable.Plugin() + assert read_only(p, 'name') == 'Plugin' + assert repr(p) == '%s.Plugin()' % plugable.__name__ + assert read_only(p, 'api') is None + raises(AssertionError, p.finalize, None) + p.finalize(api) + assert read_only(p, 'api') is api + raises(AssertionError, p.finalize, api) + + class some_plugin(plugable.Plugin): + pass + p = some_plugin() + assert read_only(p, 'name') == 'some_plugin' + assert repr(p) == '%s.some_plugin()' % __name__ + assert read_only(p, 'api') is None + raises(AssertionError, p.finalize, None) + p.finalize(api) + assert read_only(p, 'api') is api + raises(AssertionError, p.finalize, api) def test_ReadOnly(): - obj = plugable.ReadOnly() - names = ['not_an_attribute', 'an_attribute'] - for name in names: - no_set(obj, name) - no_del(obj, name) - - class some_ro_class(plugable.ReadOnly): - def __init__(self): - object.__setattr__(self, 'an_attribute', 'Hello world!') - obj = some_ro_class() - for name in names: - no_set(obj, name) - no_del(obj, name) - assert read_only(obj, 'an_attribute') == 'Hello world!' + obj = plugable.ReadOnly() + names = ['not_an_attribute', 'an_attribute'] + for name in names: + no_set(obj, name) + no_del(obj, name) + + class some_ro_class(plugable.ReadOnly): + def __init__(self): + object.__setattr__(self, 'an_attribute', 'Hello world!') + obj = some_ro_class() + for name in names: + no_set(obj, name) + no_del(obj, name) + assert read_only(obj, 'an_attribute') == 'Hello world!' def test_Proxy(): - cls = plugable.Proxy - assert issubclass(cls, plugable.ReadOnly) - - # Setup: - class base(object): - __public__ = frozenset(( - 'public_0', - 'public_1', - '__call__', - )) - - def public_0(self): - return 'public_0' - - def public_1(self): - return 'public_1' - - def __call__(self, caller): - return 'ya called it, %s.' % caller - - def private_0(self): - return 'private_0' - - def private_1(self): - return 'private_1' - - class plugin(base): - name = 'user_add' - attr_name = 'add' - - # Test that TypeError is raised when base is not a class: - raises(TypeError, cls, base(), None) - - # Test that ValueError is raised when target is not instance of base: - raises(ValueError, cls, base, object()) - - # Test with correct arguments: - i = plugin() - p = cls(base, i) - assert read_only(p, 'name') == 'user_add' - assert list(p) == sorted(base.__public__) - - # Test normal methods: - for n in xrange(2): - pub = 'public_%d' % n - priv = 'private_%d' % n - assert getattr(i, pub)() == pub - assert getattr(p, pub)() == pub - assert hasattr(p, pub) - assert getattr(i, priv)() == priv - assert not hasattr(p, priv) - - # Test __call__: - value = 'ya called it, dude.' - assert i('dude') == value - assert p('dude') == value - assert callable(p) - - # Test name_attr='name' kw arg - i = plugin() - p = cls(base, i, 'attr_name') - assert read_only(p, 'name') == 'add' - - # Test _clone(): - i = plugin() - p = cls(base, i) - assert read_only(p, 'name') == 'user_add' - c = p._clone('attr_name') - assert isinstance(c, cls) - assert read_only(c, 'name') == 'add' - assert c is not p - assert c('whoever') == p('whoever') + cls = plugable.Proxy + assert issubclass(cls, plugable.ReadOnly) + + # Setup: + class base(object): + __public__ = frozenset(( + 'public_0', + 'public_1', + '__call__', + )) + + def public_0(self): + return 'public_0' + + def public_1(self): + return 'public_1' + + def __call__(self, caller): + return 'ya called it, %s.' % caller + + def private_0(self): + return 'private_0' + + def private_1(self): + return 'private_1' + + class plugin(base): + name = 'user_add' + attr_name = 'add' + + # Test that TypeError is raised when base is not a class: + raises(TypeError, cls, base(), None) + + # Test that ValueError is raised when target is not instance of base: + raises(ValueError, cls, base, object()) + + # Test with correct arguments: + i = plugin() + p = cls(base, i) + assert read_only(p, 'name') == 'user_add' + assert list(p) == sorted(base.__public__) + + # Test normal methods: + for n in xrange(2): + pub = 'public_%d' % n + priv = 'private_%d' % n + assert getattr(i, pub)() == pub + assert getattr(p, pub)() == pub + assert hasattr(p, pub) + assert getattr(i, priv)() == priv + assert not hasattr(p, priv) + + # Test __call__: + value = 'ya called it, dude.' + assert i('dude') == value + assert p('dude') == value + assert callable(p) + + # Test name_attr='name' kw arg + i = plugin() + p = cls(base, i, 'attr_name') + assert read_only(p, 'name') == 'add' + + # Test _clone(): + i = plugin() + p = cls(base, i) + assert read_only(p, 'name') == 'user_add' + c = p._clone('attr_name') + assert isinstance(c, cls) + assert read_only(c, 'name') == 'add' + assert c is not p + assert c('whoever') == p('whoever') def test_NameSpace(): - cls = plugable.NameSpace - assert issubclass(cls, plugable.ReadOnly) - - class base(object): - __public__ = frozenset(( - 'plusplus', - )) - - def plusplus(self, n): - return n + 1 - - class plugin(base): - def __init__(self, name): - self.name = name - - def get_name(i): - return 'noun_verb%d' % i - - def get_proxies(n): - for i in xrange(n): - yield plugable.Proxy(base, plugin(get_name(i))) - - cnt = 20 - ns = cls(get_proxies(cnt)) - - # Test __len__ - assert len(ns) == cnt - - # Test __iter__ - i = None - for (i, proxy) in enumerate(ns): - assert type(proxy) is plugable.Proxy - assert proxy.name == get_name(i) - assert i == cnt - 1 - - # Test __contains__, __getitem__, getattr(): - proxies = frozenset(ns) - for i in xrange(cnt): - name = get_name(i) - assert name in ns - proxy = ns[name] - assert proxy.name == name - assert type(proxy) is plugable.Proxy - assert proxy in proxies - assert read_only(ns, name) is proxy - - # Test dir(): - assert set(get_name(i) for i in xrange(cnt)).issubset(set(dir(ns))) - - # Test that KeyError, AttributeError is raised: - name = get_name(cnt) - assert name not in ns - raises(KeyError, getitem, ns, name) - raises(AttributeError, getattr, ns, name) - no_set(ns, name) + cls = plugable.NameSpace + assert issubclass(cls, plugable.ReadOnly) + + class base(object): + __public__ = frozenset(( + 'plusplus', + )) + + def plusplus(self, n): + return n + 1 + + class plugin(base): + def __init__(self, name): + self.name = name + + def get_name(i): + return 'noun_verb%d' % i + + def get_proxies(n): + for i in xrange(n): + yield plugable.Proxy(base, plugin(get_name(i))) + + cnt = 20 + ns = cls(get_proxies(cnt)) + + # Test __len__ + assert len(ns) == cnt + + # Test __iter__ + i = None + for (i, proxy) in enumerate(ns): + assert type(proxy) is plugable.Proxy + assert proxy.name == get_name(i) + assert i == cnt - 1 + + # Test __contains__, __getitem__, getattr(): + proxies = frozenset(ns) + for i in xrange(cnt): + name = get_name(i) + assert name in ns + proxy = ns[name] + assert proxy.name == name + assert type(proxy) is plugable.Proxy + assert proxy in proxies + assert read_only(ns, name) is proxy + + # Test dir(): + assert set(get_name(i) for i in xrange(cnt)).issubset(set(dir(ns))) + + # Test that KeyError, AttributeError is raised: + name = get_name(cnt) + assert name not in ns + raises(KeyError, getitem, ns, name) + raises(AttributeError, getattr, ns, name) + no_set(ns, name) def test_Registrar(): - class Base1(object): - pass - class Base2(object): - pass - class Base3(object): - pass - class plugin1(Base1): - pass - class plugin2(Base2): - pass - class plugin3(Base3): - pass - - # Test creation of Registrar: - r = plugable.Registrar(Base1, Base2) - - # Test __hasitem__, __getitem__: - for base in [Base1, Base2]: - assert base in r - assert base.__name__ in r - assert r[base] == {} - assert r[base.__name__] == {} - - - # Check that TypeError is raised trying to register something that isn't - # a class: - raises(TypeError, r, plugin1()) - - # Check that SubclassError is raised trying to register a class that is - # not a subclass of an allowed base: - raises(errors.SubclassError, r, plugin3) - - # Check that registration works - r(plugin1) - sub_d = r['Base1'] - assert len(sub_d) == 1 - assert sub_d['plugin1'] is plugin1 - # Check that a copy is returned - assert sub_d is not r['Base1'] - assert sub_d == r['Base1'] - - # Check that DuplicateError is raised trying to register exact class - # again: - raises(errors.DuplicateError, r, plugin1) - - # Check that OverrideError is raised trying to register class with same - # name and same base: - orig1 = plugin1 - class base1_extended(Base1): - pass - class plugin1(base1_extended): - pass - raises(errors.OverrideError, r, plugin1) - - # Check that overriding works - r(plugin1, override=True) - sub_d = r['Base1'] - assert len(sub_d) == 1 - assert sub_d['plugin1'] is plugin1 - assert sub_d['plugin1'] is not orig1 - - # Check that MissingOverrideError is raised trying to override a name - # not yet registerd: - raises(errors.MissingOverrideError, r, plugin2, override=True) - - # Check that additional plugin can be registered: - r(plugin2) - sub_d = r['Base2'] - assert len(sub_d) == 1 - assert sub_d['plugin2'] is plugin2 - - - # Setup to test __iter__: - class plugin1a(Base1): - pass - r(plugin1a) - - class plugin1b(Base1): - pass - r(plugin1b) - - class plugin2a(Base2): - pass - r(plugin2a) - - class plugin2b(Base2): - pass - r(plugin2b) - - m = { - 'Base1': set([plugin1, plugin1a, plugin1b]), - 'Base2': set([plugin2, plugin2a, plugin2b]), - } - - # Now test __iter__: - for (base, plugins) in r: - assert base in [Base1, Base2] - assert set(plugins) == m[base.__name__] - assert len(list(r)) == 2 - - # Again test __hasitem__, __getitem__: - for base in [Base1, Base2]: - assert base in r - assert base.__name__ in r - d = dict((p.__name__, p) for p in m[base.__name__]) - assert len(d) == 3 - assert r[base] == d - assert r[base.__name__] == d + class Base1(object): + pass + class Base2(object): + pass + class Base3(object): + pass + class plugin1(Base1): + pass + class plugin2(Base2): + pass + class plugin3(Base3): + pass + + # Test creation of Registrar: + r = plugable.Registrar(Base1, Base2) + + # Test __hasitem__, __getitem__: + for base in [Base1, Base2]: + assert base in r + assert base.__name__ in r + assert r[base] == {} + assert r[base.__name__] == {} + + + # Check that TypeError is raised trying to register something that isn't + # a class: + raises(TypeError, r, plugin1()) + + # Check that SubclassError is raised trying to register a class that is + # not a subclass of an allowed base: + raises(errors.SubclassError, r, plugin3) + + # Check that registration works + r(plugin1) + sub_d = r['Base1'] + assert len(sub_d) == 1 + assert sub_d['plugin1'] is plugin1 + # Check that a copy is returned + assert sub_d is not r['Base1'] + assert sub_d == r['Base1'] + + # Check that DuplicateError is raised trying to register exact class + # again: + raises(errors.DuplicateError, r, plugin1) + + # Check that OverrideError is raised trying to register class with same + # name and same base: + orig1 = plugin1 + class base1_extended(Base1): + pass + class plugin1(base1_extended): + pass + raises(errors.OverrideError, r, plugin1) + + # Check that overriding works + r(plugin1, override=True) + sub_d = r['Base1'] + assert len(sub_d) == 1 + assert sub_d['plugin1'] is plugin1 + assert sub_d['plugin1'] is not orig1 + + # Check that MissingOverrideError is raised trying to override a name + # not yet registerd: + raises(errors.MissingOverrideError, r, plugin2, override=True) + + # Check that additional plugin can be registered: + r(plugin2) + sub_d = r['Base2'] + assert len(sub_d) == 1 + assert sub_d['plugin2'] is plugin2 + + + # Setup to test __iter__: + class plugin1a(Base1): + pass + r(plugin1a) + + class plugin1b(Base1): + pass + r(plugin1b) + + class plugin2a(Base2): + pass + r(plugin2a) + + class plugin2b(Base2): + pass + r(plugin2b) + + m = { + 'Base1': set([plugin1, plugin1a, plugin1b]), + 'Base2': set([plugin2, plugin2a, plugin2b]), + } + + # Now test __iter__: + for (base, plugins) in r: + assert base in [Base1, Base2] + assert set(plugins) == m[base.__name__] + assert len(list(r)) == 2 + + # Again test __hasitem__, __getitem__: + for base in [Base1, Base2]: + assert base in r + assert base.__name__ in r + d = dict((p.__name__, p) for p in m[base.__name__]) + assert len(d) == 3 + assert r[base] == d + assert r[base.__name__] == d def test_API(): - assert issubclass(plugable.API, plugable.ReadOnly) - - # Setup the test bases, create the API: - class base0(plugable.Plugin): - __public__ = frozenset(( - 'method', - )) - - def method(self, n): - return n - - class base1(plugable.Plugin): - __public__ = frozenset(( - 'method', - )) - - def method(self, n): - return n + 1 - - api = plugable.API(base0, base1) - r = api.register - assert isinstance(r, plugable.Registrar) - assert read_only(api, 'register') is r - - class base0_plugin0(base0): - pass - r(base0_plugin0) - - class base0_plugin1(base0): - pass - r(base0_plugin1) - - class base0_plugin2(base0): - pass - r(base0_plugin2) - - class base1_plugin0(base1): - pass - r(base1_plugin0) - - class base1_plugin1(base1): - pass - r(base1_plugin1) - - class base1_plugin2(base1): - pass - r(base1_plugin2) - - # Test API instance: - api() # Calling instance performs finalization - - def get_base(b): - return 'base%d' % b - - def get_plugin(b, p): - return 'base%d_plugin%d' % (b, p) - - for b in xrange(2): - base_name = get_base(b) - ns = getattr(api, base_name) - assert isinstance(ns, plugable.NameSpace) - assert read_only(api, base_name) is ns - assert len(ns) == 3 - for p in xrange(3): - plugin_name = get_plugin(b, p) - proxy = ns[plugin_name] - assert isinstance(proxy, plugable.Proxy) - assert proxy.name == plugin_name - assert read_only(ns, plugin_name) is proxy - assert read_only(proxy, 'method')(7) == 7 + b + assert issubclass(plugable.API, plugable.ReadOnly) + + # Setup the test bases, create the API: + class base0(plugable.Plugin): + __public__ = frozenset(( + 'method', + )) + + def method(self, n): + return n + + class base1(plugable.Plugin): + __public__ = frozenset(( + 'method', + )) + + def method(self, n): + return n + 1 + + api = plugable.API(base0, base1) + r = api.register + assert isinstance(r, plugable.Registrar) + assert read_only(api, 'register') is r + + class base0_plugin0(base0): + pass + r(base0_plugin0) + + class base0_plugin1(base0): + pass + r(base0_plugin1) + + class base0_plugin2(base0): + pass + r(base0_plugin2) + + class base1_plugin0(base1): + pass + r(base1_plugin0) + + class base1_plugin1(base1): + pass + r(base1_plugin1) + + class base1_plugin2(base1): + pass + r(base1_plugin2) + + # Test API instance: + api() # Calling instance performs finalization + + def get_base(b): + return 'base%d' % b + + def get_plugin(b, p): + return 'base%d_plugin%d' % (b, p) + + for b in xrange(2): + base_name = get_base(b) + ns = getattr(api, base_name) + assert isinstance(ns, plugable.NameSpace) + assert read_only(api, base_name) is ns + assert len(ns) == 3 + for p in xrange(3): + plugin_name = get_plugin(b, p) + proxy = ns[plugin_name] + assert isinstance(proxy, plugable.Proxy) + assert proxy.name == plugin_name + assert read_only(ns, plugin_name) is proxy + assert read_only(proxy, 'method')(7) == 7 + b diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index f05a9c31..f396eed2 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -26,179 +26,179 @@ from ipalib import public, plugable, errors def test_RULE_FLAG(): - assert public.RULE_FLAG == 'validation_rule' + assert public.RULE_FLAG == 'validation_rule' def test_rule(): - flag = public.RULE_FLAG - rule = public.rule - def my_func(): - pass - assert not hasattr(my_func, flag) - rule(my_func) - assert getattr(my_func, flag) is True - @rule - def my_func2(): - pass - assert getattr(my_func2, flag) is True + flag = public.RULE_FLAG + rule = public.rule + def my_func(): + pass + assert not hasattr(my_func, flag) + rule(my_func) + assert getattr(my_func, flag) is True + @rule + def my_func2(): + pass + assert getattr(my_func2, flag) is True def test_is_rule(): - is_rule = public.is_rule - flag = public.RULE_FLAG + is_rule = public.is_rule + flag = public.RULE_FLAG - class no_call(object): - def __init__(self, value): - if value is not None: - assert value in (True, False) - setattr(self, flag, value) + class no_call(object): + def __init__(self, value): + if value is not None: + assert value in (True, False) + setattr(self, flag, value) - class call(no_call): - def __call__(self): - pass + class call(no_call): + def __call__(self): + pass - assert is_rule(call(True)) - assert not is_rule(no_call(True)) - assert not is_rule(call(False)) - assert not is_rule(call(None)) + assert is_rule(call(True)) + assert not is_rule(no_call(True)) + assert not is_rule(call(False)) + assert not is_rule(call(None)) class test_option(): - def cls(self): - return public.option - - def sub(self): - rule = public.rule - class int_opt(self.cls()): - type = int - @rule - def rule_0(self, value): - if value == 0: - return 'cannot be 0' - @rule - def rule_1(self, value): - if value == 1: - return 'cannot be 1' - @rule - def rule_2(self, value): - if value == 2: - return 'cannot be 2' - return int_opt - - def test_class(self): - """ - Perform some tests on the class (not an instance). - """ - cls = self.cls() - #assert issubclass(cls, plugable.ReadOnly) - assert type(cls.rules) is property - - def test_normalize(self): - sub = self.sub() - i = sub() - # Test with values that can't be converted: - nope = ( - '7.0' - 'whatever', - object, - None, - ) - for val in nope: - e = raises(errors.NormalizationError, i.normalize, val) - assert isinstance(e, errors.ValidationError) - assert e.name == 'int_opt' - assert e.value == val - assert e.error == "not <type 'int'>" - assert e.type is int - # Test with values that can be converted: - okay = ( - 7, - 7.0, - 7.2, - 7L, - '7', - ' 7 ', - ) - for val in okay: - assert i.normalize(val) == 7 - - def test_rules(self): - """ - Test the rules property. - """ - o = self.sub()() - assert len(o.rules) == 3 - def get_rule(i): - return getattr(o, 'rule_%d' % i) - rules = tuple(get_rule(i) for i in xrange(3)) - assert o.rules == rules - - def test_validation(self): - """ - Test the validation method. - """ - o = self.sub()() - o.validate(9) - for i in xrange(3): - e = raises(errors.RuleError, o.validate, i) - assert e.error == 'cannot be %d' % i - assert e.value == i + def cls(self): + return public.option + + def sub(self): + rule = public.rule + class int_opt(self.cls()): + type = int + @rule + def rule_0(self, value): + if value == 0: + return 'cannot be 0' + @rule + def rule_1(self, value): + if value == 1: + return 'cannot be 1' + @rule + def rule_2(self, value): + if value == 2: + return 'cannot be 2' + return int_opt + + def test_class(self): + """ + Perform some tests on the class (not an instance). + """ + cls = self.cls() + #assert issubclass(cls, plugable.ReadOnly) + assert type(cls.rules) is property + + def test_normalize(self): + sub = self.sub() + i = sub() + # Test with values that can't be converted: + nope = ( + '7.0' + 'whatever', + object, + None, + ) + for val in nope: + e = raises(errors.NormalizationError, i.normalize, val) + assert isinstance(e, errors.ValidationError) + assert e.name == 'int_opt' + assert e.value == val + assert e.error == "not <type 'int'>" + assert e.type is int + # Test with values that can be converted: + okay = ( + 7, + 7.0, + 7.2, + 7L, + '7', + ' 7 ', + ) + for val in okay: + assert i.normalize(val) == 7 + + def test_rules(self): + """ + Test the rules property. + """ + o = self.sub()() + assert len(o.rules) == 3 + def get_rule(i): + return getattr(o, 'rule_%d' % i) + rules = tuple(get_rule(i) for i in xrange(3)) + assert o.rules == rules + + def test_validation(self): + """ + Test the validation method. + """ + o = self.sub()() + o.validate(9) + for i in xrange(3): + e = raises(errors.RuleError, o.validate, i) + assert e.error == 'cannot be %d' % i + assert e.value == i def test_cmd(): - cls = public.cmd - assert issubclass(cls, plugable.Plugin) + cls = public.cmd + assert issubclass(cls, plugable.Plugin) def test_obj(): - cls = public.obj - assert issubclass(cls, plugable.Plugin) + cls = public.obj + assert issubclass(cls, plugable.Plugin) def test_attr(): - cls = public.attr - assert issubclass(cls, plugable.Plugin) + cls = public.attr + assert issubclass(cls, plugable.Plugin) - class api(object): - obj = dict(user='the user obj') + class api(object): + obj = dict(user='the user obj') - class user_add(cls): - pass + class user_add(cls): + pass - i = user_add() - assert read_only(i, 'obj_name') == 'user' - assert read_only(i, 'attr_name') == 'add' - assert read_only(i, 'obj') is None - i.finalize(api) - assert read_only(i, 'api') is api - assert read_only(i, 'obj') == 'the user obj' + i = user_add() + assert read_only(i, 'obj_name') == 'user' + assert read_only(i, 'attr_name') == 'add' + assert read_only(i, 'obj') is None + i.finalize(api) + assert read_only(i, 'api') is api + assert read_only(i, 'obj') == 'the user obj' def test_mthd(): - cls = public.mthd - assert issubclass(cls, public.attr) - assert issubclass(cls, public.cmd) + cls = public.mthd + assert issubclass(cls, public.attr) + assert issubclass(cls, public.cmd) def test_prop(): - cls = public.prop - assert issubclass(cls, public.attr) + cls = public.prop + assert issubclass(cls, public.attr) def test_PublicAPI(): - cls = public.PublicAPI - assert issubclass(cls, plugable.API) + cls = public.PublicAPI + assert issubclass(cls, plugable.API) - api = cls() + api = cls() - class cmd1(public.cmd): - pass - api.register(cmd1) + class cmd1(public.cmd): + pass + api.register(cmd1) - class cmd2(public.cmd): - pass - api.register(cmd2) + class cmd2(public.cmd): + pass + api.register(cmd2) - api() + api() diff --git a/ipalib/tests/test_tstutil.py b/ipalib/tests/test_tstutil.py index a4c72364..76f819e4 100644 --- a/ipalib/tests/test_tstutil.py +++ b/ipalib/tests/test_tstutil.py @@ -25,124 +25,124 @@ import tstutil class Prop(object): - def __init__(self, *ops): - self.__ops = frozenset(ops) - self.__prop = 'prop value' + def __init__(self, *ops): + self.__ops = frozenset(ops) + self.__prop = 'prop value' - def __get_prop(self): - if 'get' not in self.__ops: - raise AttributeError('get prop') - return self.__prop + def __get_prop(self): + if 'get' not in self.__ops: + raise AttributeError('get prop') + return self.__prop - def __set_prop(self, value): - if 'set' not in self.__ops: - raise AttributeError('set prop') - self.__prop = value + def __set_prop(self, value): + if 'set' not in self.__ops: + raise AttributeError('set prop') + self.__prop = value - def __del_prop(self): - if 'del' not in self.__ops: - raise AttributeError('del prop') - self.__prop = None + def __del_prop(self): + if 'del' not in self.__ops: + raise AttributeError('del prop') + self.__prop = None - prop = property(__get_prop, __set_prop, __del_prop) + prop = property(__get_prop, __set_prop, __del_prop) def test_yes_raised(): - f = tstutil.raises + f = tstutil.raises - class SomeError(Exception): - pass + class SomeError(Exception): + pass - class AnotherError(Exception): - pass + class AnotherError(Exception): + pass - def callback1(): - 'raises correct exception' - raise SomeError() + def callback1(): + 'raises correct exception' + raise SomeError() - def callback2(): - 'raises wrong exception' - raise AnotherError() + def callback2(): + 'raises wrong exception' + raise AnotherError() - def callback3(): - 'raises no exception' + def callback3(): + 'raises no exception' - f(SomeError, callback1) + f(SomeError, callback1) - raised = False - try: - f(SomeError, callback2) - except AnotherError: - raised = True - assert raised + raised = False + try: + f(SomeError, callback2) + except AnotherError: + raised = True + assert raised - raised = False - try: - f(SomeError, callback3) - except tstutil.ExceptionNotRaised: - raised = True - assert raised + raised = False + try: + f(SomeError, callback3) + except tstutil.ExceptionNotRaised: + raised = True + assert raised def test_no_set(): - # Tests that it works when prop cannot be set: - tstutil.no_set(Prop('get', 'del'), 'prop') + # Tests that it works when prop cannot be set: + tstutil.no_set(Prop('get', 'del'), 'prop') - # Tests that ExceptionNotRaised is raised when prop *can* be set: - raised = False - try: - tstutil.no_set(Prop('set'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised + # Tests that ExceptionNotRaised is raised when prop *can* be set: + raised = False + try: + tstutil.no_set(Prop('set'), 'prop') + except tstutil.ExceptionNotRaised: + raised = True + assert raised def test_no_del(): - # Tests that it works when prop cannot be deleted: - tstutil.no_del(Prop('get', 'set'), 'prop') + # Tests that it works when prop cannot be deleted: + tstutil.no_del(Prop('get', 'set'), 'prop') - # Tests that ExceptionNotRaised is raised when prop *can* be set: - raised = False - try: - tstutil.no_del(Prop('del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised + # Tests that ExceptionNotRaised is raised when prop *can* be set: + raised = False + try: + tstutil.no_del(Prop('del'), 'prop') + except tstutil.ExceptionNotRaised: + raised = True + assert raised def test_read_only(): - # Test that it works when prop is read only: - assert tstutil.read_only(Prop('get'), 'prop') == 'prop value' - - # Test that ExceptionNotRaised is raised when prop can be set: - raised = False - try: - tstutil.read_only(Prop('get', 'set'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that ExceptionNotRaised is raised when prop can be deleted: - raised = False - try: - tstutil.read_only(Prop('get', 'del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that ExceptionNotRaised is raised when prop can be both set and - # deleted: - raised = False - try: - tstutil.read_only(Prop('get', 'del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that AttributeError is raised when prop can't be read: - raised = False - try: - tstutil.read_only(Prop(), 'prop') - except AttributeError: - raised = True - assert raised + # Test that it works when prop is read only: + assert tstutil.read_only(Prop('get'), 'prop') == 'prop value' + + # Test that ExceptionNotRaised is raised when prop can be set: + raised = False + try: + tstutil.read_only(Prop('get', 'set'), 'prop') + except tstutil.ExceptionNotRaised: + raised = True + assert raised + + # Test that ExceptionNotRaised is raised when prop can be deleted: + raised = False + try: + tstutil.read_only(Prop('get', 'del'), 'prop') + except tstutil.ExceptionNotRaised: + raised = True + assert raised + + # Test that ExceptionNotRaised is raised when prop can be both set and + # deleted: + raised = False + try: + tstutil.read_only(Prop('get', 'del'), 'prop') + except tstutil.ExceptionNotRaised: + raised = True + assert raised + + # Test that AttributeError is raised when prop can't be read: + raised = False + try: + tstutil.read_only(Prop(), 'prop') + except AttributeError: + raised = True + assert raised diff --git a/ipalib/tests/tstutil.py b/ipalib/tests/tstutil.py index d2de3b86..e3411366 100644 --- a/ipalib/tests/tstutil.py +++ b/ipalib/tests/tstutil.py @@ -22,78 +22,78 @@ Utility functions for the unit tests. """ class ExceptionNotRaised(Exception): - """ - Exception raised when an *expected* exception is *not* raised during a - unit test. - """ - msg = 'expected %s' + """ + Exception raised when an *expected* exception is *not* raised during a + unit test. + """ + msg = 'expected %s' - def __init__(self, expected): - self.expected = expected + def __init__(self, expected): + self.expected = expected - def __str__(self): - return self.msg % self.expected.__name__ + def __str__(self): + return self.msg % self.expected.__name__ def raises(exception, callback, *args, **kw): - """ - Tests that the expected exception is raised; raises ExceptionNotRaised - if test fails. - """ - raised = False - try: - callback(*args, **kw) - except exception, e: - raised = True - if not raised: - raise ExceptionNotRaised(exception) - return e + """ + Tests that the expected exception is raised; raises ExceptionNotRaised + if test fails. + """ + raised = False + try: + callback(*args, **kw) + except exception, e: + raised = True + if not raised: + raise ExceptionNotRaised(exception) + return e def getitem(obj, key): - """ - Works like getattr but for dictionary interface. Uses this in combination - with raises() to test that, for example, KeyError is raised. - """ - return obj[key] + """ + Works like getattr but for dictionary interface. Uses this in combination + with raises() to test that, for example, KeyError is raised. + """ + return obj[key] def no_set(obj, name, value='some_new_obj'): - """ - Tests that attribute cannot be set. - """ - raises(AttributeError, setattr, obj, name, value) + """ + Tests that attribute cannot be set. + """ + raises(AttributeError, setattr, obj, name, value) def no_del(obj, name): - """ - Tests that attribute cannot be deleted. - """ - raises(AttributeError, delattr, obj, name) + """ + Tests that attribute cannot be deleted. + """ + raises(AttributeError, delattr, obj, name) def read_only(obj, name, value='some_new_obj'): - """ - Tests that attribute is read-only. Returns attribute. - """ - # Test that it cannot be set: - no_set(obj, name, value) + """ + Tests that attribute is read-only. Returns attribute. + """ + # Test that it cannot be set: + no_set(obj, name, value) - # Test that it cannot be deleted: - no_del(obj, name) + # Test that it cannot be deleted: + no_del(obj, name) - # Return the attribute - return getattr(obj, name) + # Return the attribute + return getattr(obj, name) def is_prop(prop): - return type(prop) is property + return type(prop) is property class ClassChecker(object): - def new(self, *args, **kw): - return self.cls(*args, **kw) + def new(self, *args, **kw): + return self.cls(*args, **kw) - def get_sub(self): - raise NotImplementedError('get_sub()') + def get_sub(self): + raise NotImplementedError('get_sub()') |