summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-28 07:57:07 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-28 07:57:07 +0000
commit283c6f8fcec6a4687fd2cc99326a7f2b33e4e8bf (patch)
tree36b8d3a59eb8d10d852d079c8dd71f5e7ee8f879
parentc83c478ae17991391ad5431062dd987ea5640469 (diff)
downloadfreeipa-283c6f8fcec6a4687fd2cc99326a7f2b33e4e8bf.tar.gz
freeipa-283c6f8fcec6a4687fd2cc99326a7f2b33e4e8bf.tar.xz
freeipa-283c6f8fcec6a4687fd2cc99326a7f2b33e4e8bf.zip
216: Added ipa_types.Enum.validate() method; added corresponding unit tests
-rw-r--r--ipalib/ipa_types.py7
-rw-r--r--ipalib/tests/test_ipa_types.py17
2 files changed, 21 insertions, 3 deletions
diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py
index 19950ead5..c120b5abf 100644
--- a/ipalib/ipa_types.py
+++ b/ipalib/ipa_types.py
@@ -170,4 +170,11 @@ class Enum(Type):
if type(val) is not type_:
raise TypeError('%r: %r is not %r' % (val, type(val), type_))
self.values = values
+ self.frozenset = frozenset(values)
super(Enum, self).__init__(type_)
+
+ def validate(self, value):
+ if type(value) is not self.type:
+ return 'Incorrect type'
+ if value not in self.frozenset:
+ return 'Invalid value'
diff --git a/ipalib/tests/test_ipa_types.py b/ipalib/tests/test_ipa_types.py
index 6ae94c416..360478fb1 100644
--- a/ipalib/tests/test_ipa_types.py
+++ b/ipalib/tests/test_ipa_types.py
@@ -341,12 +341,13 @@ class test_Enum(ClassChecker):
def test_init(self):
for t in (unicode, int, float):
- vals = (t(1),)
- o = self.cls(*vals)
+ values = (t(1), t(2), t(3))
+ o = self.cls(*values)
assert o.__islocked__() is True
assert read_only(o, 'type') is t
assert read_only(o, 'name') is 'Enum'
- assert read_only(o, 'values') == vals
+ assert read_only(o, 'values') == values
+ assert read_only(o, 'frozenset') == frozenset(values)
# Check that ValueError is raised when no values are given:
e = raises(ValueError, self.cls)
@@ -362,3 +363,13 @@ class test_Enum(ClassChecker):
# type as first:
e = raises(TypeError, self.cls, u'hello', 'world')
assert str(e) == '%r: %r is not %r' % ('world', str, unicode)
+
+ def test_validate(self):
+ values = (u'hello', u'naughty', u'nurse')
+ o = self.cls(*values)
+ for value in values:
+ assert o.validate(value) is None
+ assert o.validate(str(value)) == 'Incorrect type'
+ for value in (u'one fish', u'two fish'):
+ assert o.validate(value) == 'Invalid value'
+ assert o.validate(str(value)) == 'Incorrect type'