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

Change error message if section name is equal to form name #511

Merged
merged 4 commits into from
Jan 14, 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
8 changes: 8 additions & 0 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,16 @@ def validate(self):
self._validate_uniqueness_of_section_names()

def _validate_uniqueness_of_section_names(self):
root_node_name = self.name
section_names = []
for element in self.iter_descendants():
if not isinstance(element, Survey) and element.name == root_node_name:
raise PyXFormError(
'The name "%s" is the same as the form name. '
"Use a different section name "
'(or change the form name in the "name" column of the settings sheet).'
% element.name
)
if isinstance(element, Section):
if element.name in section_names:
raise PyXFormError(
Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests/builder_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_uniqueness_of_section_names(self):

def setUp(self):
self.this_directory = os.path.dirname(__file__)
survey_out = Survey(name="age", sms_keyword="age", type="survey")
survey_out = Survey(name="survey_age", sms_keyword="age", type="survey")
question = InputQuestion(name="age")
question.type = "integer"
question.label = "How old are you?"
Expand Down
17 changes: 17 additions & 0 deletions pyxform/tests_v1/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ def test_choice_list_without_duplicates_is_successful(self):
</select1>
"""
self.assertPyxformXform(md=md, xml__contains=[expected])

def test_duplicate_form_name_in_section_name(self):
"""
Ensure that the section name cannot be the same as form name
"""
self.assertPyxformXform(
name="foo",
md="""
| Survey | | | |
| | Type | Name | Label |
| | begin group | foo | A group |
| | text | a | Enter text |
| | end group | | |
""",
errored=True,
error__contains=['The name "foo" is the same as the form name'],
)