test_parser_xml.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. import unittest
  3. from odml.tools import xmlparser
  4. from odml.tools.parser_utils import ParserException, InvalidVersionException
  5. from .util import TEST_RESOURCES_DIR as RES_DIR
  6. class TestXMLParser(unittest.TestCase):
  7. def setUp(self):
  8. self.base_path = RES_DIR
  9. self.xml_reader = xmlparser.XMLReader()
  10. self.xml_reader_ignore = xmlparser.XMLReader(ignore_errors=True)
  11. def test_invalid_root(self):
  12. filename = "invalid_root.xml"
  13. message = "Expecting <odML>"
  14. with self.assertRaises(ParserException) as exc:
  15. _ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
  16. self.assertIn(message, str(exc.exception))
  17. def test_missing_version(self):
  18. filename = "missing_version.xml"
  19. message = "Could not find format version attribute"
  20. with self.assertRaises(ParserException) as exc:
  21. _ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
  22. self.assertIn(message, str(exc.exception))
  23. def test_invalid_version(self):
  24. filename = "invalid_version.xml"
  25. with self.assertRaises(InvalidVersionException):
  26. _ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
  27. def test_ignore_errors(self):
  28. filename = "ignore_errors.xml"
  29. with self.assertRaises(ParserException):
  30. _ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
  31. doc = self.xml_reader_ignore.from_file(os.path.join(self.base_path, filename))
  32. doc.pprint()