1
1
import unittest
2
2
import odml
3
3
import os
4
+ import sys
4
5
import odml .validation
5
6
import odml .terminology
6
7
from . import test_samplefile as samplefile
7
8
9
+ try :
10
+ from StringIO import StringIO
11
+ except ImportError :
12
+ from io import StringIO
13
+
8
14
validate = odml .validation .Validation
9
15
10
16
@@ -129,6 +135,27 @@ def test_section_unique_ids(self):
129
135
res = validate (doc )
130
136
self .assertError (res , "Duplicate id in Section" )
131
137
138
+ def test_section_name_readable (self ):
139
+ """
140
+ Test if section name is not uuid and thus more readable.
141
+ """
142
+ doc = odml .Document ()
143
+ sec = odml .Section ("sec" , parent = doc )
144
+ sec .name = sec .id
145
+ res = validate (doc )
146
+ self .assertError (res , "Name should be readable" )
147
+
148
+ def test_property_name_readable (self ):
149
+ """
150
+ Test if property name is not uuid and thus more readable.
151
+ """
152
+ doc = odml .Document ()
153
+ sec = odml .Section ("sec" , parent = doc )
154
+ prop = odml .Property ("prop" , parent = sec )
155
+ prop .name = prop .id
156
+ res = validate (doc )
157
+ self .assertError (res , "Name should be readable" )
158
+
132
159
def test_standalone_section (self ):
133
160
"""
134
161
Test if standalone section does not return errors if required attributes are correct.
@@ -152,8 +179,20 @@ def test_standalone_property(self):
152
179
prop = odml .Property ()
153
180
prop .type = ""
154
181
155
- for err in validate (prop ).errors :
156
- assert not err .is_error
182
+ assert len (list (filter (lambda x : x .is_error , validate (prop ).errors ))) == 0
183
+
184
+ def test_section_init (self ):
185
+ """
186
+ Test validation errors printed to stdout on section init.
187
+ """
188
+ val_errs = StringIO ()
189
+
190
+ old_stdout = sys .stdout
191
+ sys .stdout = val_errs
192
+ odml .Section (name = "sec" , type = None )
193
+ sys .stdout = old_stdout
194
+
195
+ assert "Section type undefined" in val_errs .getvalue ()
157
196
158
197
def test_prop_string_values (self ):
159
198
"""
@@ -172,8 +211,8 @@ def test_prop_string_values(self):
172
211
173
212
prop2 = odml .Property (name = 'potential' , dtype = "string" ,
174
213
values = ['-4.8' , '10.0' , '-11.9' , '-10.0' , '18.0' ])
175
- self .assertError (validate (prop2 ),'Dtype of property "potential" currently is "string", '
176
- 'but might fit dtype "float"!' )
214
+ self .assertError (validate (prop2 ), 'Dtype of property "potential" currently is "string", '
215
+ 'but might fit dtype "float"!' )
177
216
178
217
prop3 = odml .Property (name = 'dates' , dtype = "string" ,
179
218
values = ['1997-12-14' , '00-12-14' , '89-07-04' ])
@@ -219,17 +258,12 @@ def test_load_section_xml(self):
219
258
path = os .path .join (self .dir_path , "resources" , "validation_section.xml" )
220
259
doc = odml .load (path )
221
260
222
- sec_type_undefined_err = False
223
- sec_type_empty_err = False
224
-
225
- for err in validate (doc ).errors :
226
- if err .msg == "Section type undefined" and err .obj .name == "sec_type_undefined" :
227
- sec_type_undefined_err = True
228
- elif err .msg == "Section type undefined" and err .obj .name == "sec_type_empty" :
229
- sec_type_empty_err = True
230
-
231
- assert sec_type_undefined_err
232
- assert sec_type_empty_err
261
+ assert len (list (filter (
262
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_undefined" ,
263
+ validate (doc ).errors ))) > 0
264
+ assert len (list (filter (
265
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_empty" ,
266
+ validate (doc ).errors ))) > 0
233
267
234
268
def test_load_dtypes_xml (self ):
235
269
"""
@@ -298,17 +332,12 @@ def test_load_section_json(self):
298
332
path = os .path .join (self .dir_path , "resources" , "validation_section.json" )
299
333
doc = odml .load (path , "JSON" )
300
334
301
- sec_type_undefined_err = False
302
- sec_type_empty_err = False
303
-
304
- for err in validate (doc ).errors :
305
- if err .msg == "Section type undefined" and err .obj .name == "sec_type_undefined" :
306
- sec_type_undefined_err = True
307
- elif err .msg == "Section type undefined" and err .obj .name == "sec_type_empty" :
308
- sec_type_empty_err = True
309
-
310
- assert sec_type_undefined_err
311
- assert sec_type_empty_err
335
+ assert len (list (filter (
336
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_undefined" ,
337
+ validate (doc ).errors ))) > 0
338
+ assert len (list (filter (
339
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_empty" ,
340
+ validate (doc ).errors ))) > 0
312
341
313
342
def test_load_dtypes_json (self ):
314
343
"""
@@ -377,17 +406,12 @@ def test_load_section_yaml(self):
377
406
path = os .path .join (self .dir_path , "resources" , "validation_section.yaml" )
378
407
doc = odml .load (path , "YAML" )
379
408
380
- sec_type_undefined_err = False
381
- sec_type_empty_err = False
382
-
383
- for err in validate (doc ).errors :
384
- if err .msg == "Section type undefined" and err .obj .name == "sec_type_undefined" :
385
- sec_type_undefined_err = True
386
- elif err .msg == "Section type undefined" and err .obj .name == "sec_type_empty" :
387
- sec_type_empty_err = True
388
-
389
- assert sec_type_undefined_err
390
- assert sec_type_empty_err
409
+ assert len (list (filter (
410
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_undefined" ,
411
+ validate (doc ).errors ))) > 0
412
+ assert len (list (filter (
413
+ lambda x : x .msg == "Section type undefined" and x .obj .name == "sec_type_empty" ,
414
+ validate (doc ).errors ))) > 0
391
415
392
416
def test_load_dtypes_yaml (self ):
393
417
"""
0 commit comments