mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-11 00:31:56 -06:00
73: Started work on validation rules for opt; added corresponding unit tests
This commit is contained in:
parent
fadbae6420
commit
8a6041b797
@ -27,6 +27,19 @@ import plugable
|
|||||||
import errors
|
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):
|
class opt(plugable.ReadOnly):
|
||||||
__public__ = frozenset((
|
__public__ = frozenset((
|
||||||
'normalize',
|
'normalize',
|
||||||
@ -35,6 +48,7 @@ class opt(plugable.ReadOnly):
|
|||||||
'required',
|
'required',
|
||||||
'type',
|
'type',
|
||||||
))
|
))
|
||||||
|
__rules = None
|
||||||
|
|
||||||
def normalize(self, value):
|
def normalize(self, value):
|
||||||
try:
|
try:
|
||||||
@ -44,6 +58,18 @@ class opt(plugable.ReadOnly):
|
|||||||
self.__class__.__name__, value, self.type
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,40 +25,94 @@ from tstutil import raises, getitem, no_set, no_del, read_only
|
|||||||
from ipalib import public, plugable, errors
|
from ipalib import public, plugable, errors
|
||||||
|
|
||||||
|
|
||||||
def test_opt():
|
def test_RULE_FLAG():
|
||||||
cls = public.opt
|
assert public.RULE_FLAG == 'validation_rule'
|
||||||
assert issubclass(cls, plugable.ReadOnly)
|
|
||||||
|
|
||||||
class int_opt(cls):
|
|
||||||
type = int
|
|
||||||
|
|
||||||
i = int_opt()
|
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
|
||||||
|
|
||||||
# Test with values that can't be converted:
|
|
||||||
nope = (
|
def test_is_rule():
|
||||||
'7.0'
|
is_rule = public.is_rule
|
||||||
'whatever',
|
flag = public.RULE_FLAG
|
||||||
object,
|
|
||||||
None,
|
class example(object):
|
||||||
)
|
def __init__(self, value):
|
||||||
for val in nope:
|
if value is not None:
|
||||||
e = raises(errors.NormalizationError, i.normalize, val)
|
assert value in (True, False)
|
||||||
assert isinstance(e, errors.ValidationError)
|
setattr(self, flag, value)
|
||||||
assert e.name == 'int_opt'
|
|
||||||
assert e.value == val
|
obj = example(True)
|
||||||
assert e.error == "not <type 'int'>"
|
assert getattr(obj, flag) is True
|
||||||
assert e.type is int
|
assert is_rule(obj)
|
||||||
# Test with values that can be converted:
|
|
||||||
okay = (
|
obj = example(False)
|
||||||
7,
|
assert getattr(obj, flag) is False
|
||||||
7.0,
|
assert not is_rule(obj)
|
||||||
7.2,
|
|
||||||
7L,
|
obj = example(None)
|
||||||
'7',
|
assert not hasattr(obj, flag)
|
||||||
' 7 ',
|
assert not is_rule(obj)
|
||||||
)
|
|
||||||
for val in okay:
|
|
||||||
assert i.normalize(val) == 7
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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_cmd():
|
def test_cmd():
|
||||||
cls = public.cmd
|
cls = public.cmd
|
||||||
|
@ -86,7 +86,14 @@ def read_only(obj, name, value='some_new_obj'):
|
|||||||
return getattr(obj, name)
|
return getattr(obj, name)
|
||||||
|
|
||||||
|
|
||||||
|
def is_prop(prop):
|
||||||
|
return type(prop) is property
|
||||||
|
|
||||||
|
|
||||||
class ClassChecker(object):
|
class ClassChecker(object):
|
||||||
|
|
||||||
def new(self, *args, **kw):
|
def new(self, *args, **kw):
|
||||||
return self.cls(*args, **kw)
|
return self.cls(*args, **kw)
|
||||||
|
|
||||||
|
def get_sub(self):
|
||||||
|
raise NotImplementedError('get_sub()')
|
||||||
|
Loading…
Reference in New Issue
Block a user