From adc1fc4666505b21fa7d8981e5008f1c2e7e9fd2 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl Date: Wed, 3 Jan 2024 16:37:00 +0100 Subject: [PATCH] tests/unit/test_xapidb_filter.py: Update/Prepare testcase for Python3 (#30) Thanks to Christian Lindig and GeraldEV for their review comments! Signed-off-by: Bernhard Kaindl --- tests/unit/test_xapidb_filter.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_xapidb_filter.py b/tests/unit/test_xapidb_filter.py index 1d9c5364..133d1e14 100644 --- a/tests/unit/test_xapidb_filter.py +++ b/tests/unit/test_xapidb_filter.py @@ -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 @@ -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