test_xml_writer.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import shutil
  3. import unittest
  4. import odml
  5. from odml.tools.xmlparser import XML_HEADER, EXTERNAL_STYLE_HEADER, \
  6. INFILE_STYLE_HEADER, INFILE_STYLE_TEMPLATE
  7. from odml.tools import XMLWriter
  8. from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
  9. class TestXMLWriter(unittest.TestCase):
  10. def setUp(self):
  11. # Set up test environment
  12. self.xmlfile = os.path.join(RES_DIR, "version_conversion_int.xml")
  13. self.tmp_dir = create_test_dir(__file__)
  14. self.outfile = os.path.join(self.tmp_dir, "xml_writer.xml")
  15. doc = odml.Document()
  16. sec = doc.create_section(name="sec", type="test")
  17. _ = sec.create_property(name="prop", value=['a', 'b', 'c'])
  18. self.doc = doc
  19. self.writer = XMLWriter(doc)
  20. def tearDown(self):
  21. if self.tmp_dir and os.path.exists(self.tmp_dir):
  22. shutil.rmtree(self.tmp_dir)
  23. def test_write_default(self):
  24. self.writer.write_file(self.outfile)
  25. # make sure the file can be read again without errors
  26. doc = odml.load(self.outfile)
  27. self.assertEqual(doc, self.doc)
  28. # test style content in saved file
  29. with open(self.outfile) as test_file:
  30. content = test_file.read()
  31. self.assertIn(XML_HEADER, content)
  32. self.assertIn(EXTERNAL_STYLE_HEADER, content)
  33. def test_write_style_default(self):
  34. self.writer.write_file(self.outfile, local_style=True)
  35. # make sure the file can be read again without errors
  36. doc = odml.load(self.outfile)
  37. self.assertEqual(doc, self.doc)
  38. # test style content in saved file
  39. with open(self.outfile) as test_file:
  40. content = test_file.read()
  41. self.assertIn(XML_HEADER, content)
  42. self.assertIn(INFILE_STYLE_HEADER, content)
  43. self.assertIn(INFILE_STYLE_TEMPLATE, content)
  44. def test_write_style_custom(self):
  45. # template stub just to see if its written properly; will not render anything
  46. cust_tmpl = "<xsl:template></xsl:template>"
  47. self.writer.write_file(self.outfile, local_style=True, custom_template=cust_tmpl)
  48. # make sure the file can be read again without errors
  49. doc = odml.load(self.outfile)
  50. self.assertEqual(doc, self.doc)
  51. # test style content in saved file
  52. with open(self.outfile) as test_file:
  53. content = test_file.read()
  54. self.assertIn(XML_HEADER, content)
  55. self.assertIn(INFILE_STYLE_HEADER, content)
  56. self.assertNotIn(INFILE_STYLE_TEMPLATE, content)
  57. self.assertIn(cust_tmpl, content)
  58. # --- test second possible way to save
  59. self.writer.write_file(self.outfile, local_style=False, custom_template=cust_tmpl)
  60. # make sure the file can be read again without errors
  61. doc = odml.load(self.outfile)
  62. self.assertEqual(doc, self.doc)
  63. # test style content in saved file
  64. with open(self.outfile) as test_file:
  65. content = test_file.read()
  66. self.assertIn(XML_HEADER, content)
  67. self.assertIn(INFILE_STYLE_HEADER, content)
  68. self.assertNotIn(INFILE_STYLE_TEMPLATE, content)
  69. self.assertIn(cust_tmpl, content)