comparison env/lib/python3.9/site-packages/galaxy/tool_util/verify/asserts/xml.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import re
2
3 from galaxy.util import (
4 parse_xml_string,
5 unicodify,
6 )
7
8
9 # Helper functions used to work with XML output.
10 def to_xml(output):
11 return parse_xml_string(output)
12
13
14 def xml_find_text(output, path):
15 xml = to_xml(output)
16 text = xml.findtext(path)
17 return text
18
19
20 def xml_find(output, path):
21 xml = to_xml(output)
22 return xml.find(path)
23
24
25 def assert_is_valid_xml(output):
26 """ Simple assertion that just verifies the specified output
27 is valid XML."""
28 try:
29 to_xml(output)
30 except Exception as e:
31 # TODO: Narrow caught exception to just parsing failure
32 raise AssertionError("Expected valid XML, but could not parse output. %s" % unicodify(e))
33
34
35 def assert_has_element_with_path(output, path):
36 """ Asserts the specified output has at least one XML element with a
37 path matching the specified path argument. Valid paths are the
38 simplified subsets of XPath implemented by lxml.etree;
39 http://effbot.org/zone/element-xpath.htm for more information."""
40 if xml_find(output, path) is None:
41 errmsg = "Expected to find XML element matching expression %s, not such match was found." % path
42 raise AssertionError(errmsg)
43
44
45 def assert_has_n_elements_with_path(output, path, n):
46 """ Asserts the specified output has exactly n elements matching the
47 path specified."""
48 xml = to_xml(output)
49 n = int(n)
50 num_elements = len(xml.findall(path))
51 if num_elements != n:
52 errmsg = "Expected to find %d elements with path %s, but %d were found." % (n, path, num_elements)
53 raise AssertionError(errmsg)
54
55
56 def assert_element_text_matches(output, path, expression):
57 """ Asserts the text of the first element matching the specified
58 path matches the specified regular expression."""
59 text = xml_find_text(output, path)
60 if re.match(expression, text) is None:
61 errmsg = f"Expected element with path '{path}' to contain text matching '{expression}', instead text '{text}' was found."
62 raise AssertionError(errmsg)
63
64
65 def assert_element_text_is(output, path, text):
66 """ Asserts the text of the first element matching the specified
67 path matches exactly the specified text. """
68 assert_element_text_matches(output, path, re.escape(text))
69
70
71 def assert_attribute_matches(output, path, attribute, expression):
72 """ Asserts the specified attribute of the first element matching
73 the specified path matches the specified regular expression."""
74 xml = xml_find(output, path)
75 attribute_value = xml.attrib[attribute]
76 if re.match(expression, attribute_value) is None:
77 errmsg = f"Expected attribute '{attribute}' on element with path '{path}' to match '{expression}', instead attribute value was '{attribute_value}'."
78 raise AssertionError(errmsg)
79
80
81 def assert_attribute_is(output, path, attribute, text):
82 """ Asserts the specified attribute of the first element matching
83 the specified path matches exactly the specified text."""
84 assert_attribute_matches(output, path, attribute, re.escape(text))
85
86
87 def assert_element_text(output, path, verify_assertions_function, children):
88 """ Recursively checks the specified assertions against the text of
89 the first element matching the specified path."""
90 text = xml_find_text(output, path)
91 verify_assertions_function(text, children)