Skip to content

Commit 4927833

Browse files
committed
[test/validation] Reduce redundancy
1 parent 4fcfcc9 commit 4927833

File tree

1 file changed

+59
-189
lines changed

1 file changed

+59
-189
lines changed

test/test_validation.py

Lines changed: 59 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ def test_standalone_property(self):
222222
prop = odml.Property()
223223
prop.type = ""
224224

225-
assert len(list(filter(lambda x: x.is_error, validate(prop).errors))) == 0
225+
errs = list(filter(lambda x: x.is_error, validate(prop).errors))
226+
self.assertEquals(len(errs), 0)
226227

227228
def test_section_init(self):
228229
"""
@@ -235,7 +236,7 @@ def test_section_init(self):
235236
odml.Section(name="sec", type=None)
236237
sys.stdout = old_stdout
237238

238-
assert "Section type undefined" in val_errs.getvalue()
239+
self.assertIn("Section type undefined", val_errs.getvalue())
239240

240241
def test_prop_string_values(self):
241242
"""
@@ -247,51 +248,44 @@ def test_prop_string_values(self):
247248
values=['-13', '101', '-11', 'hello'])
248249
assert len(validate(prop0).errors) == 0
249250

251+
msg_base = 'Dtype of property "%s" currently is "string", but might fit dtype "%s"!'
252+
250253
prop1 = odml.Property(name='members', dtype="string",
251254
values=['-13', '101', '-11', '0', '-8'])
252-
self.assertError(validate(prop1), 'Dtype of property "members" currently is "string",'
253-
' but might fit dtype "int"!')
255+
self.assertError(validate(prop1), msg_base % ("members", "int"))
254256

255257
prop2 = odml.Property(name='potential', dtype="string",
256258
values=['-4.8', '10.0', '-11.9', '-10.0', '18.0'])
257-
self.assertError(validate(prop2), 'Dtype of property "potential" currently is "string", '
258-
'but might fit dtype "float"!')
259+
self.assertError(validate(prop2), msg_base % ("potential", "float"))
259260

260261
prop3 = odml.Property(name='dates', dtype="string",
261262
values=['1997-12-14', '00-12-14', '89-07-04'])
262-
self.assertError(validate(prop3), 'Dtype of property "dates" currently is "string", '
263-
'but might fit dtype "date"!')
263+
self.assertError(validate(prop3), msg_base % ("dates", "date"))
264264

265265
prop4 = odml.Property(name='datetimes', dtype="string",
266266
values=['97-12-14 11:11:11', '97-12-14 12:12', '1997-12-14 03:03'])
267-
self.assertError(validate(prop4), 'Dtype of property "datetimes" currently is "string", '
268-
'but might fit dtype "datetime"!')
267+
self.assertError(validate(prop4), msg_base % ("datetimes", "datetime"))
269268

270269
prop5 = odml.Property(name='times', dtype="string",
271270
values=['11:11:11', '12:12:12', '03:03:03'])
272-
self.assertError(validate(prop5), 'Dtype of property "times" currently is "string", '
273-
'but might fit dtype "time"!')
271+
self.assertError(validate(prop5), msg_base % ("times", "time"))
274272

275273
prop6 = odml.Property(name='sent', dtype="string",
276274
values=['False', True, 'TRUE', False, 't'])
277-
self.assertError(validate(prop6), 'Dtype of property "sent" currently is "string", '
278-
'but might fit dtype "boolean"!')
275+
self.assertError(validate(prop6), msg_base % ("sent", "boolean"))
279276

280277
prop7 = odml.Property(name='texts', dtype="string",
281278
values=['line1\n line2', 'line3\n line4', '\nline5\nline6'])
282-
self.assertError(validate(prop7), 'Dtype of property "texts" currently is "string", '
283-
'but might fit dtype "text"!')
279+
self.assertError(validate(prop7), msg_base % ("texts", "text"))
284280

285281
prop8 = odml.Property(name="Location", dtype='string',
286282
values=['(39.12; 67.19)', '(39.12; 67.19)', '(39.12; 67.18)'])
287-
self.assertError(validate(prop8), 'Dtype of property "Location" currently is "string", '
288-
'but might fit dtype "2-tuple"!')
283+
self.assertError(validate(prop8), msg_base % ("Location", "2-tuple"))
289284

