1
1

test_parser_yaml.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. This module supplies basic tests for the odml.dict_parser.DictReader
  3. reading from yaml files.
  4. """
  5. import os
  6. import shutil
  7. import unittest
  8. import yaml
  9. from odml.tools import dict_parser
  10. from odml.tools.parser_utils import ParserException, InvalidVersionException
  11. from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
  12. _INVALID_ATTRIBUTE_HANDLING_DOC = """
  13. Document:
  14. id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
  15. %s: i_do_not_exist_on_doc_level
  16. sections:
  17. - id: d4f3120a-c02f-4102-a9fe-2e8b77d1d0d2
  18. name: sec
  19. %s: i_do_not_exist_on_sec_level
  20. properties:
  21. - id: 18ad5176-2b46-4a08-9b85-eafd6b14fab7
  22. name: prop
  23. value: []
  24. %s: i_do_not_exist_on_prop_level
  25. sections: []
  26. type: test
  27. odml-version: '1.1'
  28. """.strip()
  29. _SEC_CREATION_ERROR_DOC = """
  30. Document:
  31. id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
  32. sections:
  33. - id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
  34. name: sec
  35. sections:
  36. - id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
  37. name: %s
  38. type: test
  39. - id: [I-am-so-kaputt]
  40. name: %s
  41. type: test
  42. odml-version: '1.1'
  43. """.strip()
  44. _PROP_CREATION_ERROR_DOC = """
  45. Document:
  46. id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
  47. sections:
  48. - id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
  49. name: sec
  50. properties:
  51. - id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
  52. name: valid_prop
  53. - id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
  54. name: invalid_prop
  55. type: int
  56. values:
  57. - 'a'
  58. - 'b'
  59. type: test
  60. odml-version: '1.1'
  61. """.strip()
  62. class TestYAMLParser(unittest.TestCase):
  63. def setUp(self):
  64. self.base_path = RES_DIR
  65. self.yaml_reader = dict_parser.DictReader(show_warnings=False)
  66. self.tmp_dir_path = create_test_dir(__file__)
  67. def tearDown(self):
  68. if self.tmp_dir_path and os.path.exists(self.tmp_dir_path):
  69. shutil.rmtree(self.tmp_dir_path)
  70. def _prepare_doc(self, file_name, file_content):
  71. file_path = os.path.join(self.tmp_dir_path, file_name)
  72. with open(file_path, "w") as dump_file:
  73. dump_file.write(file_content)
  74. with open(file_path) as raw_data:
  75. parsed_doc = yaml.safe_load(raw_data)
  76. return parsed_doc
  77. def test_missing_root(self):
  78. filename = "missing_root.yaml"
  79. message = "Missing root element"
  80. with open(os.path.join(self.base_path, filename)) as raw_data:
  81. parsed_doc = yaml.safe_load(raw_data)
  82. with self.assertRaises(ParserException) as exc:
  83. _ = self.yaml_reader.to_odml(parsed_doc)
  84. self.assertIn(message, str(exc.exception))
  85. def test_missing_version(self):
  86. filename = "missing_version.yaml"
  87. message = "Could not find odml-version"
  88. with open(os.path.join(self.base_path, filename)) as raw_data:
  89. parsed_doc = yaml.safe_load(raw_data)
  90. with self.assertRaises(ParserException) as exc:
  91. _ = self.yaml_reader.to_odml(parsed_doc)
  92. self.assertIn(message, str(exc.exception))
  93. def test_invalid_version(self):
  94. filename = "invalid_version.yaml"
  95. with open(os.path.join(self.base_path, filename)) as raw_data:
  96. parsed_doc = yaml.safe_load(raw_data)
  97. with self.assertRaises(InvalidVersionException):
  98. _ = self.yaml_reader.to_odml(parsed_doc)
  99. def test_invalid_attribute_handling(self):
  100. filename = "invalid_attributes.yaml"
  101. inv_doc_attr = "invalid_doc_attr"
  102. inv_sec_attr = "invalid_sec_attr"
  103. inv_prop_attr = "invalid_prop_attr"
  104. file_content = _INVALID_ATTRIBUTE_HANDLING_DOC % (inv_doc_attr, inv_sec_attr, inv_prop_attr)
  105. parsed_doc = self._prepare_doc(filename, file_content)
  106. # Test ParserException on default parse
  107. exc_msg = "Invalid element"
  108. with self.assertRaises(ParserException) as exc:
  109. _ = self.yaml_reader.to_odml(parsed_doc)
  110. self.assertIn(exc_msg, str(exc.exception))
  111. # Test success on ignore_error parse
  112. self.yaml_reader.ignore_errors = True
  113. doc = self.yaml_reader.to_odml(parsed_doc)
  114. self.assertEqual(len(doc.sections), 1)
  115. self.assertEqual(len(doc.sections["sec"].properties), 1)
  116. self.assertEqual(len(self.yaml_reader.warnings), 3)
  117. for msg in self.yaml_reader.warnings:
  118. self.assertIn("Error", msg)
  119. self.assertTrue(inv_doc_attr in msg or inv_sec_attr in msg or inv_prop_attr in msg)
  120. def test_sec_creation_error(self):
  121. filename = "broken_section.yaml"
  122. valid = "valid_sec"
  123. invalid = "invalid_sec"
  124. file_content = _SEC_CREATION_ERROR_DOC % (valid, invalid)
  125. parsed_doc = self._prepare_doc(filename, file_content)
  126. # Test ParserException on default parse
  127. exc_msg = "Section not created"
  128. with self.assertRaises(ParserException) as exc:
  129. _ = self.yaml_reader.to_odml(parsed_doc)
  130. self.assertIn(exc_msg, str(exc.exception))
  131. # Test success on ignore_error parse
  132. self.yaml_reader.ignore_errors = True
  133. doc = self.yaml_reader.to_odml(parsed_doc)
  134. self.assertEqual(len(doc.sections["sec"].sections), 1)
  135. self.assertIn(valid, doc.sections["sec"].sections)
  136. self.assertNotIn(invalid, doc.sections["sec"].sections)
  137. self.assertEqual(len(self.yaml_reader.warnings), 1)
  138. for msg in self.yaml_reader.warnings:
  139. self.assertIn("Error", msg)
  140. self.assertIn(exc_msg, msg)
  141. def test_prop_creation_error(self):
  142. filename = "broken_property.yaml"
  143. parsed_doc = self._prepare_doc(filename, _PROP_CREATION_ERROR_DOC)
  144. # Test ParserException on default parse
  145. exc_msg = "Property not created"
  146. with self.assertRaises(ParserException) as exc:
  147. _ = self.yaml_reader.to_odml(parsed_doc)
  148. self.assertIn(exc_msg, str(exc.exception))
  149. # Test success on ignore_error parse
  150. self.yaml_reader.ignore_errors = True
  151. doc = self.yaml_reader.to_odml(parsed_doc)
  152. self.assertEqual(len(doc.sections["sec"].properties), 1)
  153. self.assertIn("valid_prop", doc.sections["sec"].properties)
  154. self.assertNotIn("invalid_prop", doc.sections["sec"].properties)
  155. self.assertEqual(len(self.yaml_reader.warnings), 1)
  156. for msg in self.yaml_reader.warnings:
  157. self.assertIn("Error", msg)
  158. self.assertIn(exc_msg, msg)