89: Moved ClassChecker from test_public.py into tstutil.py; improved unit tests for plugable.ReadOnly

This commit is contained in:
Jason Gerard DeRose
2008-08-08 22:13:49 +00:00
parent 1744723d11
commit 6f144fbaf0
3 changed files with 72 additions and 41 deletions

View File

@@ -22,9 +22,58 @@ Unit tests for `ipalib.plugable` module.
"""
from tstutil import raises, getitem, no_set, no_del, read_only
from tstutil import ClassChecker
from ipalib import plugable, errors
class test_ReadOnly(ClassChecker):
"""
Test the plugable.ReadOnly class
"""
_cls = plugable.ReadOnly
def test_class(self):
assert self.cls.__bases__ == (object,)
def test_when_unlocked(self):
"""
Test that default state is unlocked, that setting and deleting
attributes works.
"""
o = self.cls()
# Setting:
o.hello = 'world'
assert o.hello == 'world'
# Deleting:
del o.hello
assert not hasattr(o, 'hello')
def test_when_locked(self):
"""
Test that after __lock__() has been called, setting or deleting an
attribute raises AttributeError.
"""
obj = self.cls()
obj.__lock__()
names = ['not_an_attribute', 'an_attribute']
for name in names:
no_set(obj, name)
no_del(obj, name)
class some_ro_class(self.cls):
def __init__(self):
self.an_attribute = 'Hello world!'
self.__lock__()
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_valid_identifier():
f = plugable.check_identifier
okay = [
@@ -125,23 +174,7 @@ def test_Plugin():
raises(AssertionError, p.finalize, api)
def test_ReadOnly():
obj = plugable.ReadOnly()
obj.__lock__()
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):
self.an_attribute = 'Hello world!'
self.__lock__()
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():

View File

@@ -21,7 +21,7 @@
Unit tests for `ipalib.public` module.
"""
from tstutil import raises, getitem, no_set, no_del, read_only
from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker
from ipalib import public, plugable, errors
@@ -63,27 +63,7 @@ def test_is_rule():
assert not is_rule(call(None))
class ClassChecker(object):
__cls = None
__subcls = None
def __get_cls(self):
if self.__cls is None:
self.__cls = self._cls
return self.__cls
cls = property(__get_cls)
def __get_subcls(self):
if self.__subcls is None:
self.__subcls = self.get_subcls()
return self.__subcls
subcls = property(__get_subcls)
def get_subcls(self):
raise NotImplementedError(
self.__class__.__name__,
'get_subcls()'
)
class test_option(ClassChecker):

View File

@@ -21,6 +21,8 @@
Utility functions for the unit tests.
"""
import inspect
class ExceptionNotRaised(Exception):
"""
Exception raised when an *expected* exception is *not* raised during a
@@ -91,9 +93,25 @@ def is_prop(prop):
class ClassChecker(object):
__cls = None
__subcls = None
def new(self, *args, **kw):
return self.cls(*args, **kw)
def __get_cls(self):
if self.__cls is None:
self.__cls = self._cls
assert inspect.isclass(self.__cls)
return self.__cls
cls = property(__get_cls)
def get_sub(self):
raise NotImplementedError('get_sub()')
def __get_subcls(self):
if self.__subcls is None:
self.__subcls = self.get_subcls()
assert inspect.isclass(self.__subcls)
return self.__subcls
subcls = property(__get_subcls)
def get_subcls(self):
raise NotImplementedError(
self.__class__.__name__,
'get_subcls()'
)