Skip to content

Commit

Permalink
Ensure stable ordering of attributes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanokwa committed Jan 6, 2020
1 parent 23f081c commit 5c7c5a0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
16 changes: 14 additions & 2 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,20 @@ def date_stamp(self):
"""Returns a date string with the format of %Y_%m_%d."""
return self._created.strftime("%Y_%m_%d")

def _to_ugly_xml(self):
return '<?xml version="1.0"?>' + self.xml().toxml()
def _to_testable_xml(self):
"""Preserves attribute ordering across all Python versions.
See python_test_case.reorder_attributes for related code.
"""
tree = ETree.fromstring(self.xml().toxml())
for el in tree.iter():
attrib = el.attrib
if len(attrib) > 1:
# adjust attribute order, e.g. by sorting
attribs = sorted(attrib.items())
attrib.clear()
attrib.update(attribs)
return ETree.tostring(tree, encoding="utf-8").decode("utf-8")

def _to_pretty_xml(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions pyxform/tests_v1/pyxform_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ def reorder_attributes(root):
See bottom of https://docs.python.org/3/library/xml.etree.elementtree.html#element-objects and
https://github.com/python/cpython/commit/a3697db0102b9b6747fe36009e42f9b08f0c1ea8 for more information.
See survey._to_testable_xml for related code.
"""
for el in root.iter():
attrib = el.attrib
Expand Down
4 changes: 2 additions & 2 deletions pyxform/tests_v1/test_dynamic_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_handling_dynamic_default(self):
kwargs={"id_string": "id", "name": "dynamic", "title": "some-title"},
autoname=False,
)
survey_xml = survey._to_pretty_xml()
survey_xml = survey._to_testable_xml()

self.assertContains(survey_xml, "<first_name/>", 1)
self.assertContains(survey_xml, "<last_name>not_func$</last_name>", 1)
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_handling_dynamic_default_in_repeat(self):
kwargs={"id_string": "id", "name": "dynamic", "title": "some-title"},
autoname=False,
)
survey_xml = survey._to_pretty_xml()
survey_xml = survey._to_testable_xml()

self.assertContains(survey_xml, "<feeling>not_func$</feeling>", 2)
self.assertContains(survey_xml, "<age/>", 2)
Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests_v1/test_whitespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ def test_values_without_whitespaces_are_processed_successfully(self):

survey = self.md_to_pyxform_survey(md_raw=md)
expected = """<submission action="https://odk.ona.io/random_person/submission" base64RsaPublicKey="MIIB" method="post"/>"""
xml = survey._to_pretty_xml()
xml = survey._to_testable_xml()
self.assertEqual(1, xml.count(expected))
self.assertPyxformXform(md=md, xml__contains=expected, run_odk_validate=True)

0 comments on commit 5c7c5a0

Please sign in to comment.