11import unittest
22import odml
33import os
4+ import sys
45import odml .validation
56import odml .terminology
67from . import test_samplefile as samplefile
78
9+ try :
10+ from StringIO import StringIO
11+ except ImportError :
12+ from io import StringIO
13+
814validate = odml .validation .Validation
915
1016
@@ -129,6 +135,27 @@ def test_section_unique_ids(self):
129135 res = validate (doc )
130136 self .assertError (res , "Duplicate id in Section" )
131137
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+
132159 def test_standalone_section (self ):
133160 """
134161 Test if standalone section does not return errors if required attributes are correct.
@@ -152,8 +179,20 @@ def test_standalone_property(self):
152179 prop = odml .Property ()
153180 prop .type = ""
154181
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 ()
157196
158197 def test_prop_string_values (self ):
159198 """
@@ -172,8 +211,8 @@ def test_prop_string_values(self):
172211
173212 prop2 = odml .Property (name = 'potential' , dtype = "string" ,
174213 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"!' )
177216
178217 prop3 = odml .Property (name = 'dates' , dtype = "string" ,
179218 values = ['1997-12-14' , '00-12-14' , '89-07-04' ])
@@ -219,17 +258,12 @@ def test_load_section_xml(self):
219258 path = os .path .join (self .dir_path , "resources" , "validation_section.xml" )
220259 doc = odml .load (path )
221260
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
233267
234268 def test_load_dtypes_xml (self ):
235269 """
@@ -298,17 +332,12 @@ def test_load_section_json(self):
298332 path = os .path .join (self .dir_path , "resources" , "validation_section.json" )
299333 doc = odml .load (path , "JSON" )
300334
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
312341
313342 def test_load_dtypes_json (self ):
314343 """
@@ -377,17 +406,12 @@ def test_load_section_yaml(self):
377406 path = os .path .join (self .dir_path , "resources" , "validation_section.yaml" )
378407 doc = odml .load (path , "YAML" )
379408
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
391415
392416 def test_load_dtypes_yaml (self ):
393417 """
0 commit comments