290285
prop9 = odml.Property(name="Coos", dtype='string',
291286
values=['(39.12; 89; 67.19)', '(39.12; 78; 67.19)',
292287
'(39.12; 56; 67.18)'])
293-
self.assertError(validate(prop9), 'Dtype of property "Coos" currently is "string", '
294-
'but might fit dtype "3-tuple"!')
288+
self.assertError(validate(prop9), msg_base % ("Coos", "3-tuple"))
295289

296290
def test_load_section_xml(self):
297291
"""
@@ -308,65 +302,6 @@ def test_load_section_xml(self):
308302
lambda x: x.msg == "Section type undefined" and x.obj.name == "sec_type_empty",
309303
validate(doc).errors))) > 0
310304

311-
def test_load_dtypes_xml(self):
312-
"""
313-
Test if loading xml document raises validation errors for Properties with undefined dtypes.
314-
"""
315-
316-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.xml")
317-
doc = odml.load(path)
318-
319-
self.assertError(validate(doc), 'Dtype of property "members_no" currently is "string", '
320-
'but might fit dtype "int"!')
321-
322-
self.assertError(validate(doc), 'Dtype of property "potential_no" currently is "string", '
323-
'but might fit dtype "float"!')
324-
325-
self.assertError(validate(doc), 'Dtype of property "dates_no" currently is "string", '
326-
'but might fit dtype "date"!')
327-
328-
self.assertError(validate(doc), 'Dtype of property "datetimes_no" currently is "string", '
329-
'but might fit dtype "datetime"!')
330-
331-
self.assertError(validate(doc), 'Dtype of property "times_no" currently is "string", '
332-
'but might fit dtype "time"!')
333-
334-
self.assertError(validate(doc), 'Dtype of property "sent_no" currently is "string", '
335-
'but might fit dtype "boolean"!')
336-
337-
self.assertError(validate(doc), 'Dtype of property "Location_no" currently is "string", '
338-
'but might fit dtype "2-tuple"!')
339-
340-
self.assertError(validate(doc), 'Dtype of property "Coos_no" currently is "string", '
341-
'but might fit dtype "3-tuple"!')
342-
343-
self.assertError(validate(doc), 'Dtype of property "members_mislabelled" currently is '
344-
'"string", but might fit dtype "int"!')
345-
346-
self.assertError(validate(doc), 'Dtype of property "potential_mislabelled" currently is '
347-
'"string", but might fit dtype "float"!')
348-
349-
self.assertError(validate(doc), 'Dtype of property "dates_mislabelled" currently is '
350-
'"string", but might fit dtype "date"!')
351-
352-
self.assertError(validate(doc), 'Dtype of property "datetimes_mislabelled" currently is '
353-
'"string", but might fit dtype "datetime"!')
354-
355-
self.assertError(validate(doc), 'Dtype of property "times_mislabelled" currently is '
356-
'"string", but might fit dtype "time"!')
357-
358-
self.assertError(validate(doc), 'Dtype of property "sent_mislabelled" currently is '
359-
'"string", but might fit dtype "boolean"!')
360-
361-
self.assertError(validate(doc), 'Dtype of property "texts_mislabelled" currently is '
362-
'"string", but might fit dtype "text"!')
363-
364-
self.assertError(validate(doc), 'Dtype of property "Location_mislabelled" currently is '
365-
'"string", but might fit dtype "2-tuple"!')
366-
367-
self.assertError(validate(doc), 'Dtype of property "Coos_mislabelled" currently is '
368-
'"string", but might fit dtype "3-tuple"!')
369-
370305
def test_load_section_json(self):
371306
"""
372307
Test if loading json document raises validation errors for Sections with undefined type.
@@ -382,65 +317,6 @@ def test_load_section_json(self):
382317
lambda x: x.msg == "Section type undefined" and x.obj.name == "sec_type_empty",
383318
validate(doc).errors))) > 0
384319

