test_doc_integration.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """
  2. This file tests proper creation, saving and loading
  3. of odML Documents with all supported odML parsers.
  4. """
  5. import os
  6. import shutil
  7. import unittest
  8. import odml
  9. from .util import create_test_dir
  10. class TestDocumentIntegration(unittest.TestCase):
  11. def setUp(self):
  12. # Set up test environment
  13. self.tmp_dir = create_test_dir(__file__)
  14. self.json_file = os.path.join(self.tmp_dir, "test.json")
  15. self.xml_file = os.path.join(self.tmp_dir, "test.xml")
  16. self.yaml_file = os.path.join(self.tmp_dir, "test.yaml")
  17. # Set up odML document stub
  18. doc = odml.Document()
  19. self.doc = 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 save_load(self):
  24. """
  25. Helper method to save and load the current state of the document
  26. with all supported parsers.
  27. :return: jdoc ... document loaded from JSON file
  28. xdoc ... document loaded from XML file
  29. ydoc ... document loaded from YAML file
  30. """
  31. odml.save(self.doc, self.json_file, "JSON")
  32. jdoc = odml.load(self.json_file, "JSON")
  33. odml.save(self.doc, self.xml_file)
  34. xdoc = odml.load(self.xml_file)
  35. odml.save(self.doc, self.yaml_file, "YAML")
  36. ydoc = odml.load(self.yaml_file, "YAML")
  37. return jdoc, xdoc, ydoc
  38. def test_id(self):
  39. """
  40. This test checks the correct writing and loading of
  41. autogenerated and assigned document id.
  42. """
  43. # Test correct save and load of generated id.
  44. jdoc, xdoc, ydoc = self.save_load()
  45. self.assertEqual(jdoc.id, self.doc.id)
  46. self.assertEqual(xdoc.id, self.doc.id)
  47. self.assertEqual(ydoc.id, self.doc.id)
  48. # Test correct save and load of assigned id.
  49. assigned_id = "79b613eb-a256-46bf-84f6-207df465b8f7"
  50. self.doc = odml.Document(oid=assigned_id)
  51. jdoc, xdoc, ydoc = self.save_load()
  52. self.assertEqual(jdoc.id, assigned_id)
  53. self.assertEqual(xdoc.id, assigned_id)
  54. self.assertEqual(ydoc.id, assigned_id)
  55. def test_simple_attributes(self):
  56. """
  57. This test checks correct writing and loading of 'simple'
  58. Document format attributes.
  59. """
  60. author = "HPL"
  61. version = "ver64"
  62. date = "1890-08-20"
  63. repository = "invalid"
  64. self.doc = odml.Document(author, date, version, repository)
  65. jdoc, xdoc, ydoc = self.save_load()
  66. # Test correct JSON save and load.
  67. self.assertEqual(jdoc.author, author)
  68. self.assertEqual(jdoc.version, version)
  69. self.assertEqual(str(jdoc.date), date)
  70. self.assertEqual(jdoc.repository, repository)
  71. # Test correct XML save and load.
  72. self.assertEqual(xdoc.author, author)
  73. self.assertEqual(xdoc.version, version)
  74. self.assertEqual(str(xdoc.date), date)
  75. self.assertEqual(xdoc.repository, repository)
  76. # Test correct YAML save and load.
  77. self.assertEqual(ydoc.author, author)
  78. self.assertEqual(ydoc.version, version)
  79. self.assertEqual(str(ydoc.date), date)
  80. self.assertEqual(ydoc.repository, repository)
  81. def test_children(self):
  82. """
  83. This test checks the correct saving and loading of Section children of a Document.
  84. """
  85. s_type = "type"
  86. # Lvl 1 child Sections
  87. sec_lvl_11 = odml.Section(name="sec_11", type=s_type, parent=self.doc)
  88. _ = odml.Section(name="sec_12", type=s_type, parent=self.doc)
  89. # Lvl 2 child Sections
  90. sec_lvl_21 = odml.Section(name="sec_21", type=s_type, parent=sec_lvl_11)
  91. _ = odml.Section(name="sec_22", type=s_type, parent=sec_lvl_11)
  92. _ = odml.Section(name="sec_23", type=s_type, parent=sec_lvl_11)
  93. # Lvl 2 child Properties
  94. _ = odml.Property(name="prop_21", parent=sec_lvl_11)
  95. _ = odml.Property(name="prop_22", parent=sec_lvl_11)
  96. _ = odml.Property(name="prop_23", parent=sec_lvl_11)
  97. # Lvl 3 child Sections
  98. _ = odml.Section(name="sec_31", type=s_type, parent=sec_lvl_21)
  99. _ = odml.Section(name="sec_32", type=s_type, parent=sec_lvl_21)
  100. _ = odml.Section(name="sec_33", type=s_type, parent=sec_lvl_21)
  101. _ = odml.Section(name="sec_34", type=s_type, parent=sec_lvl_21)
  102. # Lvl 3 child Properties
  103. _ = odml.Property(name="prop_31", parent=sec_lvl_21)
  104. _ = odml.Property(name="prop_32", parent=sec_lvl_21)
  105. _ = odml.Property(name="prop_33", parent=sec_lvl_21)
  106. _ = odml.Property(name="prop_34", parent=sec_lvl_21)
  107. jdoc, xdoc, ydoc = self.save_load()
  108. # Test correct JSON save and load.
  109. self.assertEqual(len(jdoc.sections), 2)
  110. jsec_lvl_1 = jdoc[sec_lvl_11.name]
  111. self.assertEqual(len(jsec_lvl_1.sections), 3)
  112. self.assertEqual(len(jsec_lvl_1.properties), 3)
  113. jsec_lvl_2 = jsec_lvl_1[sec_lvl_21.name]
  114. self.assertEqual(len(jsec_lvl_2.sections), 4)
  115. self.assertEqual(len(jsec_lvl_2.properties), 4)
  116. # Test correct XML save and load.
  117. self.assertEqual(len(xdoc.sections), 2)
  118. xsec_lvl_1 = xdoc[sec_lvl_11.name]
  119. self.assertEqual(len(xsec_lvl_1.sections), 3)
  120. self.assertEqual(len(xsec_lvl_1.properties), 3)
  121. xsec_lvl_2 = xsec_lvl_1[sec_lvl_21.name]
  122. self.assertEqual(len(xsec_lvl_2.sections), 4)
  123. self.assertEqual(len(xsec_lvl_2.properties), 4)
  124. # Test correct YAML save and load.
  125. self.assertEqual(len(ydoc.sections), 2)
  126. ysec_lvl_1 = ydoc[sec_lvl_11.name]
  127. self.assertEqual(len(ysec_lvl_1.sections), 3)
  128. self.assertEqual(len(ysec_lvl_1.properties), 3)
  129. ysec_lvl_2 = ysec_lvl_1[sec_lvl_21.name]
  130. self.assertEqual(len(ysec_lvl_2.sections), 4)
  131. self.assertEqual(len(ysec_lvl_2.properties), 4)