tests: add helper to check XML documents

This commit is contained in:
Benjamin Dauvergne 2016-02-26 13:09:03 +01:00
parent 26ffe9af97
commit c504f7e8db
1 changed files with 15 additions and 0 deletions

15
tests/xml_utils.py Normal file
View File

@ -0,0 +1,15 @@
from lxml import etree as ET
def assert_xml_constraints(content, constraint, namespaces={}):
d = ET.fromstring(content)
def check(constraint, prefix=''):
path, count = constraint[:2]
path = prefix + path
if not count is None:
l = d.xpath(path, namespaces=namespaces)
assert len(l) == count, 'len(xpath(%s)) is not %s but %s' % (path, count, len(l))
if len(constraint) > 2:
for constraint in constraint[2:]:
check(constraint, prefix=path)
check(constraint)