385-
def test_load_dtypes_json(self):
386-
"""
387-
Test if loading json document raises validation errors for Properties with undefined dtypes.
388-
"""
389-
390-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.json")
391-
doc = odml.load(path, "JSON")
392-
393-
self.assertError(validate(doc), 'Dtype of property "members_no" currently is "string", '
394-
'but might fit dtype "int"!')
395-
396-
self.assertError(validate(doc), 'Dtype of property "potential_no" currently is "string", '
397-
'but might fit dtype "float"!')
398-
399-
self.assertError(validate(doc), 'Dtype of property "dates_no" currently is "string", '
400-
'but might fit dtype "date"!')
401-
402-
self.assertError(validate(doc), 'Dtype of property "datetimes_no" currently is "string", '
403-
'but might fit dtype "datetime"!')
404-
405-
self.assertError(validate(doc), 'Dtype of property "times_no" currently is "string", '
406-
'but might fit dtype "time"!')
407-
408-
self.assertError(validate(doc), 'Dtype of property "sent_no" currently is "string", '
409-
'but might fit dtype "boolean"!')
410-
411-
self.assertError(validate(doc), 'Dtype of property "Location_no" currently is "string", '
412-
'but might fit dtype "2-tuple"!')
413-
414-
self.assertError(validate(doc), 'Dtype of property "Coos_no" currently is "string", '
415-
'but might fit dtype "3-tuple"!')
416-
417-
self.assertError(validate(doc), 'Dtype of property "members_mislabelled" currently is '
418-
'"string", but might fit dtype "int"!')
419-
420-
self.assertError(validate(doc), 'Dtype of property "potential_mislabelled" currently is '
421-
'"string", but might fit dtype "float"!')
422-
423-
self.assertError(validate(doc), 'Dtype of property "dates_mislabelled" currently is '
424-
'"string", but might fit dtype "date"!')
425-
426-
self.assertError(validate(doc), 'Dtype of property "datetimes_mislabelled" currently is '
427-
'"string", but might fit dtype "datetime"!')
428-
429-
self.assertError(validate(doc), 'Dtype of property "times_mislabelled" currently is '
430-
'"string", but might fit dtype "time"!')
431-
432-
self.assertError(validate(doc), 'Dtype of property "sent_mislabelled" currently is '
433-
'"string", but might fit dtype "boolean"!')
434-
435-
self.assertError(validate(doc), 'Dtype of property "texts_mislabelled" currently is '
436-
'"string", but might fit dtype "text"!')
437-
438-
self.assertError(validate(doc), 'Dtype of property "Location_mislabelled" currently is '
439-
'"string", but might fit dtype "2-tuple"!')
440-
441-
self.assertError(validate(doc), 'Dtype of property "Coos_mislabelled" currently is '
442-
'"string", but might fit dtype "3-tuple"!')
443-
444320
def test_load_section_yaml(self):
445321
"""
446322
Test if loading yaml document raises validation errors for Sections with undefined type.
@@ -456,61 +332,55 @@ def test_load_section_yaml(self):
456332
lambda x: x.msg == "Section type undefined" and x.obj.name == "sec_type_empty",
457333
validate(doc).errors))) > 0
458334

459-
def test_load_dtypes_yaml(self):
335+
def load_dtypes_validation(self, doc):
336+
msg_base = 'Dtype of property "%s" currently is "string", but might fit dtype "%s"!'
337+
338+
doc_val = validate(doc)
339+
self.assertError(doc_val, msg_base % ("members_no", "int"))
340+
self.assertError(doc_val, msg_base % ("potential_no", "float"))
341+
self.assertError(doc_val, msg_base % ("dates_no", "date"))
342+
self.assertError(doc_val, msg_base % ("datetimes_no", "datetime"))
343+
self.assertError(doc_val, msg_base % ("times_no", "time"))
344+
self.assertError(doc_val, msg_base % ("sent_no", "boolean"))
345+
self.assertError(doc_val, msg_base % ("Location_no", "2-tuple"))
346+
self.assertError(doc_val, msg_base % ("Coos_no", "3-tuple"))
347+
348+
self.assertError(doc_val, msg_base % ("members_mislabelled", "int"))
349+
self.assertError(doc_val, msg_base % ("potential_mislabelled", "float"))
350+
self.assertError(doc_val, msg_base % ("dates_mislabelled", "date"))
351+
self.assertError(doc_val, msg_base % ("datetimes_mislabelled", "datetime"))
352+
self.assertError(doc_val, msg_base % ("times_mislabelled", "time"))
353+
self.assertError(doc_val, msg_base % ("sent_mislabelled", "boolean"))
354+
self.assertError(doc_val, msg_base % ("texts_mislabelled", "text"))
355+
self.assertError(doc_val, msg_base % ("Location_mislabelled", "2-tuple"))
356+
self.assertError(doc_val, msg_base % ("Coos_mislabelled", "3-tuple"))
357+
358+
def test_load_dtypes_xml(self):
460359
"""
461-
Test if loading yaml document raises validation errors for Properties with undefined dtypes.
360+
Test if loading xml document raises validation errors
361+
for Properties with undefined dtypes.
462362
"""
463363

464-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.yaml")
465-
doc = odml.load(path, "YAML")
466-
467-
self.assertError(validate(doc), 'Dtype of property "members_no" currently is "string", '
468-
'but might fit dtype "int"!')
469-
470-
self.assertError(validate(doc), 'Dtype of property "potential_no" currently is "string", '
471-
'but might fit dtype "float"!')
472-
473-
self.assertError(validate(doc), 'Dtype of property "dates_no" currently is "string", '
474-
'but might fit dtype "date"!')
475-
476-
self.assertError(validate(doc), 'Dtype of property "datetimes_no" currently is "string", '
477-
'but might fit dtype "datetime"!')
478-
479-
self.assertError(validate(doc), 'Dtype of property "times_no" currently is "string", '
480-
'but might fit dtype "time"!')
481-
482-
self.assertError(validate(doc), 'Dtype of property "sent_no" currently is "string", '
483-
'but might fit dtype "boolean"!')
484-
485-
self.assertError(validate(doc), 'Dtype of property "Location_no" currently is "string", '
486-
'but might fit dtype "2-tuple"!')
487-
488-
self.assertError(validate(doc), 'Dtype of property "Coos_no" currently is "string", '
489-
'but might fit dtype "3-tuple"!')
490-
491-
self.assertError(validate(doc), 'Dtype of property "members_mislabelled" currently is '
492-
'"string", but might fit dtype "int"!')
493-
494-
self.assertError(validate(doc), 'Dtype of property "potential_mislabelled" currently is '
495-
'"string", but might fit dtype "float"!')
496-
497-
self.assertError(validate(doc), 'Dtype of property "dates_mislabelled" currently is '
498-
'"string", but might fit dtype "date"!')
499-
500-
self.assertError(validate(doc), 'Dtype of property "datetimes_mislabelled" currently is '
501-
'"string", but might fit dtype "datetime"!')
502-
503-
self.assertError(validate(doc), 'Dtype of property "times_mislabelled" currently is '
504-
'"string", but might fit dtype "time"!')
364+
path = os.path.join(self.dir_path, "resources", "validation_dtypes.xml")
365+
doc = odml.load(path)
366+
self.load_dtypes_validation(doc)
505367

506-
self.assertError(validate(doc), 'Dtype of property "sent_mislabelled" currently is '
507-
'"string", but might fit dtype "boolean"!')
368+
def test_load_dtypes_json(self):
369+
"""
370+
Test if loading json document raises validation errors
371+
for Properties with undefined dtypes.
372+
"""
508373

509-
self.assertError(validate(doc), 'Dtype of property "texts_mislabelled" currently is '
510-
'"string", but might fit dtype "text"!')
374+
path = os.path.join(self.dir_path, "resources", "validation_dtypes.json")
375+
doc = odml.load(path, "JSON")
376+
self.load_dtypes_validation(doc)
511377

512-
self.assertError(validate(doc), 'Dtype of property "Location_mislabelled" currently is '
513-
'"string", but might fit dtype "2-tuple"!')
378+
def test_load_dtypes_yaml(self):
379+
"""
380+
Test if loading yaml document raises validation errors
381+
for Properties with undefined dtypes.
382+
"""
514383

515-
self.assertError(validate(doc), 'Dtype of property "Coos_mislabelled" currently is '
516-
'"string", but might fit dtype "3-tuple"!')
384+
path = os.path.join(self.dir_path, "resources", "validation_dtypes.yaml")
385+
doc = odml.load(path, "YAML")
386+
self.load_dtypes_validation(doc)

0 commit comments

Comments
 (0)