Skip to content

Commit

Permalink
- added test.util.test_xml module
Browse files Browse the repository at this point in the history
It contains testcases for the util.xml module.
  • Loading branch information
marcus-h committed Nov 11, 2014
1 parent 1006f10 commit 0225491
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/suite.py
Expand Up @@ -15,6 +15,7 @@
from test.wc import test_convert
from test.util import test_xpath
from test.util import test_cpio
from test.util import test_xml
from test.cli.util import test_shell


Expand All @@ -34,6 +35,7 @@ def additional_tests():
suite.addTests(test_convert.suite())
suite.addTests(test_xpath.suite())
suite.addTests(test_cpio.suite())
suite.addTests(test_xml.suite())
suite.addTests(test_shell.suite())
return suite

Expand Down
90 changes: 90 additions & 0 deletions test/util/test_xml.py
@@ -0,0 +1,90 @@
import unittest
from collections import Sequence

from osc2.util.xml import fromstring


def suite():
return unittest.makeSuite(TestXML)


class TestXML(unittest.TestCase):
def setUp(self):
self.xml = fromstring(
"""
<root>
<foo>
<bar name="xyz">
<foo/>
</bar>
<bar/>
<bar/>
</foo>
<foo/>
</root>
"""
)

def test_find(self):
"""Find single element (child)"""
elm = self.xml.find('foo')
self.assertTrue(elm is not None)
self.assertFalse(isinstance(elm, Sequence))

def test_find_xpath(self):
"""Find single element using xpath"""
elm = self.xml.find('//bar')
self.assertTrue(elm is not None)
self.assertFalse(isinstance(elm, Sequence))
self.assertEqual(elm.get('name'), 'xyz')

def test_find_result_xpath(self):
"""Call find with an xpath expr on the result returned by find"""
elm = self.xml.find('//bar')
self.assertTrue(elm is not None)
elm = elm.find('//foo')
self.assertTrue(elm is not None)

def test_find_nonexistent(self):
"""Try to find a nonexistent element"""
elm = self.xml.find('nonexistent')
self.assertTrue(elm is None)

def test_find_arbitrary_xpath(self):
"""Find also takes an aribtrary xpath"""
# this usage is discouraged, find should only
# be used to return an element
data = self.xml.find('2 + 3')
self.assertEqual(data, 5.0)

def test_findall(self):
"""Test findall"""
elms = self.xml.findall('foo')
self.assertTrue(isinstance(elms, Sequence))
self.assertEqual(len(elms), 2)

def test_findall_xpath(self):
"""Test findall with xpath"""
elms = self.xml.findall('//bar')
self.assertTrue(isinstance(elms, Sequence))
self.assertEqual(len(elms), 3)

def test_findall_nonexistent(self):
"""Test findall with nonexistent element"""
elms = self.xml.findall('nonexistent')
self.assertTrue(isinstance(elms, Sequence))
self.assertEqual(len(elms), 0)

def test_findall_arbitrary_xpath(self):
"""findall also takes an aribtrary xpath"""
# this usage is discouraged, findall should only
# be used to return a list of elements
data = self.xml.findall('2 + 3')
self.assertEqual(data, 5.0)

def test_iterfind(self):
"""iterfind is not overriden (the default does not support an xpath)"""
self.assertRaises(SyntaxError, self.xml.iterfind, '//foo')

if __name__ == '__main__':
unittest.main()

0 comments on commit 0225491

Please sign in to comment.