# Copyright 2009-2010 ITA Software, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from lxml import etree from twisted.trial import unittest from nagcat import errors, filters class XPathTestCase(unittest.TestCase): example = """ Test XML
This has been a test

Text #1

Text #2

""" def testBasic(self): f = filters.Filter(object(), "xpath://div/text()") self.assertEquals(f.filter(self.example), "This has been a test") def testMissing(self): f = filters.Filter(object(), "xpath://span/text()") self.assertIsInstance(f.filter(self.example), errors.Failure) def testDefault(self): f = filters.Filter(object(), "xpath[none]://span/text()") self.assertEquals(f.filter(self.example), "none") def testBad(self): f = filters.Filter(object(), "xpath://span/text()") self.assertIsInstance(f.filter(""), errors.Failure) def testXML(self): f = filters.Filter(object(), "xpath://title") self.assertEquals(f.filter(self.example), "Test XML") def testMultiXML(self): f = filters.Filter(object(), "xpath://p") self.assertEquals(f.filter(self.example), "

Text #1

\n

Text #2

") class XSLTTestCase(unittest.TestCase): # example swiped from wikipedia raw_xml = """ John Smith Morka Ismincius """ raw_xslt = """ """ result = ('\n' ' John\n' ' Morka\n' '\n') def testBasic(self): f = filters.Filter(object(), "xslt:%s" % self.raw_xslt) self.assertEquals(str(f.filter(self.raw_xml)), self.result) def testPath(self): path = os.path.abspath(self.mktemp()) fd = open(path, "w") fd.write(self.raw_xslt) fd.close() f = filters.Filter(object(), "xslt:%s" % path) self.assertEquals(str(f.filter(self.raw_xml)), self.result) def testBadXSLTXML(self): self.assertRaises(errors.InitError, filters.Filter, object(), "xslt:blah") def testBadXSLT(self): self.assertRaises(errors.InitError, filters.Filter, object(), "xslt:") def testBadInputXML(self): f = filters.Filter(object(), "xslt:%s" % self.raw_xslt) self.assertIsInstance(f.filter("blah"), errors.Failure) class HTMLTestCase(unittest.TestCase): example = """ Test HTML
This has been a test

Text #1

Text #2

""" expect = """ Test HTML
This has been a test

Text #1

Text #2

""" def testBasic(self): f = filters.Filter(object(), "html") xml = f.filter(self.example) self.assertIsInstance(xml, str) self.assertEqualsXML(xml, self.expect) def assertEqualsXML(self, result, expect): # Parse the xml, strip white space, and convert back # this allows us to compare if they are logically equal parser = etree.XMLParser(remove_blank_text=True) result = etree.tostring(etree.XML(result, parser)) expect = etree.tostring(etree.XML(expect, parser)) self.assertEquals(result, expect)