diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-05 02:20:09 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-05 02:20:09 -0700 |
commit | 6d6c0d81ddbfc56672f0595a5f631c5e846d8b2b (patch) | |
tree | aa7b6c0ca70e4211ba10a41015f0a932b08e8412 /tests | |
parent | c121d0064bb7a7bd1a289ae29ceb2dee314c2d2f (diff) | |
download | freeipa-6d6c0d81ddbfc56672f0595a5f631c5e846d8b2b.tar.gz freeipa-6d6c0d81ddbfc56672f0595a5f631c5e846d8b2b.tar.xz freeipa-6d6c0d81ddbfc56672f0595a5f631c5e846d8b2b.zip |
New Param: decided on calling signature for rules; added unit tests for Bytes._rule_minlength, _rule_maxlength, and _rule_length
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_ipalib/test_parameter.py | 102 | ||||
-rw-r--r-- | tests/util.py | 7 |
2 files changed, 107 insertions, 2 deletions
diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py index 63a1f4ae..59e32031 100644 --- a/tests/test_ipalib/test_parameter.py +++ b/tests/test_ipalib/test_parameter.py @@ -346,6 +346,108 @@ class test_Bytes(ClassChecker): assert str(e) == \ "Bytes('my_bytes'): minlength == maxlength; use length=7 instead" + def test_rule_minlength(self): + """ + 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' + 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') + + # 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' + + 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' + 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') + + # 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' + + 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' + 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') + + # 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' + + 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' + 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') + + # Test with failing values: + for value in ('ab', '123', '12345', 'abcdef'): + assert_equal( + m(dummy, name, value), + translation % dict(name=name, length=4), + ) + assert dummy.message == \ + '%(name)s must be exactly %(length)d bytes' + dummy = dummy_ugettext(translation) + class test_Str(ClassChecker): """ diff --git a/tests/util.py b/tests/util.py index 9ed10016..3033b82b 100644 --- a/tests/util.py +++ b/tests/util.py @@ -290,8 +290,11 @@ class PluginTester(object): class dummy_ugettext(object): __called = False - def __init__(self): - self.translation = u'The translation' + def __init__(self, translation=None): + if translation is None: + translation = u'The translation' + self.translation = translation + assert type(self.translation) is unicode def __call__(self, message): assert type(message) is str |