Skip to content

Commit

Permalink
Move util that uses etree to the directory that isn't run on python 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabycourt committed May 1, 2014
1 parent 08cad19 commit 0d0e202
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 111 deletions.
48 changes: 47 additions & 1 deletion devel/pulp/devel/unit/server/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
from xml.etree import ElementTree

from pulp.server.exceptions import PulpCodedValidationException

Expand Down Expand Up @@ -58,4 +59,49 @@ def assert_validation_exception(method, error_codes, *args, **kwargs):
raise AssertionError("The following errors were not specified but were raised: %s" %
str(errors_raised_unexpectedly))
else:
raise AssertionError("A validation exception was not raised")
raise AssertionError("A validation exception was not raised")


def compare_element(source, target):
"""
Utility method to recursively compare two etree elements
:param source: The source element to compare against the target
:type source: xml.etree.ElementTree.Element
:param target: The target element to compare against the source
:type target: xml.etree.ElementTree.Element
:raise AssertionError: if the elements do not match
"""
if not ElementTree.iselement(source):
raise AssertionError("Source is not an element")
if not ElementTree.iselement(target):
raise AssertionError("Target is not an element")

if source.tag != target.tag:
raise AssertionError("elements do not match. Tags are different %s != %s" %
(source.tag, target.tag))

#test keys
source_keys = set(source.keys())
target_keys = set(target.keys())

if source_keys != target_keys:
raise AssertionError("elements do not match. Keys are different")

for key in source_keys:
if source.get(key) != target.get(key):
raise AssertionError("Key values do not match. Value mismatch for key %s: %s != %s" %
(key, source.get(key), target.get(key)))

if source.text != target.text:
raise AssertionError("elements do not match. Text is different\n%s\n%s" % (source.text,
target.text))

#Use the deprecated getchildren method for python 2.6 support
source_children = list(source.getchildren())
target_children = list(target.getchildren())
if len(source_children) != len(target_children):
raise AssertionError("elements do not match. Unequal number of child elements")

for source_child, target_child in zip(source_children, target_children):
compare_element(source_child, target_child)
46 changes: 0 additions & 46 deletions devel/pulp/devel/unit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

import os
from xml.etree import ElementTree


def compare_dict(source, target):
Expand Down Expand Up @@ -47,51 +46,6 @@ def compare_dict(source, target):
{'key': key, 'value1': source[key], 'value2': target[key]})


def compare_element(source, target):
"""
Utility method to recursively compare two etree elements
:param source: The source element to compare against the target
:type source: xml.etree.ElementTree.Element
:param target: The target element to compare against the source
:type target: xml.etree.ElementTree.Element
:raise AssertionError: if the elements do not match
"""
if not ElementTree.iselement(source):
raise AssertionError("Source is not an element")
if not ElementTree.iselement(target):
raise AssertionError("Target is not an element")

if source.tag != target.tag:
raise AssertionError("elements do not match. Tags are different %s != %s" %
(source.tag, target.tag))

#test keys
source_keys = set(source.keys())
target_keys = set(target.keys())

if source_keys != target_keys:
raise AssertionError("elements do not match. Keys are different")

for key in source_keys:
if source.get(key) != target.get(key):
raise AssertionError("Key values do not match. Value mismatch for key %s: %s != %s" %
(key, source.get(key), target.get(key)))

if source.text != target.text:
raise AssertionError("elements do not match. Text is different\n%s\n%s" % (source.text,
target.text))

#Use the deprecated getchildren method for python 2.6 support
source_children = list(source.getchildren())
target_children = list(target.getchildren())
if len(source_children) != len(target_children):
raise AssertionError("elements do not match. Unequal number of child elements")

for source_child, target_child in zip(source_children, target_children):
compare_element(source_child, target_child)


def assert_body_matches_async_task(body, task):
assert body['spawned_tasks'][0]['task_id'] == task.id

Expand Down
65 changes: 65 additions & 0 deletions devel/test/unit/server/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

import unittest
from xml.etree import ElementTree

from mock import Mock

from pulp.common import error_codes
from pulp.devel.unit.server import util
from pulp.devel.unit.server.util import compare_element
from pulp.server.exceptions import PulpCodedValidationException, PulpCodedException


Expand Down Expand Up @@ -67,3 +69,66 @@ def test_raises_validation_exception(self):
mock_method = Mock()
self.assertRaises(AssertionError, util.assert_validation_exception, mock_method,
error_codes=[error_codes.PLP0001])


class TestCompareEtree(unittest.TestCase):

def test_compare_element_equality(self):
source_string = '<foo alpha="bar">some text <baz></baz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(source_string)
compare_element(source, target)

def test_compare_element_inequality_tags(self):
source_string = '<foo></foo>'
target_string = '<bar></bar>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)

def test_compare_element_inequality_text(self):
source_string = '<foo>alpha</foo>'
target_string = '<foo>beta</foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)

def test_compare_element_inequality_keys(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo beta="bar"></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)

def test_compare_element_inequality_values(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)

def test_compare_element_source_not_element(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source_string, target)

def test_compare_element_target_not_element(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
source = ElementTree.fromstring(source_string)
self.assertRaises(AssertionError, compare_element, source, target_string)

def test_compare_element_child_different(self):
source_string = '<foo alpha="bar">some text <baz>qux</baz></foo>'
target_string = '<foo alpha="bar">some text <baz>zap</baz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)

def test_compare_element_child_different_number(self):
source_string = '<foo alpha="bar">some text <baz>qux</baz></foo>'
target_string = '<foo alpha="bar">some text <baz>zap</baz><fuz></fuz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, compare_element, source, target)
64 changes: 0 additions & 64 deletions devel/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import os
import shutil
import tempfile
from xml.etree import ElementTree

from mock import Mock

Expand Down Expand Up @@ -63,69 +62,6 @@ def test_compare_dict_target_not_dict(self):
self.assertRaises(AssertionError, util.compare_dict, source, target)


class TestCompareEtree(unittest.TestCase):

def test_compare_element_equality(self):
source_string = '<foo alpha="bar">some text <baz></baz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(source_string)
util.compare_element(source, target)

def test_compare_element_inequality_tags(self):
source_string = '<foo></foo>'
target_string = '<bar></bar>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)

def test_compare_element_inequality_text(self):
source_string = '<foo>alpha</foo>'
target_string = '<foo>beta</foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)

def test_compare_element_inequality_keys(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo beta="bar"></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)

def test_compare_element_inequality_values(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)

def test_compare_element_source_not_element(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source_string, target)

def test_compare_element_target_not_element(self):
source_string = '<foo alpha="bar"></foo>'
target_string = '<foo alpha="foo"></foo>'
source = ElementTree.fromstring(source_string)
self.assertRaises(AssertionError, util.compare_element, source, target_string)

def test_compare_element_child_different(self):
source_string = '<foo alpha="bar">some text <baz>qux</baz></foo>'
target_string = '<foo alpha="bar">some text <baz>zap</baz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)

def test_compare_element_child_different_number(self):
source_string = '<foo alpha="bar">some text <baz>qux</baz></foo>'
target_string = '<foo alpha="bar">some text <baz>zap</baz><fuz></fuz></foo>'
source = ElementTree.fromstring(source_string)
target = ElementTree.fromstring(target_string)
self.assertRaises(AssertionError, util.compare_element, source, target)


class TestAssertBodyMatchesAsyncTask(unittest.TestCase):

def test_successful_match(self):
Expand Down

0 comments on commit 0d0e202

Please sign in to comment.