Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

validation.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import unittest
  2. import samplefile
  3. import odml
  4. import odml.validation
  5. import odml.terminology
  6. import odml.mapping
  7. validate = odml.validation.Validation
  8. class TestValidation(unittest.TestCase):
  9. def setUp(self):
  10. self.doc = samplefile.SampleFileCreator().create_document()
  11. self.maxDiff = None
  12. def filter_repository_errors(self, errors):
  13. return filter(lambda x: not "A section should have an associated repository" in x.msg, errors)
  14. def filter_mapping_errors(self, errors):
  15. return filter(lambda x: not x.msg.startswith("mapping:"), errors)
  16. def test_errorfree(self):
  17. res = validate(self.doc)
  18. self.assertEqual(self.filter_repository_errors(res.errors), [])
  19. def assertError(self, res, err, filter_rep=True, filter_map=False):
  20. """
  21. passes only if err appears in res.errors
  22. """
  23. errs = res.errors
  24. if filter_rep: errs = self.filter_repository_errors(errs)
  25. if filter_map: errs = self.filter_mapping_errors(errs)
  26. for i in errs:
  27. if err in i.msg:
  28. return
  29. self.assertEqual(errs, err)
  30. def test_section_type(self):
  31. doc = samplefile.parse("""s1[undefined]""")
  32. res = validate(doc)
  33. # the section type is undefined (also in the mapping)
  34. self.assertError(res, "Section type undefined")
  35. def test_section_in_terminology(self):
  36. doc = samplefile.parse("""s1[T1]""")
  37. res = validate(doc)
  38. self.assertError(res, "A section should have an associated repository", filter_rep=False)
  39. odml.terminology.terminologies['map'] = samplefile.parse("""
  40. s0[t0]
  41. - S1[T1]
  42. """)
  43. odml.mapping.unmap_document(doc)
  44. doc.sections[0].repository = 'map'
  45. res = validate(doc)
  46. # TODO: mappings don't take over the repository attribute yet
  47. # thus the mapped equivalent of the document would still raise the error
  48. self.assertEqual(self.filter_mapping_errors(res.errors), [])
  49. def test_uniques(self):
  50. doc = samplefile.parse("""
  51. s1[t1]
  52. s1[t1]
  53. """)
  54. res = validate(doc)
  55. self.assertError(res, "name/type combination must be unique")
  56. doc = samplefile.parse("""
  57. s1[t1]
  58. - p1
  59. - p1
  60. """)
  61. res = validate(doc)
  62. self.assertError(res, "Object names must be unique")
  63. def test_mapping_errors(self):
  64. # 1. mappings don't resolve
  65. doc = samplefile.parse("""s1[t1] mapping [T2]""")
  66. odml.terminology.terminologies['map'] = samplefile.parse("S1[T1]")
  67. res = validate(doc)
  68. self.assertError(res, "No section of type 'T2' could be found")
  69. # 2. mapped property does not resolve
  70. doc = samplefile.parse("""
  71. s1[t1]
  72. - p1 mapping [T1:P1]
  73. """)
  74. res = validate(doc)
  75. self.assertError(res, "No property named 'P1' could be found in section 'S1'")
  76. def test_invalid_mapped_document(self):
  77. # the following mapping creates an illegal document
  78. # in which the property P1 is found twice in the same section
  79. doc = samplefile.parse("""
  80. s1[t1]
  81. - p1 mapping [T1:P1]
  82. - P1
  83. """)
  84. odml.terminology.terminologies['map'] = samplefile.parse("""
  85. S1[T1]
  86. - P1
  87. """)
  88. res = validate(doc)
  89. self.assertError(res, "mapping: Object names must be unique")
  90. def test_property_in_terminology(self):
  91. doc = samplefile.parse("""
  92. s1[t1]
  93. - P1
  94. """)
  95. odml.terminology.terminologies['term'] = samplefile.parse("""
  96. S1[T1]
  97. - P1
  98. """)
  99. doc.repository = 'term'
  100. res = validate(doc)
  101. self.assertEqual(res.errors, [])
  102. doc = samplefile.parse("""
  103. s1[t1]
  104. - p1
  105. - P1
  106. """)
  107. doc.repository = 'term'
  108. res = validate(doc)
  109. self.assertError(res, "Property 'p1' not found in terminology")
  110. def test_property_values(self):
  111. # different units
  112. doc = samplefile.parse("""s1[t1]""")
  113. p = odml.Property(name="p1", value=[0,1])
  114. doc["s1"].append(p)
  115. p.values[0].unit = "km"
  116. p.values[1].unit = "mV"
  117. res = validate(doc)
  118. self.assertError(res, "the same unit")
  119. del p.values[1]
  120. # missing dependency
  121. p.dependency = "p2"
  122. res = validate(doc)
  123. self.assertError(res, "non-existant dependency object")
  124. if __name__ == '__main__':
  125. unittest.main()