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
@@ -12,7 +18,26 @@ def setUp(self):
12
18
dir_path = os .path .dirname (os .path .realpath (__file__ ))
13
19
self .basepath = os .path .join (dir_path , "resources" )
14
20
15
- self .json_reader = dict_parser .DictReader ()
21
+ self .json_reader = dict_parser .DictReader (show_warnings = False )
22
+
23
+ dir_name = os .path .basename (os .path .splitext (__file__ )[0 ])
24
+ tmp_dir_path = os .path .join (tempfile .gettempdir (), "odml_test" , dir_name )
25
+
26
+ if not os .path .exists (tmp_dir_path ):
27
+ os .mkdir (tmp_dir_path )
28
+
29
+ self .tmp_dir_path = tmp_dir_path
30
+
31
+ def _prepare_doc (self , file_name , file_content ):
32
+ file_path = os .path .join (self .tmp_dir_path , file_name )
33
+
34
+ with open (file_path , "w" ) as dump_file :
35
+ dump_file .write (file_content )
36
+
37
+ with open (file_path ) as raw_data :
38
+ parsed_doc = json .load (raw_data )
39
+
40
+ return parsed_doc
16
41
17
42
def test_missing_root (self ):
18
43
filename = "missing_root.json"
@@ -46,3 +71,167 @@ def test_invalid_version(self):
46
71
47
72
with self .assertRaises (InvalidVersionException ):
48
73
_ = self .json_reader .to_odml (parsed_doc )
74
+
75
+ def test_invalid_attribute_handling (self ):
76
+ filename = "invalid_attributes.yaml"
77
+
78
+ inv_doc_attr = "invalid_doc_attr"
79
+ inv_sec_attr = "invalid_sec_attr"
80
+ inv_prop_attr = "invalid_prop_attr"
81
+
82
+ file_content = """{
83
+ "Document": {
84
+ "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
85
+ "%s": "i_do_not_exist_on_doc_level",
86
+ "sections": [
87
+ {
88
+ "id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
89
+ "%s": "i_do_not_exist_on_sec_level",
90
+ "type": "test",
91
+ "name": "sec",
92
+ "sections": [],
93
+ "properties": [
94
+ {
95
+ "id": "c5aed35a-d9ff-437d-82d6-68f0cda8ea94",
96
+ "%s": "i_do_not_exist_on_prop_level",
97
+ "name": "prop",
98
+ "value": [
99
+ 1,
100
+ 2,
101
+ 3
102
+ ],
103
+ "type": "int"
104
+ }
105
+ ]
106
+ }
107
+ ]
108
+ },
109
+ "odml-version": "1.1"
110
+ }
111
+ """ % (inv_doc_attr , inv_sec_attr , inv_prop_attr )
112
+ parsed_doc = self ._prepare_doc (filename , file_content )
113
+
114
+ # Test ParserException on default parse
115
+ exc_msg = "Invalid element"
116
+ with self .assertRaises (ParserException ) as exc :
117
+ _ = self .json_reader .to_odml (parsed_doc )
118
+ self .assertIn (exc_msg , str (exc .exception ))
119
+
120
+ # Test success on ignore_error parse
121
+ self .json_reader .ignore_errors = True
122
+ doc = self .json_reader .to_odml (parsed_doc )
123
+
124
+ self .assertEqual (len (doc .sections ), 1 )
125
+ self .assertEqual (len (doc .sections ["sec" ].properties ), 1 )
126
+
127
+ self .assertEqual (len (self .json_reader .warnings ), 3 )
128
+ for msg in self .json_reader .warnings :
129
+ self .assertIn ("Error" , msg )
130
+ self .assertTrue (inv_doc_attr in msg or inv_sec_attr in msg or inv_prop_attr in msg )
131
+
132
+ def test_sec_creation_error (self ):
133
+ filename = "broken_section.yaml"
134
+
135
+ valid = "valid_sec"
136
+ invalid = "invalid_sec"
137
+
138
+ file_content = """{
139
+ "Document": {
140
+ "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
141
+ "sections": [
142
+ {
143
+ "id": "1113c79c-a7d7-471e-be16-e085caa851fb",
144
+ "name": "sec",
145
+ "type": "test",
146
+ "sections": [
147
+ {
148
+ "id": "1213c79c-a7d7-471e-be16-e085caa851fb",
149
+ "name": "%s",
150
+ "type": "test"
151
+ },
152
+ {
153
+ "id": [
154
+ "I-am-so-kaputt"
155
+ ],
156
+ "name": "%s",
157
+ "type": "test"
158
+ }
159
+ ]
160
+ }
161
+ ]
162
+ },
163
+ "odml-version": "1.1"
164
+ }
165
+ """ % (valid , invalid )
166
+ parsed_doc = self ._prepare_doc (filename , file_content )
167
+
168
+ # Test ParserException on default parse
169
+ exc_msg = "Section not created"
170
+ with self .assertRaises (ParserException ) as exc :
171
+ _ = self .json_reader .to_odml (parsed_doc )
172
+ self .assertIn (exc_msg , str (exc .exception ))
173
+
174
+ # Test success on ignore_error parse
175
+ self .json_reader .ignore_errors = True
176
+ doc = self .json_reader .to_odml (parsed_doc )
177
+
178
+ self .assertEqual (len (doc .sections ["sec" ].sections ), 1 )
179
+ self .assertIn (valid , doc .sections ["sec" ].sections )
180
+ self .assertNotIn (invalid , doc .sections ["sec" ].sections )
181
+
182
+ self .assertEqual (len (self .json_reader .warnings ), 1 )
183
+ for msg in self .json_reader .warnings :
184
+ self .assertIn ("Error" , msg )
185
+ self .assertIn (exc_msg , msg )
186
+
187
+ def test_prop_creation_error (self ):
188
+ filename = "broken_property.yaml"
189
+ file_content = """{
190
+ "Document": {
191
+ "id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
192
+ "sections": [
193
+ {
194
+ "id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
195
+ "type": "test",
196
+ "name": "sec",
197
+ "properties": [
198
+ {
199
+ "id": "121ed35a-d9ff-437d-82d6-68f0cda8ea94",
200
+ "name": "valid_prop"
201
+ },
202
+ {
203
+ "id": "122ed35a-d9ff-437d-82d6-68f0cda8ea94",
204
+ "name": "invalid_prop",
205
+ "value": [
206
+ "a",
207
+ "b"
208
+ ],
209
+ "type": "int"
210
+ }
211
+ ]
212
+ }
213
+ ]
214
+ },
215
+ "odml-version": "1.1"
216
+ }
217
+ """
218
+ parsed_doc = self ._prepare_doc (filename , file_content )
219
+
220
+ # Test ParserException on default parse
221
+ exc_msg = "Property not created"
222
+ with self .assertRaises (ParserException ) as exc :
223
+ _ = self .json_reader .to_odml (parsed_doc )
224
+ self .assertIn (exc_msg , str (exc .exception ))
225
+
226
+ # Test success on ignore_error parse
227
+ self .json_reader .ignore_errors = True
228
+ doc = self .json_reader .to_odml (parsed_doc )
229
+
230
+ self .assertEqual (len (doc .sections ["sec" ].properties ), 1 )
231
+ self .assertIn ("valid_prop" , doc .sections ["sec" ].properties )
232
+ self .assertNotIn ("invalid_prop" , doc .sections ["sec" ].properties )
233
+
234
+ self .assertEqual (len (self .json_reader .warnings ), 1 )
235
+ for msg in self .json_reader .warnings :
236
+ self .assertIn ("Error" , msg )
237
+ self .assertIn (exc_msg , msg )
0 commit comments