test_property_integration.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 tempfile
  8. import unittest
  9. import odml
  10. class TestPropertyIntegration(unittest.TestCase):
  11. def setUp(self):
  12. # Set up test environment
  13. self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
  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 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)