73: Started work on validation rules for opt; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-08-07 04:51:21 +00:00
parent fadbae6420
commit 8a6041b797
3 changed files with 118 additions and 31 deletions

View File

@ -27,6 +27,19 @@ import plugable
import errors
RULE_FLAG = 'validation_rule'
def rule(obj):
assert not hasattr(obj, RULE_FLAG)
setattr(obj, RULE_FLAG, True)
return obj
def is_rule(obj):
return getattr(obj, RULE_FLAG, False) is True
class opt(plugable.ReadOnly):
__public__ = frozenset((
'normalize',
@ -35,6 +48,7 @@ class opt(plugable.ReadOnly):
'required',
'type',
))
__rules = None
def normalize(self, value):
try:
@ -44,6 +58,18 @@ class opt(plugable.ReadOnly):
self.__class__.__name__, value, self.type
)
def __get_rules(self):
if self.__rules is None:
self.__rules = tuple(self.__rules_iter())
return self.__rules
rules = property(__get_rules)
def __rules_iter(self):
pass
def validate(self, value):
pass

View File

@ -25,14 +25,68 @@ from tstutil import raises, getitem, no_set, no_del, read_only
from ipalib import public, plugable, errors
def test_opt():
cls = public.opt
def test_RULE_FLAG():
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
def test_is_rule():
is_rule = public.is_rule
flag = public.RULE_FLAG
class example(object):
def __init__(self, value):
if value is not None:
assert value in (True, False)
setattr(self, flag, value)
obj = example(True)
assert getattr(obj, flag) is True
assert is_rule(obj)
obj = example(False)
assert getattr(obj, flag) is False
assert not is_rule(obj)
obj = example(None)
assert not hasattr(obj, flag)
assert not is_rule(obj)
class test_opt():
def cls(self):
return public.opt
def sub(self):
class int_opt(self.cls()):
type = int
return int_opt
def test_class(self):
cls = self.cls()
assert issubclass(cls, plugable.ReadOnly)
class int_opt(cls):
type = int
def test_normalize(self):
sub = self.sub()
i = int_opt()
i = sub()
# Test with values that can't be converted:
nope = (

View File

@ -86,7 +86,14 @@ def read_only(obj, name, value='some_new_obj'):
return getattr(obj, name)
def is_prop(prop):
return type(prop) is property
class ClassChecker(object):
def new(self, *args, **kw):
return self.cls(*args, **kw)
def get_sub(self):
raise NotImplementedError('get_sub()')