|
| 1 | +""" |
| 2 | +This module supplies basic tests for the odml.dict_parser.DictReader |
| 3 | +reading from json files. |
| 4 | +""" |
| 5 | + |
1 | 6 | import json
|
2 | 7 | import os
|
| 8 | +import tempfile |
3 | 9 | import unittest
|
4 | 10 |
|
5 | 11 | from odml.tools import dict_parser
|
6 | 12 | from odml.tools.parser_utils import ParserException, InvalidVersionException
|
7 | 13 |
|
8 | 14 |
|
| 15 | +_INVALID_ATTRIBUTE_HANDLING_DOC = """ |
| 16 | +{ |
| 17 | + "Document": { |
| 18 | + "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383", |
| 19 | + "%s": "i_do_not_exist_on_doc_level", |
| 20 | + "sections": [ |
| 21 | + { |
| 22 | + "id": "51f3c79c-a7d7-471e-be16-e085caa851fb", |
| 23 | + "%s": "i_do_not_exist_on_sec_level", |
| 24 | + "type": "test", |
| 25 | + "name": "sec", |
| 26 | + "sections": [], |
| 27 | + "properties": [ |
| 28 | + { |
| 29 | + "id": "c5aed35a-d9ff-437d-82d6-68f0cda8ea94", |
| 30 | + "%s": "i_do_not_exist_on_prop_level", |
| 31 | + "name": "prop", |
| 32 | + "value": [ |
| 33 | + 1, |
| 34 | + 2, |
| 35 | + 3 |
| 36 | + ], |
| 37 | + "type": "int" |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + ] |
| 42 | + }, |
| 43 | + "odml-version": "1.1" |
| 44 | +} |
| 45 | +""".strip() |
| 46 | + |
| 47 | +_SEC_CREATION_ERROR_DOC = """ |
| 48 | +{ |
| 49 | + "Document": { |
| 50 | + "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383", |
| 51 | + "sections": [ |
| 52 | + { |
| 53 | + "id": "1113c79c-a7d7-471e-be16-e085caa851fb", |
| 54 | + "name": "sec", |
| 55 | + "type": "test", |
| 56 | + "sections": [ |
| 57 | + { |
| 58 | + "id": "1213c79c-a7d7-471e-be16-e085caa851fb", |
| 59 | + "name": "%s", |
| 60 | + "type": "test" |
| 61 | + }, |
| 62 | + { |
| 63 | + "id": [ |
| 64 | + "I-am-so-kaputt" |
| 65 | + ], |
| 66 | + "name": "%s", |
| 67 | + "type": "test" |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + ] |
| 72 | + }, |
| 73 | + "odml-version": "1.1" |
| 74 | +} |
| 75 | +""".strip() |
| 76 | + |
| 77 | +_PROP_CREATION_ERROR_DOC = """ |
| 78 | +{ |
| 79 | + "Document": { |
| 80 | + "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383", |
| 81 | + "sections": [ |
| 82 | + { |
| 83 | + "id": "51f3c79c-a7d7-471e-be16-e085caa851fb", |
| 84 | + "type": "test", |
| 85 | + "name": "sec", |
| 86 | + "properties": [ |
| 87 | + { |
| 88 | + "id": "121ed35a-d9ff-437d-82d6-68f0cda8ea94", |
| 89 | + "name": "valid_prop" |
| 90 | + }, |
| 91 | + { |
| 92 | + "id": "122ed35a-d9ff-437d-82d6-68f0cda8ea94", |
| 93 | + "name": "invalid_prop", |
| 94 | + "value": [ |
| 95 | + "a", |
| 96 | + "b" |
| 97 | + ], |
| 98 | + "type": "int" |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + ] |
| 103 | + }, |
| 104 | + "odml-version": "1.1" |
| 105 | +} |
| 106 | +""".strip() |
| 107 | + |
| 108 | + |
9 | 109 | class TestJSONParser(unittest.TestCase):
|
10 | 110 |
|
11 | 111 | def setUp(self):
|
12 | 112 | dir_path = os.path.dirname(os.path.realpath(__file__))
|
13 | 113 | self.basepath = os.path.join(dir_path, "resources")
|
14 | 114 |
|
15 |
| - self.json_reader = dict_parser.DictReader() |
| 115 | + self.json_reader = dict_parser.DictReader(show_warnings=False) |
| 116 | + |
| 117 | + dir_name = os.path.basename(os.path.splitext(__file__)[0]) |
| 118 | + tmp_base_path = os.path.join(tempfile.gettempdir(), "odml_test") |
| 119 | + if not os.path.exists(tmp_base_path): |
| 120 | + os.mkdir(tmp_base_path) |
| 121 | + |
| 122 | + tmp_dir_path = os.path.join(tmp_base_path, dir_name) |
| 123 | + if not os.path.exists(tmp_dir_path): |
| 124 | + os.mkdir(tmp_dir_path) |
| 125 | + |
| 126 | + self.tmp_dir_path = tmp_dir_path |
| 127 | + |
| 128 | + def _prepare_doc(self, file_name, file_content): |
| 129 | + file_path = os.path.join(self.tmp_dir_path, file_name) |
| 130 | + |
| 131 | + with open(file_path, "w") as dump_file: |
| 132 | + dump_file.write(file_content) |
| 133 | + |
| 134 | + with open(file_path) as raw_data: |
| 135 | + parsed_doc = json.load(raw_data) |
| 136 | + |
| 137 | + return parsed_doc |
16 | 138 |
|
17 | 139 | def test_missing_root(self):
|
18 | 140 | filename = "missing_root.json"
|
@@ -46,3 +168,83 @@ def test_invalid_version(self):
|
46 | 168 |
|
47 | 169 | with self.assertRaises(InvalidVersionException):
|
48 | 170 | _ = self.json_reader.to_odml(parsed_doc)
|
| 171 | + |
| 172 | + def test_invalid_attribute_handling(self): |
| 173 | + filename = "invalid_attributes.yaml" |
| 174 | + |
| 175 | + inv_doc_attr = "invalid_doc_attr" |
| 176 | + inv_sec_attr = "invalid_sec_attr" |
| 177 | + inv_prop_attr = "invalid_prop_attr" |
| 178 | + |
| 179 | + file_content = _INVALID_ATTRIBUTE_HANDLING_DOC % (inv_doc_attr, inv_sec_attr, inv_prop_attr) |
| 180 | + parsed_doc = self._prepare_doc(filename, file_content) |
| 181 | + |
| 182 | + # Test ParserException on default parse |
| 183 | + exc_msg = "Invalid element" |
| 184 | + with self.assertRaises(ParserException) as exc: |
| 185 | + _ = self.json_reader.to_odml(parsed_doc) |
| 186 | + self.assertIn(exc_msg, str(exc.exception)) |
| 187 | + |
| 188 | + # Test success on ignore_error parse |
| 189 | + self.json_reader.ignore_errors = True |
| 190 | + doc = self.json_reader.to_odml(parsed_doc) |
| 191 | + |
| 192 | + self.assertEqual(len(doc.sections), 1) |
| 193 | + self.assertEqual(len(doc.sections["sec"].properties), 1) |
| 194 | + |
| 195 | + self.assertEqual(len(self.json_reader.warnings), 3) |
| 196 | + for msg in self.json_reader.warnings: |
| 197 | + self.assertIn("Error", msg) |
| 198 | + self.assertTrue(inv_doc_attr in msg or inv_sec_attr in msg or inv_prop_attr in msg) |
| 199 | + |
| 200 | + def test_sec_creation_error(self): |
| 201 | + filename = "broken_section.yaml" |
| 202 | + |
| 203 | + valid = "valid_sec" |
| 204 | + invalid = "invalid_sec" |
| 205 | + |
| 206 | + file_content = _SEC_CREATION_ERROR_DOC % (valid, invalid) |
| 207 | + parsed_doc = self._prepare_doc(filename, file_content) |
| 208 | + |
| 209 | + # Test ParserException on default parse |
| 210 | + exc_msg = "Section not created" |
| 211 | + with self.assertRaises(ParserException) as exc: |
| 212 | + _ = self.json_reader.to_odml(parsed_doc) |
| 213 | + self.assertIn(exc_msg, str(exc.exception)) |
| 214 | + |
| 215 | + # Test success on ignore_error parse |
| 216 | + self.json_reader.ignore_errors = True |
| 217 | + doc = self.json_reader.to_odml(parsed_doc) |
| 218 | + |
| 219 | + self.assertEqual(len(doc.sections["sec"].sections), 1) |
| 220 | + self.assertIn(valid, doc.sections["sec"].sections) |
| 221 | + self.assertNotIn(invalid, doc.sections["sec"].sections) |
| 222 | + |
| 223 | + self.assertEqual(len(self.json_reader.warnings), 1) |
| 224 | + for msg in self.json_reader.warnings: |
| 225 | + self.assertIn("Error", msg) |
| 226 | + self.assertIn(exc_msg, msg) |
| 227 | + |
| 228 | + def test_prop_creation_error(self): |
| 229 | + filename = "broken_property.yaml" |
| 230 | + |
| 231 | + parsed_doc = self._prepare_doc(filename, _PROP_CREATION_ERROR_DOC) |
| 232 | + |
| 233 | + # Test ParserException on default parse |
| 234 | + exc_msg = "Property not created" |
| 235 | + with self.assertRaises(ParserException) as exc: |
| 236 | + _ = self.json_reader.to_odml(parsed_doc) |
| 237 | + self.assertIn(exc_msg, str(exc.exception)) |
| 238 | + |
| 239 | + # Test success on ignore_error parse |
| 240 | + self.json_reader.ignore_errors = True |
| 241 | + doc = self.json_reader.to_odml(parsed_doc) |
| 242 | + |
| 243 | + self.assertEqual(len(doc.sections["sec"].properties), 1) |
| 244 | + self.assertIn("valid_prop", doc.sections["sec"].properties) |
| 245 | + self.assertNotIn("invalid_prop", doc.sections["sec"].properties) |
| 246 | + |
| 247 | + self.assertEqual(len(self.json_reader.warnings), 1) |
| 248 | + for msg in self.json_reader.warnings: |
| 249 | + self.assertIn("Error", msg) |
| 250 | + self.assertIn(exc_msg, msg) |
0 commit comments