From faa5f530c8371cf9f2ce76361194b76a229ae2c3 Mon Sep 17 00:00:00 2001 From: Joshua Beretta Date: Tue, 4 Oct 2022 14:04:27 +0000 Subject: [PATCH] handle multiple spaces in or_other and add tests --- src/formpack/utils/expand_content.py | 4 ++-- tests/test_expand_content.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/formpack/utils/expand_content.py b/src/formpack/utils/expand_content.py index 3f1c8c67..df2fd78f 100644 --- a/src/formpack/utils/expand_content.py +++ b/src/formpack/utils/expand_content.py @@ -303,9 +303,9 @@ def _mark_special(**kwargs: str) -> None: def _expand_type_to_dict(type_str: str) -> Dict[str, Union[str, bool]]: SELECT_PATTERN = r'^({select_type})\s+(\S+)$' out = {} - match = re.search('( or.other)$', type_str) + match = re.search('\s+(or.other)$', type_str) if match: - type_str = type_str.replace(match.groups()[0], '') + type_str = type_str.replace(match.groups()[0], '').strip() out[OR_OTHER_COLUMN] = True match = re.search('select_(one|multiple)(_or_other)', type_str) if match: diff --git a/tests/test_expand_content.py b/tests/test_expand_content.py index 28872671..b23e0467 100644 --- a/tests/test_expand_content.py +++ b/tests/test_expand_content.py @@ -15,6 +15,7 @@ def test_expand_selects_with_or_other(): assert _expand_type_to_dict('select_one xx or other').get(_OR_OTHER) == True + assert _expand_type_to_dict('select_one xx or_other').get(_OR_OTHER) == True assert _expand_type_to_dict('select_one_or_other xx').get(_OR_OTHER) == True assert ( _expand_type_to_dict('select_multiple_or_other xx').get(_OR_OTHER) @@ -56,6 +57,14 @@ def test_expand_select_one_or_other(): assert s1['survey'][0]['select_from_list_name'] == 'dogs' +def test_expand_select_one_or_other_with_spaces(): + s1 = {'survey': [{'type': 'select_one dogs or_other'}]} + expand_content(s1, in_place=True) + assert s1['survey'][0]['type'] == 'select_one' + assert s1['survey'][0]['select_from_list_name'] == 'dogs' + assert s1['survey'][0][_OR_OTHER] == True + + def test_expand_select_multiple(): s1 = {'survey': [{'type': 'select_multiple dogs'}]} expand_content(s1, in_place=True)