Skip to content

Commit

Permalink
Add flake8 configuration for code formatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Apr 21, 2022
1 parent 3551e91 commit eb75502
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length = 88
...
select = C,E,F,W,B,B950
extend-ignore = E203, E501
93 changes: 55 additions & 38 deletions onadata/libs/test_utils/pyxform_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,29 +157,36 @@ def assertPyxformXform(self, **kwargs):
one or many of these string "matchers":
* xml__contains: an array of strings which exist in the
resulting xml. [xml|model|instance|itext]_excludes are also supported.
resulting xml. [xml|model|instance|itext]_excludes are also
supported.
* error__contains: a list of strings which should exist in the error
* error__not_contains: a list of strings which should not exist in the error
* odk_validate_error__contains: list of strings; run_odk_validate must be set
* warnings__contains: a list of strings which should exist in the warnings
* warnings__not_contains: a list of strings which should not exist in the warnings
* error__not_contains: a list of strings which should not exist in
the error
* odk_validate_error__contains: list of strings; run_odk_validate
must be set
* warnings__contains: a list of strings which should exist in the
warnings
* warnings__not_contains: a list of strings which should not
exist in the warnings
* warnings_count: the number of expected warning messages
* xml__excludes: an array of strings which should not exist in the resulting
xml. [xml|model|instance|itext]_excludes are also supported.
* xml__xpath_exact: A list of tuples where the first tuple element is an XPath
expression and the second tuple element is a set of exact string match
results that are expected.
* xml__xpath_count: A list of tuples where the first tuple element is an XPath
expression and the second tuple element is the integer number of match
results that are expected.
* xml__xpath_match: A list of XPath expression strings for which exactly one
match result each is expected. Effectively a shortcut for
xml__xpath_count with a count of 1.
For each of the xpath_* matchers above, if the XPath expression is looking for an
element in the 'default' namespace (xforms) then use an 'x' namespace prefix for
the element. For example to find input nodes in the body: ".//h:body/x:input".
This 'x' prefix is not required for attributes. When writing a xpath_* test, use
* xml__excludes: an array of strings which should not exist in the
resulting xml. [xml|model|instance|itext]_excludes are also
supported.
* xml__xpath_exact: A list of tuples where the first tuple element
is an XPath expression and the second tuple element is a
set of exact string match results that are expected.
* xml__xpath_count: A list of tuples where the first tuple element
is_an XPath expression and the second tuple element is the
integer number of match results that are expected.
* xml__xpath_match: A list of XPath expression strings for which
exactly one match result each is expected. Effectively a
shortcut for xml__xpath_count with a count of 1.
For each of the xpath_* matchers above, if the XPath expression
is looking for an element in the 'default' namespace (xforms) then
use an 'x' namespace prefix for the element. For example to find
input nodes in the body: ".//h:body/x:input". This 'x' prefix is
not required for attributes. When writing a xpath_* test, use
debug=True to show the XPath match results.
optional other parameters passed to pyxform:
Expand All @@ -191,8 +198,9 @@ def assertPyxformXform(self, **kwargs):
* run_odk_validate: (bool) when True, runs ODK Validate process
Default value = False because it slows down tests
* warnings: (list) a list to use for storing warnings for inspection.
* debug: (bool) when True, log details of the test to stdout. Details include
the input survey markdown, the XML document, XPath match strings.
* debug: (bool) when True, log details of the test to stdout.
Details include the input survey markdown, the XML document,
XPath match strings.
"""
debug = kwargs.get("debug", False)
expecting_invalid_survey = kwargs.get("errored", False)
Expand Down Expand Up @@ -436,7 +444,9 @@ def assertNotContains(self, content, text, msg_prefix=""):
)

self.assertEqual(
real_count, 0, msg_prefix + "Response should not contain %s" % text_repr
real_count,
0,
msg_prefix + "Response should not contain %s" % text_repr,
)

def assert_xpath_exact(
Expand Down Expand Up @@ -498,23 +508,27 @@ def assert_xpath_count(

def reorder_attributes(root):
"""
Forces alphabetical ordering of all XML attributes to match pre Python 3.8 behavior.
In general, we should not rely on ordering, but changing all the tests is not
realistic at this moment.
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.
NOTE: there's a similar ordering change made in utils.node. This one is also needed because in
assertPyxformXform, the survey is converted to XML and then read back in using ETree.fromstring. This
means that attribute ordering here is based on the attribute representation of xml.etree.ElementTree objects.
In utils.node, it is based on xml.dom.minidom.Element objects. See https://github.com/XLSForm/pyxform/issues/414.
Forces alphabetical ordering of all XML attributes to match pre Python 3.8
behaviour. In general, we should not rely on ordering, but changing all the
tests is not realistic at this moment.
See bottom of https://bit.ly/38docMg and
https://bit.ly/3ODx9iG for more information.
NOTE: there's a similar ordering change made in utils.node. This one is
also needed because in assertPyxformXform, the survey is converted to XML
and then read back in using ETree.fromstring. This means that attribute
ordering here is based on the attribute representation of
xml.etree.ElementTree objects.
In utils.node, it is based on xml.dom.minidom.Element objects.
See https://github.com/XLSForm/pyxform/issues/414.
"""
for el in root.iter():
attrib = el.attrib
if len(attrib) > 1:
# Sort attributes. Attributes are represented as {namespace}name so attributes with explicit
# namespaces will always sort after those without explicit namespaces.
# Sort attributes. Attributes are represented as {namespace}name
# so attributes with explicit namespaces will always sort after
# those without explicit namespaces.
attribs = sorted(attrib.items())
attrib.clear()
attrib.update(attribs)
Expand Down Expand Up @@ -544,7 +558,10 @@ def xpath_clean_result_strings(


def xpath_evaluate(
matcher_context: "MatcherContext", content: "_Element", xpath: str, for_exact=False
matcher_context: "MatcherContext",
content: "_Element",
xpath: str,
for_exact=False,
) -> "Union[Set[_Element], Set[str]]":
"""
Evaluate an XPath and return the results.
Expand Down

0 comments on commit eb75502

Please sign in to comment.