Skip to content

Commit

Permalink
tests/unit/test_xapidb_filter.py: Update/Prepare testcase for Python3 (
Browse files Browse the repository at this point in the history
…#30)

Thanks to Christian Lindig and GeraldEV for their review comments!

Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl authored Jan 3, 2024
1 parent b2190a5 commit adc1fc4
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tests/unit/test_xapidb_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""tests/unit/test_xapidb_filter.py: Ensure that the xen-bugtool.DBFilter() filters the XAPI DB properly"""
# This uses the deprecated imp module because it has to run with Python2.7 for now:
import os
import sys
import xml.dom.minidom
import xml.etree.ElementTree as ET

import pytest

Expand Down Expand Up @@ -55,9 +55,33 @@
"""


def assert_xml_element_trees_equiv(a, b):
"""Assert that the contents of two XML ElementTrees are equivalent (recursive)"""
# Check the XML tag
assert a.tag == b.tag
# Check the XML text
assert (a.text or "").strip() == (b.text or "").strip()
# Check the XML tail
assert (a.tail or "").strip() == (b.tail or "").strip()
# Check the XML attributes
assert a.attrib.items() == b.attrib.items()
# Compare the number of child nodes
assert len(list(a)) == len(list(b))
# Recursively repeat for all child nodes (expect same order of child nodes):
for achild, bchild in zip(list(a), list(b)):
assert_xml_element_trees_equiv(achild, bchild)


@pytest.mark.skipif(sys.version_info >= (3, 0), reason="requires python2")
def test_xapi_database_filter(bugtool):
"""Assert that bugtool.DBFilter().output() filters the xAPI database as expected"""

filtered = bugtool.DBFilter(original).output()
assert xml.dom.minidom.parseString(filtered).toprettyxml(indent=" ") == expected

# Works for Python2 equally, so we can use it to check against Python2/3 regressions:
assert_xml_element_trees_equiv(ET.fromstring(filtered), ET.fromstring(expected))

# Double-check with parseString(): Its output will differ between Py2/Py3
# though, so we will use it for one language version at a time:
if sys.version_info < (3, 0):
assert xml.dom.minidom.parseString(filtered).toprettyxml(indent=" ") == expected

0 comments on commit adc1fc4

Please sign in to comment.