From 2c5d92ad3ef217743fbd9a5cf02202a0d36a9afd Mon Sep 17 00:00:00 2001 From: Ross Webster Date: Wed, 22 Nov 2023 12:33:05 +1000 Subject: [PATCH 1/4] [QOLSVC-3826] Convert empty size string to None and add unit tests --- ckanext/data_qld/converters.py | 4 +++- ckanext/data_qld/tests/test_converters.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ckanext/data_qld/tests/test_converters.py diff --git a/ckanext/data_qld/converters.py b/ckanext/data_qld/converters.py index 4639107b..402f0a5a 100644 --- a/ckanext/data_qld/converters.py +++ b/ckanext/data_qld/converters.py @@ -17,10 +17,12 @@ def filesize_converter(value, context): 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) is None: 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..b68c079d --- /dev/null +++ b/ckanext/data_qld/tests/test_converters.py @@ -0,0 +1,23 @@ +# 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 = {'foo': 'FOO', + 'FOO': 'FOO', + 'foo,baz': 'FOOBAZ', + ' , ': None, + ' ': None, + '': None, + None: None, + 1024: '1 KiB', + '1024': '1 KiB', + '1024, ': '1 KiB', + '1024a': '1024A', + } + for key, value in six.iteritems(test_cases): + assert value == filesize_converter(key, {}) From ec59359a28b96339e3500be70d0874ebfc0305dc Mon Sep 17 00:00:00 2001 From: Ross Webster Date: Wed, 22 Nov 2023 14:58:20 +1000 Subject: [PATCH 2/4] [QOLSVC-3826] Fixing unit tests --- ckanext/data_qld/tests/test_converters.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ckanext/data_qld/tests/test_converters.py b/ckanext/data_qld/tests/test_converters.py index b68c079d..aa53c6b8 100644 --- a/ckanext/data_qld/tests/test_converters.py +++ b/ckanext/data_qld/tests/test_converters.py @@ -7,17 +7,13 @@ def test_filesize_converter(): - test_cases = {'foo': 'FOO', - 'FOO': 'FOO', - 'foo,baz': 'FOOBAZ', - ' , ': None, + test_cases = {' , ': None, ' ': None, '': None, None: None, - 1024: '1 KiB', - '1024': '1 KiB', - '1024, ': '1 KiB', - '1024a': '1024A', + '2 kb': 2048, + 1024: 1024, + '1024, ': 1024, } for key, value in six.iteritems(test_cases): assert value == filesize_converter(key, {}) From 67c56df43c68c8ca6b8027274ffea6e11a294a62 Mon Sep 17 00:00:00 2001 From: Ross Webster Date: Wed, 22 Nov 2023 15:17:57 +1000 Subject: [PATCH 3/4] [QOLSVC-3826] Check for None before converting to string, and adding more unit test cases --- ckanext/data_qld/converters.py | 2 ++ ckanext/data_qld/tests/test_converters.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ckanext/data_qld/converters.py b/ckanext/data_qld/converters.py index 402f0a5a..79a53f66 100644 --- a/ckanext/data_qld/converters.py +++ b/ckanext/data_qld/converters.py @@ -12,6 +12,8 @@ def filesize_converter(value, context): :rtype: int """ + if not value: + return None value = str(value) # remove whitespaces value = re.sub(' ', '', value) diff --git a/ckanext/data_qld/tests/test_converters.py b/ckanext/data_qld/tests/test_converters.py index aa53c6b8..e2d1d491 100644 --- a/ckanext/data_qld/tests/test_converters.py +++ b/ckanext/data_qld/tests/test_converters.py @@ -14,6 +14,8 @@ def test_filesize_converter(): '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, {}) From 9d1d226864bd1aa967f92984a511c4ec6ebfe89d Mon Sep 17 00:00:00 2001 From: Ross Webster Date: Wed, 22 Nov 2023 15:54:56 +1000 Subject: [PATCH 4/4] [QOLSVC-3826] Return numbers as ints, not strings --- ckanext/data_qld/converters.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ckanext/data_qld/converters.py b/ckanext/data_qld/converters.py index 79a53f66..7f450601 100644 --- a/ckanext/data_qld/converters.py +++ b/ckanext/data_qld/converters.py @@ -24,7 +24,9 @@ def filesize_converter(value, context): value = value.upper() # If the size is not all digits then get size converted into bytes - if re.search(r'^\d+$', value) is None: + if re.search(r'^\d+$', value): + value = int(value) + else: value = filesize_bytes(value) return value