test_section_integration.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """
  2. This file tests proper creation, saving and loading
  3. of odML Sections 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 TestSectionIntegration(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. # Test correct save and load of generated id.
  40. sec_name = "empty_id"
  41. sec_type = "type"
  42. sec = odml.Section(name=sec_name, type=sec_type, parent=self.doc)
  43. jdoc, xdoc, ydoc = self.save_load()
  44. self.assertEqual(jdoc.sections[sec_name].oid, sec.oid)
  45. self.assertEqual(xdoc.sections[sec_name].oid, sec.oid)
  46. self.assertEqual(ydoc.sections[sec_name].oid, sec.oid)
  47. # Test correct save and load of assigned id.
  48. sec_name = "assigned_id"
  49. assigned_id = "79b613eb-a256-46bf-84f6-207df465b8f7"
  50. _ = odml.Section(name=sec_name, oid=assigned_id, type=sec_type, parent=self.doc)
  51. jdoc, xdoc, ydoc = self.save_load()
  52. self.assertEqual(jdoc.sections[sec_name].oid, assigned_id)
  53. self.assertEqual(xdoc.sections[sec_name].oid, assigned_id)
  54. self.assertEqual(ydoc.sections[sec_name].oid, assigned_id)
  55. def test_simple_attributes(self):
  56. """
  57. This test checks correct writing and loading of 'simple'
  58. Section format attributes meaning all attributes that
  59. do not require any special handling when they are set.
  60. """
  61. s_name = "section"
  62. s_type = "mellow"
  63. s_repo = "unresolvable"
  64. s_ref = "4 8 15 16 23 42"
  65. s_def = "undefined"
  66. _ = odml.Section(name=s_name, type=s_type, repository=s_repo,
  67. reference=s_ref, definition=s_def, parent=self.doc)
  68. jdoc, xdoc, ydoc = self.save_load()
  69. # Test correct JSON save and load.
  70. jsec = jdoc.sections[s_name]
  71. self.assertEqual(jsec.name, s_name)
  72. self.assertEqual(jsec.type, s_type)
  73. self.assertEqual(jsec.repository, s_repo)
  74. self.assertEqual(jsec.reference, s_ref)
  75. self.assertEqual(jsec.definition, s_def)
  76. # Test correct XML save and load.
  77. xsec = xdoc.sections[s_name]
  78. self.assertEqual(xsec.name, s_name)
  79. self.assertEqual(xsec.type, s_type)
  80. self.assertEqual(xsec.repository, s_repo)
  81. self.assertEqual(xsec.reference, s_ref)
  82. self.assertEqual(xsec.definition, s_def)
  83. # Test correct YAML save and load.
  84. ysec = ydoc.sections[s_name]
  85. self.assertEqual(ysec.name, s_name)
  86. self.assertEqual(ysec.type, s_type)
  87. self.assertEqual(ysec.repository, s_repo)
  88. self.assertEqual(ysec.reference, s_ref)
  89. self.assertEqual(ysec.definition, s_def)
  90. def test_children(self):
  91. """
  92. This test checks correct writing and loading of Section and Property
  93. children of a Section.
  94. """
  95. s_type = "type"
  96. root = odml.Section(name="root", type=s_type, parent=self.doc)
  97. # Lvl 1 child Sections
  98. sec_lvl_11 = odml.Section(name="sec_11", type=s_type, parent=root)
  99. _ = odml.Section(name="sec_12", type=s_type, parent=root)
  100. # Lvl 1 child Properties
  101. _ = odml.Property(name="prop_11", parent=root)
  102. _ = odml.Property(name="prop_12", parent=root)
  103. # Lvl 2 child Sections
  104. sec_lvl_21 = odml.Section(name="sec_21", type=s_type, parent=sec_lvl_11)
  105. _ = odml.Section(name="sec_22", type=s_type, parent=sec_lvl_11)
  106. _ = odml.Section(name="sec_23", type=s_type, parent=sec_lvl_11)
  107. # Lvl 2 child Properties
  108. _ = odml.Property(name="prop_21", parent=sec_lvl_11)
  109. _ = odml.Property(name="prop_22", parent=sec_lvl_11)
  110. _ = odml.Property(name="prop_23", parent=sec_lvl_11)
  111. # Lvl 3 child Sections
  112. _ = odml.Section(name="sec_31", type=s_type, parent=sec_lvl_21)
  113. _ = odml.Section(name="sec_32", type=s_type, parent=sec_lvl_21)
  114. _ = odml.Section(name="sec_33", type=s_type, parent=sec_lvl_21)
  115. _ = odml.Section(name="sec_34", type=s_type, parent=sec_lvl_21)
  116. # Lvl 3 child Properties
  117. _ = odml.Property(name="prop_31", parent=sec_lvl_21)
  118. _ = odml.Property(name="prop_32", parent=sec_lvl_21)
  119. _ = odml.Property(name="prop_33", parent=sec_lvl_21)
  120. _ = odml.Property(name="prop_34", parent=sec_lvl_21)
  121. jdoc, xdoc, ydoc = self.save_load()
  122. # Test correct JSON save and load.
  123. jsec = jdoc.sections[root.name]
  124. self.assertEqual(len(jsec.sections), 2)
  125. self.assertEqual(len(jsec.properties), 2)
  126. jsec_lvl_1 = jsec[sec_lvl_11.name]
  127. self.assertEqual(len(jsec_lvl_1.sections), 3)
  128. self.assertEqual(len(jsec_lvl_1.properties), 3)
  129. jsec_lvl_2 = jsec_lvl_1[sec_lvl_21.name]
  130. self.assertEqual(len(jsec_lvl_2.sections), 4)
  131. self.assertEqual(len(jsec_lvl_2.properties), 4)
  132. # Test correct XML save and load.
  133. xsec = xdoc.sections[root.name]
  134. self.assertEqual(len(xsec.sections), 2)
  135. self.assertEqual(len(xsec.properties), 2)
  136. xsec_lvl_1 = xsec[sec_lvl_11.name]
  137. self.assertEqual(len(xsec_lvl_1.sections), 3)
  138. self.assertEqual(len(xsec_lvl_1.properties), 3)
  139. xsec_lvl_2 = xsec_lvl_1[sec_lvl_21.name]
  140. self.assertEqual(len(xsec_lvl_2.sections), 4)
  141. self.assertEqual(len(xsec_lvl_2.properties), 4)
  142. # Test correct YAML save and load.
  143. ysec = ydoc.sections[root.name]
  144. self.assertEqual(len(ysec.sections), 2)
  145. self.assertEqual(len(ysec.properties), 2)
  146. ysec_lvl_1 = ysec[sec_lvl_11.name]
  147. self.assertEqual(len(ysec_lvl_1.sections), 3)
  148. self.assertEqual(len(ysec_lvl_1.properties), 3)
  149. ysec_lvl_2 = ysec_lvl_1[sec_lvl_21.name]
  150. self.assertEqual(len(ysec_lvl_2.sections), 4)
  151. self.assertEqual(len(ysec_lvl_2.properties), 4)
  152. def _test_cardinality_load(self, obj_attribute, doc, card_dict,
  153. sec_empty, sec_max, sec_min, sec_full):
  154. """
  155. Tests the basic set of both Section properties and sub-sections cardinality.
  156. :param obj_attribute: string with the cardinality attribute that is supposed to be tested.
  157. Should be either 'prop_cardinality' or 'sec_cardinality'.
  158. :param doc: loaded odml Document to be tested.
  159. :param card_dict: dictionary containing cardinality conditions mapped to the values that
  160. should have been restored.
  161. """
  162. oat = obj_attribute
  163. sec = doc.sections[sec_empty]
  164. self.assertIsNone(getattr(sec, oat))
  165. sec = doc.sections[sec_max]
  166. self.assertEqual(getattr(sec, oat), card_dict[sec_max])
  167. sec = doc.sections[sec_min]
  168. self.assertEqual(getattr(sec, oat), card_dict[sec_min])
  169. sec = doc.sections[sec_full]
  170. self.assertEqual(getattr(sec, oat), card_dict[sec_full])
  171. def test_prop_cardinality(self):
  172. """
  173. Test saving and loading of Section properties cardinality variants to
  174. and from all supported file formats.
  175. """
  176. doc = odml.Document()
  177. sec_empty = "card_empty"
  178. sec_max = "card_max"
  179. sec_min = "card_min"
  180. sec_full = "card_full"
  181. card_dict = {
  182. sec_empty: None,
  183. sec_max: (None, 10),
  184. sec_min: (2, None),
  185. sec_full: (1, 5)
  186. }
  187. _ = odml.Section(name=sec_empty, type="test", parent=doc)
  188. _ = odml.Section(name=sec_max, prop_cardinality=card_dict[sec_max], type="test", parent=doc)
  189. _ = odml.Section(name=sec_min, prop_cardinality=card_dict[sec_min], type="test", parent=doc)
  190. _ = odml.Section(name=sec_full, prop_cardinality=card_dict[sec_full],
  191. type="test", parent=doc)
  192. # Test saving to and loading from an XML file
  193. odml.save(doc, self.xml_file)
  194. xml_doc = odml.load(self.xml_file)
  195. self._test_cardinality_load("prop_cardinality", xml_doc, card_dict,
  196. sec_empty, sec_max, sec_min, sec_full)
  197. # Test saving to and loading from a JSON file
  198. odml.save(doc, self.json_file, "JSON")
  199. json_doc = odml.load(self.json_file, "JSON")
  200. self._test_cardinality_load("prop_cardinality", json_doc, card_dict,
  201. sec_empty, sec_max, sec_min, sec_full)
  202. # Test saving to and loading from a YAML file
  203. odml.save(doc, self.yaml_file, "YAML")
  204. yaml_doc = odml.load(self.yaml_file, "YAML")
  205. self._test_cardinality_load("prop_cardinality", yaml_doc, card_dict,
  206. sec_empty, sec_max, sec_min, sec_full)
  207. def test_sec_cardinality(self):
  208. """
  209. Test saving and loading of Section sections cardinality variants to
  210. and from all supported file formats.
  211. """
  212. doc = odml.Document()
  213. sec_empty = "card_empty"
  214. sec_max = "card_max"
  215. sec_min = "card_min"
  216. sec_full = "card_full"
  217. card_dict = {
  218. sec_empty: None,
  219. sec_max: (None, 10),
  220. sec_min: (2, None),
  221. sec_full: (1, 5)
  222. }
  223. _ = odml.Section(name=sec_empty, type="test", parent=doc)
  224. _ = odml.Section(name=sec_max, sec_cardinality=card_dict[sec_max], type="test", parent=doc)
  225. _ = odml.Section(name=sec_min, sec_cardinality=card_dict[sec_min], type="test", parent=doc)
  226. _ = odml.Section(name=sec_full, sec_cardinality=card_dict[sec_full],
  227. type="test", parent=doc)
  228. # Test saving to and loading from an XML file
  229. odml.save(doc, self.xml_file)
  230. xml_doc = odml.load(self.xml_file)
  231. self._test_cardinality_load("sec_cardinality", xml_doc, card_dict,
  232. sec_empty, sec_max, sec_min, sec_full)
  233. # Test saving to and loading from a JSON file
  234. odml.save(doc, self.json_file, "JSON")
  235. json_doc = odml.load(self.json_file, "JSON")
  236. self._test_cardinality_load("sec_cardinality", json_doc, card_dict,
  237. sec_empty, sec_max, sec_min, sec_full)
  238. # Test saving to and loading from a YAML file
  239. odml.save(doc, self.yaml_file, "YAML")
  240. yaml_doc = odml.load(self.yaml_file, "YAML")
  241. self._test_cardinality_load("sec_cardinality", yaml_doc, card_dict,
  242. sec_empty, sec_max, sec_min, sec_full)
  243. def test_link(self):
  244. pass
  245. def test_include(self):
  246. pass