From 53f8eb67378f6a8054cb107e72b094f070d40c83 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 5 Dec 2013 09:58:18 -0500 Subject: Tools: new Augeas driver --- testsuite/common.py | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'testsuite/common.py') diff --git a/testsuite/common.py b/testsuite/common.py index e26d0be61..536b11cbd 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -13,6 +13,7 @@ import re import sys import codecs import unittest +import lxml.etree from mock import patch, MagicMock, _patch, DEFAULT from Bcfg2.Compat import wraps @@ -262,24 +263,43 @@ class Bcfg2TestCase(unittest.TestCase): "%s is not less than or equal to %s") def assertXMLEqual(self, el1, el2, msg=None): - """ Test that the two XML trees given are equal. Both - elements and all children are expected to have ``name`` - attributes. """ - self.assertEqual(el1.tag, el2.tag, msg=msg) - self.assertEqual(el1.text, el2.text, msg=msg) - self.assertItemsEqual(el1.attrib.items(), el2.attrib.items(), msg=msg) + """ Test that the two XML trees given are equal. """ + if msg is None: + msg = "XML trees are not equal: %s" + else: + msg += ": %s" + fullmsg = msg + "\nFirst: %s" % lxml.etree.tostring(el1) + \ + "\nSecond: %s" % lxml.etree.tostring(el2) + + self.assertEqual(el1.tag, el2.tag, msg=fullmsg % "Tags differ") + if el1.text is not None and el2.text is not None: + self.assertEqual(el1.text.strip(), el2.text.strip(), + msg=fullmsg % "Text content differs") + else: + self.assertEqual(el1.text, el2.text, + msg=fullmsg % "Text content differs") + self.assertItemsEqual(el1.attrib.items(), el2.attrib.items(), + msg=fullmsg % "Attributes differ") self.assertEqual(len(el1.getchildren()), - len(el2.getchildren())) + len(el2.getchildren()), + msg=fullmsg % "Different numbers of children") + matched = [] for child1 in el1.getchildren(): - cname = child1.get("name") - self.assertIsNotNone(cname, - msg="Element %s has no 'name' attribute" % - child1.tag) - children2 = el2.xpath("%s[@name='%s']" % (child1.tag, cname)) - self.assertEqual(len(children2), 1, - msg="More than one %s element named %s" % \ - (child1.tag, cname)) - self.assertXMLEqual(child1, children2[0], msg=msg) + for child2 in el2.xpath(child1.tag): + if child2 in matched: + continue + try: + self.assertXMLEqual(child1, child2) + matched.append(child2) + break + except AssertionError: + continue + else: + assert False, \ + fullmsg % ("Element %s is missing from second" % + lxml.etree.tostring(child1)) + self.assertItemsEqual(el2.getchildren(), matched, + msg=fullmsg % "Second has extra element(s)") class DBModelTestCase(Bcfg2TestCase): @@ -394,4 +414,3 @@ try: re_type = re._pattern_type except AttributeError: re_type = type(re.compile("")) - -- cgit v1.2.3-1-g7c22 From 9a83b7b25a2aa8fe48e291d6918cacb99750ac5a Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 5 Dec 2013 15:22:52 -0500 Subject: testsuite: fixed local implementation of assertItemsEqual --- testsuite/common.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'testsuite/common.py') diff --git a/testsuite/common.py b/testsuite/common.py index 536b11cbd..7471795a6 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -223,8 +223,11 @@ class Bcfg2TestCase(unittest.TestCase): lines = ['First has %d, Second has %d: %r' % diff for diff in differences] diffMsg = '\n'.join(lines) - standardMsg = self._truncateMessage(standardMsg, diffMsg) - msg = self._formatMessage(msg, standardMsg) + standardMsg += diffMsg + if msg is None: + msg = standardMsg + else: + msg = "%s : %s" % (standardMsg, msg) self.fail(msg) if not hasattr(unittest.TestCase, "assertRegexpMatches"): -- cgit v1.2.3-1-g7c22