diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-13 01:07:33 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-13 01:07:33 -0700 |
commit | c2b0d03f82f16debcc55d34ac44197e0bc97e0e8 (patch) | |
tree | 9b49474abcaad206ef47368d33dce3aa3bf50e3a | |
parent | 33db9fee6017c0777f4fc5da8b020aefd714e387 (diff) | |
download | freeipa-c2b0d03f82f16debcc55d34ac44197e0bc97e0e8.tar.gz freeipa-c2b0d03f82f16debcc55d34ac44197e0bc97e0e8.tar.xz freeipa-c2b0d03f82f16debcc55d34ac44197e0bc97e0e8.zip |
New Param: updated Bytes and Str length rules to use new rule(_, value) calling signature; updated corresponding unit tests
-rw-r--r-- | ipalib/parameter.py | 36 | ||||
-rw-r--r-- | tests/test_ipalib/test_parameter.py | 142 | ||||
-rw-r--r-- | tests/util.py | 14 |
3 files changed, 99 insertions, 93 deletions
diff --git a/ipalib/parameter.py b/ipalib/parameter.py index 1c88d286..dbf75298 100644 --- a/ipalib/parameter.py +++ b/ipalib/parameter.py @@ -618,7 +618,7 @@ class Float(Param): class Bytes(Param): """ - + A parameter for binary data. """ type = str @@ -670,45 +670,40 @@ class Bytes(Param): """ return value - def _rule_minlength(self, _, name, value): + def _rule_minlength(self, _, value): """ Check minlength constraint. """ assert type(value) is str if len(value) < self.minlength: - return _('%(name)s must be at least %(minlength)d bytes') % dict( - name=name, + return _('must be at least %(minlength)d bytes') % dict( minlength=self.minlength, ) - def _rule_maxlength(self, _, name, value): + def _rule_maxlength(self, _, value): """ Check maxlength constraint. """ assert type(value) is str if len(value) > self.maxlength: - return _('%(name)s can be at most %(maxlength)d bytes') % dict( - name=name, + return _('can be at most %(maxlength)d bytes') % dict( maxlength=self.maxlength, ) - def _rule_length(self, _, name, value): + def _rule_length(self, _, value): """ Check length constraint. """ assert type(value) is str if len(value) != self.length: - return _('%(name)s must be exactly %(length)d bytes') % dict( - name=name, + return _('must be exactly %(length)d bytes') % dict( length=self.length, ) - - class Str(Bytes): """ - + A parameter for character (textual) data. """ type = unicode @@ -724,36 +719,33 @@ class Str(Bytes): 'Can only implicitly convert int, float, or bool; got %r' % value ) - def _rule_minlength(self, _, name, value): + def _rule_minlength(self, _, value): """ Check minlength constraint. """ assert type(value) is unicode if len(value) < self.minlength: - return _('%(name)s must be at least %(minlength)d characters') % dict( - name=name, + return _('must be at least %(minlength)d characters') % dict( minlength=self.minlength, ) - def _rule_maxlength(self, _, name, value): + def _rule_maxlength(self, _, value): """ Check maxlength constraint. """ assert type(value) is unicode if len(value) > self.maxlength: - return _('%(name)s can be at most %(maxlength)d characters') % dict( - name=name, + return _('can be at most %(maxlength)d characters') % dict( maxlength=self.maxlength, ) - def _rule_length(self, _, name, value): + def _rule_length(self, _, value): """ Check length constraint. """ assert type(value) is unicode if len(value) != self.length: - return _('%(name)s must be exactly %(length)d characters') % dict( - name=name, + return _('must be exactly %(length)d characters') % dict( length=self.length, ) diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py index 8abba496..3a0bd0c1 100644 --- a/tests/test_ipalib/test_parameter.py +++ b/tests/test_ipalib/test_parameter.py @@ -569,78 +569,79 @@ class test_Bytes(ClassChecker): """ Test the `ipalib.parameter.Bytes._rule_minlength` method. """ - name = 'My Bytes' o = self.cls('my_bytes', minlength=3) assert o.minlength == 3 - m = o._rule_minlength - translation = u'name=%(name)r, minlength=%(minlength)r' + rule = o._rule_minlength + translation = u'minlength=%(minlength)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in ('abc', 'four', '12345'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False - # Test with a failing value: - assert_equal( - m(dummy, name, 'ab'), - translation % dict(name=name, minlength=3), - ) - assert dummy.message == \ - '%(name)s must be at least %(minlength)d bytes' + # Test with failing values: + for value in ('', 'a', '12'): + assert_equal( + rule(dummy, value), + translation % dict(minlength=3) + ) + assert dummy.message == 'must be at least %(minlength)d bytes' + assert dummy.called() is True + dummy.reset() def test_rule_maxlength(self): """ Test the `ipalib.parameter.Bytes._rule_maxlength` method. """ - name = 'My Bytes' o = self.cls('my_bytes', maxlength=4) assert o.maxlength == 4 - m = o._rule_maxlength - translation = u'name=%(name)r, maxlength=%(maxlength)r' + rule = o._rule_maxlength + translation = u'maxlength=%(maxlength)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in ('ab', '123', 'four'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False - # Test with a failing value: - assert_equal( - m(dummy, name, '12345'), - translation % dict(name=name, maxlength=4), - ) - assert dummy.message == \ - '%(name)s can be at most %(maxlength)d bytes' + # Test with failing values: + for value in ('12345', 'sixsix'): + assert_equal( + rule(dummy, value), + translation % dict(maxlength=4) + ) + assert dummy.message == 'can be at most %(maxlength)d bytes' + assert dummy.called() is True + dummy.reset() def test_rule_length(self): """ Test the `ipalib.parameter.Bytes._rule_length` method. """ - name = 'My Bytes' o = self.cls('my_bytes', length=4) assert o.length == 4 - m = o._rule_length - translation = u'name=%(name)r, length=%(length)r' + rule = o._rule_length + translation = u'length=%(length)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in ('1234', 'four'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False # Test with failing values: - for value in ('ab', '123', '12345', 'abcdef'): + for value in ('ab', '123', '12345', 'sixsix'): assert_equal( - m(dummy, name, value), - translation % dict(name=name, length=4), + rule(dummy, value), + translation % dict(length=4), ) - assert dummy.message == \ - '%(name)s must be exactly %(length)d bytes' - dummy = dummy_ugettext(translation) + assert dummy.message == 'must be exactly %(length)d bytes' + assert dummy.called() is True + dummy.reset() class test_Str(ClassChecker): @@ -676,78 +677,79 @@ class test_Str(ClassChecker): """ Test the `ipalib.parameter.Str._rule_minlength` method. """ - name = 'My Str' o = self.cls('my_str', minlength=3) assert o.minlength == 3 - m = o._rule_minlength - translation = u'name=%(name)r, minlength=%(minlength)r' + rule = o._rule_minlength + translation = u'minlength=%(minlength)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in (u'abc', u'four', u'12345'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False - # Test with a failing value: - assert_equal( - m(dummy, name, u'ab'), - translation % dict(name=name, minlength=3), - ) - assert dummy.message == \ - '%(name)s must be at least %(minlength)d characters' + # Test with failing values: + for value in (u'', u'a', u'12'): + assert_equal( + rule(dummy, value), + translation % dict(minlength=3) + ) + assert dummy.message == 'must be at least %(minlength)d characters' + assert dummy.called() is True + dummy.reset() def test_rule_maxlength(self): """ Test the `ipalib.parameter.Str._rule_maxlength` method. """ - name = 'My Str' o = self.cls('my_str', maxlength=4) assert o.maxlength == 4 - m = o._rule_maxlength - translation = u'name=%(name)r, maxlength=%(maxlength)r' + rule = o._rule_maxlength + translation = u'maxlength=%(maxlength)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in (u'ab', u'123', u'four'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False - # Test with a failing value: - assert_equal( - m(dummy, name, u'12345'), - translation % dict(name=name, maxlength=4), - ) - assert dummy.message == \ - '%(name)s can be at most %(maxlength)d characters' + # Test with failing values: + for value in (u'12345', u'sixsix'): + assert_equal( + rule(dummy, value), + translation % dict(maxlength=4) + ) + assert dummy.message == 'can be at most %(maxlength)d characters' + assert dummy.called() is True + dummy.reset() def test_rule_length(self): """ Test the `ipalib.parameter.Str._rule_length` method. """ - name = 'My Str' o = self.cls('my_str', length=4) assert o.length == 4 - m = o._rule_length - translation = u'name=%(name)r, length=%(length)r' + rule = o._rule_length + translation = u'length=%(length)r' dummy = dummy_ugettext(translation) assert dummy.translation is translation # Test with passing values: for value in (u'1234', u'four'): - assert m(dummy, name, value) is None - assert not hasattr(dummy, 'message') + assert rule(dummy, value) is None + assert dummy.called() is False # Test with failing values: - for value in (u'ab', u'123', u'12345', u'abcdef'): + for value in (u'ab', u'123', u'12345', u'sixsix'): assert_equal( - m(dummy, name, value), - translation % dict(name=name, length=4), + rule(dummy, value), + translation % dict(length=4), ) - assert dummy.message == \ - '%(name)s must be exactly %(length)d characters' - dummy = dummy_ugettext(translation) + assert dummy.message == 'must be exactly %(length)d characters' + assert dummy.called() is True + dummy.reset() def test_create_param(): diff --git a/tests/util.py b/tests/util.py index 3033b82b..c1480840 100644 --- a/tests/util.py +++ b/tests/util.py @@ -297,12 +297,24 @@ class dummy_ugettext(object): assert type(self.translation) is unicode def __call__(self, message): - assert type(message) is str assert self.__called is False self.__called = True + assert type(message) is str + assert not hasattr(self, 'message') self.message = message + assert type(self.translation) is unicode return self.translation + def called(self): + return self.__called + + def reset(self): + assert type(self.translation) is unicode + assert type(self.message) is str + del self.message + assert self.__called is True + self.__called = False + class dummy_ungettext(object): __called = False |