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

${} references in predicates should be expanded using current() #524

Merged
merged 5 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,19 @@ def _var_repl_function(
is_indexed_repeat = matchobj.string.find("indexed-repeat(") > -1
indexed_repeat_regex = re.compile(r"indexed-repeat\([^)]+\)")
function_args_regex = re.compile(r"\b[^()]+\((.*)\)$")
instance_regex = re.compile(r"instance\([^)]+\S+")

def _is_secondary_instance():
"""check if current matchobj is a predicate and part of primary instance"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're really going to tie ourselves up in knots if this code needs to be revisited so making sure the naming is precise is important.

Name: _in_secondary_instance_predicate
Comment: check if ${} expression represented by matchobj is in a predicate for a path expression for a secondary instance

@MartijnR any other ideas for tightening the wording?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @lognaturel
Fixed in the next commit.

# It is possible to have multiple instance in an expression
instance_match_iter = instance_regex.finditer(matchobj.string)
for instance_match in instance_match_iter:
if (
matchobj.start() >= instance_match.start()
and matchobj.end() <= instance_match.end()
):
return True
return False

def _relative_path(name):
"""Given name in ${name}, return relative xpath to ${name}."""
Expand Down Expand Up @@ -974,6 +987,8 @@ def _is_return_relative_path():
)

if _is_return_relative_path():
if not use_current:
use_current = _is_secondary_instance()
relative_path = _relative_path(name)
if relative_path:
return relative_path
Expand Down
150 changes: 150 additions & 0 deletions pyxform/tests_v1/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,153 @@ def test_indexed_repeat_math_expression_with_double_variable_in_nested_repeat_re
"""<bind nodeset="/data/family/person/prev_name" relevant=" ../age &gt; indexed-repeat( /data/family/person/age , /data/family , 1, /data/family/person , 2)" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_repeat_using_select_with_reference_path_in_predicate_uses_current(self,):
"""
Test relative path expansion using current if reference path is inside a predicate in a survey with select choice list
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | begin repeat | item-repeat | Item | |
| | calculate | item-counter | | position(..) |
| | calculate | item | | instance('item')/root/item[itemindex=${item-counter}]/label |
| | begin group | item-info | Item info | |
| | note | item-note | All the following questions are about ${item}. | |
| | select one item | stock-item | Do you stock this item? | |
| | end group | item-info | | |
| | end repeat | | | |
| choices | | | | |
| | list_name | name | label | itemindex |
| | item | gasoline-regular | Gasoline, Regular | 1 |
| | item | gasoline-premium | Gasoline, Premium | 2 |
| | item | gasoline-diesel | Gasoline, Diesel | 3 |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate="instance('item')/root/item[itemindex= current()/../item-counter ]/label" nodeset="/data/item-repeat/item" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_repeat_using_select_uses_current_with_reference_path_in_predicate_and_instance_is_not_first_expression(
self,
):
"""
Test relative path expansion using current if reference path is inside a predicate and instance is not first expression in a survey with select choice list
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | begin repeat | item-repeat | Item | |
| | calculate | item-counter | | position(..) |
| | calculate | item | | ${item-counter} + instance('item')/root/item[itemindex=${item-counter}]/label |
| | begin group | item-info | Item info | |
| | note | item-note | All the following questions are about ${item}. | |
| | select one item | stock-item | Do you stock this item? | |
| | end group | item-info | | |
| | end repeat | | | |
| choices | | | | |
| | list_name | name | label | itemindex |
| | item | gasoline-regular | Gasoline, Regular | 1 |
| | item | gasoline-premium | Gasoline, Premium | 2 |
| | item | gasoline-diesel | Gasoline, Diesel | 3 |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate=" ../item-counter + instance('item')/root/item[itemindex= current()/../item-counter ]/label" nodeset="/data/item-repeat/item" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_repeat_and_group_with_reference_path_in_predicate_uses_current(self,):
"""
Test relative path expansion using current if reference path is inside a predicate in a survey with group
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | xml-external | item | | |
| | begin repeat | rep3 | | |
| | calculate | pos3 | | position(..) |
| | begin group | grp3 | | |
| | text | txt3 | Enter | |
| | calculate | item3 | | instance('item')/root/item[index=${pos3}]/label |
| | end group | | | |
| | end repeat | | | |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate="instance('item')/root/item[index= current()/../../pos3 ]/label" nodeset="/data/rep3/grp3/item3" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_repeat_with_reference_path_in_predicate_uses_current(self,):
"""
Test relative path expansion using current if reference path is inside a predicate
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | xml-external | item | | |
| | calculate | pos1 | | position(..) |
| | begin repeat | rep5 | | |
| | calculate | pos5 | | position(..) |
| | calculate | item5 | | instance('item')/root/item[index=${pos5} and ${pos1} = 1]/label |
| | end repeat | | | |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate="instance('item')/root/item[index= current()/../pos5 and /data/pos1 = 1]/label" nodeset="/data/rep5/item5" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_relative_path_expansion_not_using_current_if_reference_path_is_predicate_but_not_in_a_repeat(
self,
):
"""
Test relative path expansion using xpath without current() if reference path is inside a predicate and not inside a repeat
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | xml-external | item | | |
| | calculate | pos1 | | position(..) |
| | calculate | item1 | | instance('item')/root/item[index=${pos1}]/label |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate="instance('item')/root/item[index= /data/pos1 ]/label" nodeset="/data/item1" type="string"/>""" # noqa pylint: disable=line-too-long
],
)

def test_relative_path_expansion_not_using_current_if_reference_path_is_predicate_but_not_part_of_primary_instance(
self,
):
"""
Test relative path expansion using xpath without current() if reference path is inside a predicate but not part of the primary instance
"""
xlsform_md = """
| survey | | | | |
| | type | name | label | calculation |
| | xml-external | item | | |
| | begin repeat | rep1 | | |
| | calculate | pos2 | | 1 |
| | calculate | item2 | | ${rep1}[number(${pos2})]/label |
| | end repeat | | | |
"""
self.assertPyxformXform(
name="data",
md=xlsform_md,
xml__contains=[
"""<bind calculate=" /data/rep1 [number( ../pos2 )]/label" nodeset="/data/rep1/item2" type="string"/>""" # noqa pylint: disable=line-too-long
],
)