diff --git a/ckanext/data_qld/converters.py b/ckanext/data_qld/converters.py index 4639107b..7f450601 100644 --- a/ckanext/data_qld/converters.py +++ b/ckanext/data_qld/converters.py @@ -12,15 +12,21 @@ def filesize_converter(value, context): :rtype: int """ + if not value: + return None value = str(value) # remove whitespaces value = re.sub(' ', '', value) # remove commas value = re.sub(',', '', value) + if not value: + return None value = value.upper() # If the size is not all digits then get size converted into bytes - if value and re.search(r'^\d+$', value) is None: + if re.search(r'^\d+$', value): + value = int(value) + else: value = filesize_bytes(value) return value diff --git a/ckanext/data_qld/tests/test_converters.py b/ckanext/data_qld/tests/test_converters.py new file mode 100644 index 00000000..e2d1d491 --- /dev/null +++ b/ckanext/data_qld/tests/test_converters.py @@ -0,0 +1,21 @@ +# encoding: utf-8 +"""Unit tests for ckan/logic/converters.py. + +""" +import six +from ckanext.data_qld.converters import filesize_converter + + +def test_filesize_converter(): + test_cases = {' , ': None, + ' ': None, + '': None, + None: None, + '2 kb': 2048, + 1024: 1024, + '1024, ': 1024, + '1,000 bytes': 1000, + '1,000 MB': 1048576000, + } + for key, value in six.iteritems(test_cases): + assert value == filesize_converter(key, {})