test_version_converter_integration.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. This file tests proper conversion of complex v1.0 odML files
  3. to v1.1 with all supported file formats.
  4. """
  5. import os
  6. import shutil
  7. import unittest
  8. from odml import load
  9. from odml.tools.converters import VersionConverter as VC
  10. from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
  11. class TestVersionConverterIntegration(unittest.TestCase):
  12. def setUp(self):
  13. # Set up test environment
  14. self.jsonfile = os.path.join(RES_DIR, "version_conversion_int.json")
  15. self.xmlfile = os.path.join(RES_DIR, "version_conversion_int.xml")
  16. self.yamlfile = os.path.join(RES_DIR, "version_conversion_int.yaml")
  17. self.tmp_dir = create_test_dir(__file__)
  18. self.outfile = os.path.join(self.tmp_dir, "version_conversion.xml")
  19. def tearDown(self):
  20. if self.tmp_dir and os.path.exists(self.tmp_dir):
  21. shutil.rmtree(self.tmp_dir)
  22. def test_convert_xml(self):
  23. VC(self.xmlfile).write_to_file(self.outfile)
  24. self.assertTrue(os.path.exists(self.outfile))
  25. self.check_result()
  26. def test_convert_yaml(self):
  27. with self.assertRaises(Exception):
  28. VC(self.yamlfile).write_to_file(self.outfile)
  29. VC(self.yamlfile).write_to_file(self.outfile, "YAML")
  30. self.assertTrue(os.path.exists(self.outfile))
  31. self.check_result()
  32. def test_convert_json(self):
  33. with self.assertRaises(Exception):
  34. VC(self.jsonfile).write_to_file(self.outfile)
  35. VC(self.jsonfile).write_to_file(self.outfile, "JSON")
  36. self.assertTrue(os.path.exists(self.outfile))
  37. self.check_result()
  38. def check_result(self):
  39. doc = load(self.outfile)
  40. # Test document attribute export
  41. self.assertEqual(doc.author, "author")
  42. self.assertEqual(doc.version, "v1.13")
  43. repo = "https://terminologies.g-node.org/v1.1/terminologies.xml"
  44. self.assertEqual(doc.repository, repo)
  45. self.assertEqual(len(doc.sections), 3)
  46. sec = doc.sections["sec_one"]
  47. self.assertEqual(sec.definition, "def s1")
  48. self.assertEqual(sec.reference, "ref s1")
  49. self.assertEqual(sec.type, "mainsec")
  50. self.assertEqual(len(sec.sections), 1)
  51. self.assertEqual(len(sec.properties), 3)
  52. prop = sec.properties["prop_one"]
  53. self.assertEqual(prop.definition, "def prop1")
  54. self.assertEqual(prop.dependency, "dep p1")
  55. self.assertEqual(prop.dependency_value, "dep val p1")
  56. self.assertEqual(prop.reference, "ref val 1")
  57. self.assertEqual(prop.uncertainty, "11")
  58. self.assertEqual(prop.unit, "arbitrary")
  59. self.assertEqual(prop.value_origin, "filename val 1")
  60. self.assertEqual(prop.dtype, "string")
  61. self.assertEqual(len(prop.values), 3)
  62. prop = sec.properties["prop_two"]
  63. self.assertEqual(len(prop.values), 8)
  64. prop = sec.properties["prop_three"]
  65. self.assertEqual(len(prop.values), 0)