Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parsing of (multiple) pulldata calls in a (multi-line) expression #547

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
from functools32 import lru_cache


RE_PULLDATA = re.compile(r"(pulldata\s*\(\s*)(.*?),")


def register_nsmap():
"""Function to register NSMAP namespaces with ETree"""
for prefix, uri in NSMAP.items():
Expand Down Expand Up @@ -400,20 +403,19 @@ def get_instance_info(element, file_id):
instance=node("instance", id=file_id, src=uri),
)

pulldata_calls = get_pulldata_functions(element)
if len(pulldata_calls) > 0:
pulldata_usages = get_pulldata_functions(element)
if len(pulldata_usages) > 0:
pulldata_instances = []
for pulldata_call in pulldata_calls:
pulldata_arguments = re.sub(".*pulldata\s*\(\s*", "", pulldata_call)
parsed_pulldata_arguments = pulldata_arguments.split(",")
if len(parsed_pulldata_arguments) > 0:
first_argument = parsed_pulldata_arguments[0]
first_argument = (
first_argument.replace("'", "").replace('"', "").strip()
)
pulldata_instances.append(
get_instance_info(element, first_argument)
)
for usage in pulldata_usages:
for call_match in re.finditer(RE_PULLDATA, usage):
groups = call_match.groups()
if len(groups) == 2:
first_argument = ( # first argument to pulldata()
groups[1].replace("'", "").replace('"', "").strip()
)
pulldata_instances.append(
get_instance_info(element, first_argument)
)
return pulldata_instances
return None

Expand Down
84 changes: 84 additions & 0 deletions pyxform/tests_v1/test_external_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from pyxform.errors import PyXFormError
from pyxform.tests_v1.pyxform_test_case import PyxformTestCase, PyxformTestError
from textwrap import dedent


class ExternalInstanceTests(PyxformTestCase):
Expand Down Expand Up @@ -408,6 +409,89 @@ def test_external_instance_pulldata_constraint_in_expression(self):
xml__contains=["""<instance id="ID" src="jr://file-csv/ID.csv"/>"""],
)

def test_pulldata_calculate_multi_line_expression__one_call(self):
"""Should find the pulldata instance name in a multi-line expression."""
qd = """\
if(${a},
pulldata('my_data_b', 'my_ref', 'metainstanceID', ${b}),
(count-selected(${c})))
"""
self.assertPyxformXform(
# Can't use md here because of multi-line calculate expression.
ss_structure={
"survey": [
{"type": "calculate", "name": "a", "label": "QA", "calculate": "1"},
{"type": "calculate", "name": "b", "label": "QB", "calculate": "1"},
{"type": "calculate", "name": "c", "label": "QC", "calculate": "1"},
{
"type": "calculate",
"name": "d",
"label": "QD",
"calculate": dedent(qd),
},
]
},
xml__contains=[
"""<instance id="my_data_b" src="jr://file-csv/my_data_b.csv"/>"""
],
# Is Validate OK with the multi-line expression.
run_odk_validate=True,
)

def test_pulldata_calculate_multi_line_expression__multiple_calls(self):
"""Should find the pulldata instance names in a multi-line expression."""
qd = """\
if(${a},
pulldata('my_data_b', 'my_ref', 'metainstanceID', ${b}),
pulldata('my_data_c', 'my_ref', 'metainstanceID', ${c}))
"""
self.assertPyxformXform(
# Can't use md here because of multi-line calculate expression.
ss_structure={
"survey": [
{"type": "calculate", "name": "a", "label": "QA", "calculate": "1"},
{"type": "calculate", "name": "b", "label": "QB", "calculate": "1"},
{"type": "calculate", "name": "c", "label": "QC", "calculate": "1"},
{
"type": "calculate",
"name": "d",
"label": "QD",
"calculate": dedent(qd),
},
]
},
xml__contains=[
"""<instance id="my_data_b" src="jr://file-csv/my_data_b.csv"/>""",
"""<instance id="my_data_c" src="jr://file-csv/my_data_c.csv"/>""",
],
# Is Validate OK with the multi-line expression and multiple instances.
run_odk_validate=True,
)

def test_pulldata_calculate_single_line_expression__multiple_calls(self):
"""Should find all pulldata instance names in an expression."""
qd = (
"if(${a}, pulldata('my_data_b', 'my_ref', 'metainstanceID', ${b}), "
"pulldata('my_data_c', 'my_ref', 'metainstanceID', ${c}))"
)
self.assertPyxformXform(
# Not using md here due to very long calculate expression.
ss_structure={
"survey": [
{"type": "calculate", "name": "a", "label": "QA", "calculate": "1"},
{"type": "calculate", "name": "b", "label": "QB", "calculate": "1"},
{"type": "calculate", "name": "c", "label": "QC", "calculate": "1"},
{"type": "calculate", "name": "d", "label": "QD", "calculate": qd},
]
},
xml__contains=[
"""<instance id="my_data_b" src="jr://file-csv/my_data_b.csv"/>""",
"""<instance id="my_data_c" src="jr://file-csv/my_data_c.csv"/>""",
],
# Is Validate OK with the multiple instances.
run_odk_validate=True,
)

def test_external_instance_pulldata_readonly(self):
"""
Checks if instance node for pulldata function is added
Expand Down