test_property_integration.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. """
  2. This file tests proper creation, saving and loading
  3. of odML Properties 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 TestPropertyIntegration(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. _ = odml.Section(name="properties", type="test", parent=doc)
  20. self.doc = doc
  21. def tearDown(self):
  22. if self.tmp_dir and os.path.exists(self.tmp_dir):
  23. shutil.rmtree(self.tmp_dir)
  24. def save_load(self):
  25. """
  26. Helper method to save and load the current state of the document
  27. with all supported parsers.
  28. :return: jdoc ... document loaded from JSON file
  29. xdoc ... document loaded from XML file
  30. ydoc ... document loaded from YAML file
  31. """
  32. odml.save(self.doc, self.json_file, "JSON")
  33. jdoc = odml.load(self.json_file, "JSON")
  34. odml.save(self.doc, self.xml_file)
  35. xdoc = odml.load(self.xml_file)
  36. odml.save(self.doc, self.yaml_file, "YAML")
  37. ydoc = odml.load(self.yaml_file, "YAML")
  38. return jdoc, xdoc, ydoc
  39. def test_id(self):
  40. # Test correct save and load of generated id.
  41. prop_name = "empty_id"
  42. prop = odml.Property(name=prop_name, parent=self.doc.sections[0])
  43. jdoc, xdoc, ydoc = self.save_load()
  44. self.assertEqual(jdoc.sections[0].properties[prop_name].id, prop.id)
  45. self.assertEqual(xdoc.sections[0].properties[prop_name].id, prop.id)
  46. self.assertEqual(ydoc.sections[0].properties[prop_name].id, prop.id)
  47. # Test correct save and load of assigned id.
  48. prop_name = "assigned_id"
  49. assigned_id = "79b613eb-a256-46bf-84f6-207df465b8f7"
  50. _ = odml.Property(name=prop_name, oid=assigned_id,
  51. parent=self.doc.sections[0])
  52. jdoc, xdoc, ydoc = self.save_load()
  53. self.assertEqual(jdoc.sections[0].properties[prop_name].id, assigned_id)
  54. self.assertEqual(xdoc.sections[0].properties[prop_name].id, assigned_id)
  55. self.assertEqual(ydoc.sections[0].properties[prop_name].id, assigned_id)
  56. def test_simple_attributes(self):
  57. """
  58. This test checks correct writing and loading of 'simple'
  59. Property format attributes meaning all attributes that
  60. do not require any special handling when they are set.
  61. Note: the xml reader and writer converts all attribute values
  62. to string whereas json and yaml retain other types like int or
  63. float identity. Since this is currently not relevant, all
  64. attributes are tested as string values.
  65. """
  66. p_name = "propertyName"
  67. p_origin = "from over there"
  68. p_unit = "pears"
  69. p_uncertainty = "+-12"
  70. p_ref = "4 8 15 16 23"
  71. p_def = "an odml test property"
  72. p_dep = "yes"
  73. p_dep_val = "42"
  74. _ = odml.Property(name=p_name, value_origin=p_origin, unit=p_unit,
  75. uncertainty=p_uncertainty, reference=p_ref, definition=p_def,
  76. dependency=p_dep, dependency_value=p_dep_val,
  77. parent=self.doc.sections[0])
  78. jdoc, xdoc, ydoc = self.save_load()
  79. # Test correct JSON save and load.
  80. jprop = jdoc.sections[0].properties[p_name]
  81. self.assertEqual(jprop.name, p_name)
  82. self.assertEqual(jprop.value_origin, p_origin)
  83. self.assertEqual(jprop.unit, p_unit)
  84. self.assertEqual(jprop.uncertainty, p_uncertainty)
  85. self.assertEqual(jprop.reference, p_ref)
  86. self.assertEqual(jprop.definition, p_def)
  87. self.assertEqual(jprop.dependency, p_dep)
  88. self.assertEqual(jprop.dependency_value, p_dep_val)
  89. # Test correct XML save and load.
  90. xprop = xdoc.sections[0].properties[p_name]
  91. self.assertEqual(xprop.name, p_name)
  92. self.assertEqual(xprop.value_origin, p_origin)
  93. self.assertEqual(xprop.unit, p_unit)
  94. self.assertEqual(xprop.uncertainty, p_uncertainty)
  95. self.assertEqual(xprop.reference, p_ref)
  96. self.assertEqual(xprop.definition, p_def)
  97. self.assertEqual(xprop.dependency, p_dep)
  98. self.assertEqual(xprop.dependency_value, p_dep_val)
  99. # Test correct YAML save and load.
  100. yprop = ydoc.sections[0].properties[p_name]
  101. self.assertEqual(yprop.name, p_name)
  102. self.assertEqual(yprop.value_origin, p_origin)
  103. self.assertEqual(yprop.unit, p_unit)
  104. self.assertEqual(yprop.uncertainty, p_uncertainty)
  105. self.assertEqual(yprop.reference, p_ref)
  106. self.assertEqual(yprop.definition, p_def)
  107. self.assertEqual(yprop.dependency, p_dep)
  108. self.assertEqual(yprop.dependency_value, p_dep_val)
  109. def test_cardinality(self):
  110. """
  111. Test saving and loading of property values cardinality variants to
  112. and from all supported file formats.
  113. """
  114. doc = odml.Document()
  115. sec = odml.Section(name="sec", type="sometype", parent=doc)
  116. prop_empty = "prop_cardinality_empty"
  117. prop_max = "prop_cardinality_max"
  118. prop_max_card = (None, 10)
  119. prop_min = "prop_cardinality_min"
  120. prop_min_card = (2, None)
  121. prop_full = "prop_full"
  122. prop_full_card = (1, 5)
  123. _ = odml.Property(name=prop_empty, parent=sec)
  124. _ = odml.Property(name=prop_max, val_cardinality=prop_max_card, parent=sec)
  125. _ = odml.Property(name=prop_min, val_cardinality=prop_min_card, parent=sec)
  126. _ = odml.Property(name=prop_full, val_cardinality=prop_full_card, parent=sec)
  127. # Test saving to and loading from an XML file
  128. odml.save(doc, self.xml_file)
  129. xml_doc = odml.load(self.xml_file)
  130. xml_prop = xml_doc.sections["sec"].properties[prop_empty]
  131. self.assertIsNone(xml_prop.val_cardinality)
  132. xml_prop = xml_doc.sections["sec"].properties[prop_max]
  133. self.assertEqual(xml_prop.val_cardinality, prop_max_card)
  134. xml_prop = xml_doc.sections["sec"].properties[prop_min]
  135. self.assertEqual(xml_prop.val_cardinality, prop_min_card)
  136. xml_prop = xml_doc.sections["sec"].properties[prop_full]
  137. self.assertEqual(xml_prop.val_cardinality, prop_full_card)
  138. # Test saving to and loading from a JSON file
  139. odml.save(doc, self.json_file, "JSON")
  140. json_doc = odml.load(self.json_file, "JSON")
  141. json_prop = json_doc.sections["sec"].properties[prop_empty]
  142. self.assertIsNone(json_prop.val_cardinality)
  143. json_prop = json_doc.sections["sec"].properties[prop_max]
  144. self.assertEqual(json_prop.val_cardinality, prop_max_card)
  145. json_prop = json_doc.sections["sec"].properties[prop_min]
  146. self.assertEqual(json_prop.val_cardinality, prop_min_card)
  147. json_prop = json_doc.sections["sec"].properties[prop_full]
  148. self.assertEqual(json_prop.val_cardinality, prop_full_card)
  149. # Test saving to and loading from a YAML file
  150. odml.save(doc, self.yaml_file, "YAML")
  151. yaml_doc = odml.load(self.yaml_file, "YAML")
  152. yaml_prop = yaml_doc.sections["sec"].properties[prop_empty]
  153. self.assertIsNone(yaml_prop.val_cardinality)
  154. yaml_prop = yaml_doc.sections["sec"].properties[prop_max]
  155. self.assertEqual(yaml_prop.val_cardinality, prop_max_card)
  156. yaml_prop = yaml_doc.sections["sec"].properties[prop_min]
  157. self.assertEqual(yaml_prop.val_cardinality, prop_min_card)
  158. yaml_prop = yaml_doc.sections["sec"].properties[prop_full]
  159. self.assertEqual(yaml_prop.val_cardinality, prop_full_card)