1
0

test_validation.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import unittest
  2. import odml
  3. import odml.validation
  4. import odml.terminology
  5. from . import test_samplefile as samplefile
  6. validate = odml.validation.Validation
  7. class TestValidation(unittest.TestCase):
  8. def setUp(self):
  9. self.doc = samplefile.SampleFileCreator().create_document()
  10. self.maxDiff = None
  11. def filter_repository_errors(self, errors):
  12. return filter(lambda x: "A section should have an associated "
  13. "repository" not in x.msg, errors)
  14. def test_errorfree(self):
  15. res = validate(self.doc)
  16. self.assertEqual(list(self.filter_repository_errors(res.errors)), [])
  17. def assertError(self, res, err, filter_rep=True, filter_map=False):
  18. """
  19. Passes only if err appears in res.errors
  20. """
  21. errs = res.errors
  22. if filter_rep:
  23. errs = self.filter_repository_errors(errs)
  24. for i in errs:
  25. if err in i.msg:
  26. return
  27. self.assertEqual(errs, err)
  28. def test_section_type(self):
  29. doc = samplefile.parse("""s1[undefined]""")
  30. res = validate(doc)
  31. # the section type is undefined (also in the mapping)
  32. self.assertError(res, "Section type undefined")
  33. def test_section_in_terminology(self):
  34. doc = samplefile.parse("""s1[T1]""")
  35. res = validate(doc)
  36. self.assertError(res, "A section should have an associated repository",
  37. filter_rep=False)
  38. odml.terminology.terminologies['map'] = samplefile.parse("""
  39. s0[t0]
  40. - S1[T1]
  41. """)
  42. doc.sections[0].repository = 'map'
  43. res = validate(doc)
  44. # self.assertEqual(list(self.filter_mapping_errors(res.errors)), [])
  45. self.assertEqual(res.errors, [])
  46. def test_uniques(self):
  47. self.assertRaises(KeyError, samplefile.parse, """
  48. s1[t1]
  49. s1[t1]
  50. """)
  51. self.assertRaises(KeyError, samplefile.parse, """
  52. s1[t1]
  53. - p1
  54. - p1
  55. """)
  56. def test_property_in_terminology(self):
  57. doc = samplefile.parse("""
  58. s1[t1]
  59. - P1
  60. """)
  61. odml.terminology.terminologies['term'] = samplefile.parse("""
  62. S1[T1]
  63. - P1
  64. """)
  65. doc.repository = 'term'
  66. res = validate(doc)
  67. self.assertEqual(res.errors, [])
  68. doc = samplefile.parse("""
  69. s1[t1]
  70. - p1
  71. - P1
  72. """)
  73. doc.repository = 'term'
  74. res = validate(doc)
  75. self.assertError(res, "Property 'p1' not found in terminology")
  76. def test_property_values(self):
  77. # different units
  78. doc = samplefile.parse("""s1[t1]""")
  79. p = odml.Property(name="p1", value=[0, 1])
  80. doc["s1"].append(p)
  81. # missing dependency
  82. p.dependency = "p2"
  83. res = validate(doc)
  84. self.assertError(res, "non-existent dependency object")
  85. def test_property_unique_ids(self):
  86. """
  87. Test if identical ids in properties raise a validation error
  88. """
  89. doc = odml.Document()
  90. sec_one = odml.Section("sec1", parent=doc)
  91. sec_two = odml.Section("sec2", parent=doc)
  92. prop = odml.Property("prop", parent=sec_one)
  93. cprop = prop.clone(keep_id=True)
  94. sec_two.append(cprop)
  95. res = validate(doc)
  96. self.assertError(res, "Duplicate id in Property")
  97. def test_section_unique_ids(self):
  98. """
  99. Test if identical ids in sections raise a validation error.
  100. """
  101. doc = odml.Document()
  102. sec = odml.Section("sec", parent=doc)
  103. csec = sec.clone(keep_id=True)
  104. sec.append(csec)
  105. res = validate(doc)
  106. self.assertError(res, "Duplicate id in Section")