From 039b9a2a9b8e4a5a81d338b642869cefa6a6d8e4 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 28 Aug 2008 02:02:03 +0000 Subject: [PATCH] 210: Type.__init__() now takes the type as the first argument, does not use subclass attribute; updated Int, Unicode, and their unit tests accordingly --- ipalib/ipa_types.py | 17 ++++++++++------- ipalib/tests/test_ipa_types.py | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py index 5bcea5344..8d30aaa48 100644 --- a/ipalib/ipa_types.py +++ b/ipalib/ipa_types.py @@ -51,7 +51,14 @@ class Type(ReadOnly): Base class for all IPA types. """ - type = None # Override in base class + def __init__(self, type_): + allowed = (bool, int, float, unicode) + if type_ not in allowed: + raise ValueError( + 'type_ must be in %r, got %r' % (type_, allowed) + ) + self.type = type_ + lock(self) def __get_name(self): """ @@ -75,13 +82,11 @@ class Type(ReadOnly): class Int(Type): - type = int - def __init__(self, min_value=None, max_value=None): check_min_max(min_value, max_value, 'min_value', 'max_value') self.min_value = min_value self.max_value = max_value - lock(self) + super(Int, self).__init__(int) def validate(self, value): if type(value) is not self.type: @@ -93,8 +98,6 @@ class Int(Type): class Unicode(Type): - type = unicode - def __init__(self, min_length=None, max_length=None, pattern=None): check_min_max(min_length, max_length, 'min_length', 'max_length') if min_length is not None and min_length < 0: @@ -112,7 +115,7 @@ class Unicode(Type): self.regex = None else: self.regex = re.compile(pattern) - lock(self) + super(Unicode, self).__init__(unicode) def validate(self, value): if type(value) is not self.type: diff --git a/ipalib/tests/test_ipa_types.py b/ipalib/tests/test_ipa_types.py index 657a99bd7..4ca44121e 100644 --- a/ipalib/tests/test_ipa_types.py +++ b/ipalib/tests/test_ipa_types.py @@ -75,13 +75,14 @@ class test_Int(ClassChecker): def test_class(self): assert self.cls.__bases__ == (ipa_types.Type,) - assert self.cls.type is int def test_init(self): o = self.cls() - assert o.name == 'Int' - assert o.min_value is None - assert o.max_value is None + assert o.__islocked__() is True + assert read_only(o, 'type') is int + assert read_only(o, 'name') == 'Int' + assert read_only(o, 'min_value') is None + assert read_only(o, 'max_value') is None okay = [ (None, -5), @@ -122,7 +123,6 @@ class test_Int(ClassChecker): 'min_value > max_value: min_value=%d, max_value=%d' % (l, h) ) - def test_call(self): o = self.cls() @@ -168,15 +168,16 @@ class test_Unicode(ClassChecker): def test_class(self): assert self.cls.__bases__ == (ipa_types.Type,) - assert self.cls.type is unicode def test_init(self): o = self.cls() + assert o.__islocked__() is True + assert read_only(o, 'type') is unicode assert read_only(o, 'name') == 'Unicode' - assert o.min_length is None - assert o.max_length is None - assert o.pattern is None - assert o.regex is None + assert read_only(o, 'min_length') is None + assert read_only(o, 'max_length') is None + assert read_only(o, 'pattern') is None + assert read_only(o, 'regex') is None # Test min_length, max_length: okay = (