summaryrefslogtreecommitdiffstats
path: root/tests/test_ipalib
diff options
context:
space:
mode:
authorJohn Dennis <jdennis@redhat.com>2009-11-20 13:41:44 -0500
committerRob Crittenden <rcritten@redhat.com>2009-11-23 16:53:43 -0500
commit5d2bbf53256c212ad26505bae6827f6739c73f22 (patch)
treeabb951adf6a64dc75ff27a3a299a5ee71bdf3863 /tests/test_ipalib
parent55422cb7b9319eaabc929151a9f4506e5d85600e (diff)
downloadfreeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.tar.gz
freeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.tar.xz
freeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.zip
Reading INT parameter class should respect radix prefix
This modifies the original patch by including a unit test, handling floats when passed as unicode, and handling large magnitude values beyond maxint. The INT parameter class was not respecting any radix prefix (e.g. 0x) the user may have supplied. This patch implements _convert_scalar method for the Int class so that we can pass the special radix base of zero to the int constructor telling it to determine the radix from the prefix (if present).
Diffstat (limited to 'tests/test_ipalib')
-rw-r--r--tests/test_ipalib/test_parameters.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test_ipalib/test_parameters.py b/tests/test_ipalib/test_parameters.py
index f6bc66f4..d1c5f7f9 100644
--- a/tests/test_ipalib/test_parameters.py
+++ b/tests/test_ipalib/test_parameters.py
@@ -22,6 +22,7 @@ Test the `ipalib.parameters` module.
"""
import re
+import sys
from types import NoneType
from inspect import isclass
from tests.util import raises, ClassChecker, read_only
@@ -1225,6 +1226,32 @@ class test_Int(ClassChecker):
assert dummy.called() is True
dummy.reset()
+ def test_convert_scalar(self):
+ """
+ Test the `ipalib.parameters.Int._convert_scalar` method.
+ Assure radix prefixes work, str objects fail,
+ floats (native & string) are truncated,
+ large magnitude values are promoted to long,
+ empty strings & invalid numerical representations fail
+ """
+ o = self.cls('my_number')
+ # Assure invalid inputs raise error
+ for bad in ['hello', u'hello', True, None, '10', u'', u'.']:
+ e = raises(errors.ConversionError, o._convert_scalar, bad)
+ assert e.name == 'my_number'
+ assert e.index is None
+ # Assure large magnatude values are handled correctly
+ assert type(o._convert_scalar(sys.maxint*2)) == long
+ assert o._convert_scalar(sys.maxint*2) == sys.maxint*2
+ assert o._convert_scalar(unicode(sys.maxint*2)) == sys.maxint*2
+ assert o._convert_scalar(long(16)) == 16
+ # Assure normal conversions produce expected result
+ assert o._convert_scalar(u'16.99') == 16
+ assert o._convert_scalar(16.99) == 16
+ assert o._convert_scalar(u'16') == 16
+ assert o._convert_scalar(u'0x10') == 16
+ assert o._convert_scalar(u'020') == 16
+
class test_Float(ClassChecker):
"""
Test the `ipalib.parameters.Float` class.