Skip to content

Commit bbecc62

Browse files
committed
[test/validation] Add section sec card test
Adds tests for the Section sub-section cardinality validation. Once the ValidationError obj provides an id attribute, the tests should be refactored.
1 parent 5b431ff commit bbecc62

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/test_validation.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,63 @@ def test_section_properties_cardinality(self):
140140

141141
self.assertTrue(found)
142142

143+
def test_section_sections_cardinality(self):
144+
msg_base = "Section sections cardinality violated"
145+
146+
doc = odml.Document()
147+
# Test no caught warning on empty cardinality
148+
sec = odml.Section(name="sec_empty_cardinality", type="test", parent=doc)
149+
# Check that the current section did not throw any sections cardinality warnings
150+
for err in validate(doc).errors:
151+
if err.obj.id == sec.id:
152+
self.assertNotIn(msg_base, err.msg)
153+
154+
# Test no warning on valid cardinality
155+
sec = odml.Section(name="sec_valid_cardinality", sec_cardinality=(1, 2), parent=doc)
156+
_ = odml.Section(name="sub_sec_valid_cardinality", type="test", parent=sec)
157+
for err in validate(doc).errors:
158+
if err.obj.id == sec.id:
159+
self.assertNotIn(msg_base, err.msg)
160+
161+
# Test maximum value cardinality validation
162+
test_range = 3
163+
test_card = 2
164+
sec = odml.Section(name="sec_invalid_max_val", sec_cardinality=test_card, parent=doc)
165+
for i in range(test_range):
166+
sec_name = "sub_sec_invalid_max_val_%s" % i
167+
_ = odml.Section(name=sec_name, type="test", parent=sec)
168+
169+
test_msg = "%s (maximum %s values, %s found)" % (msg_base, test_card, len(sec.sections))
170+
171+
# Once ValidationErrors provide validation ids, the following can be simplified.
172+
found = False
173+
for err in validate(doc).errors:
174+
if err.obj.id == sec.id and msg_base in err.msg:
175+
self.assertFalse(err.is_error)
176+
self.assertIn(test_msg, err.msg)
177+
found = True
178+
179+
self.assertTrue(found)
180+
181+
# Test minimum value cardinality validation
182+
test_card = (4, None)
183+
184+
sec = odml.Section(name="sec_invalid_min_val", sec_cardinality=test_card, parent=sec)
185+
_ = odml.Section(name="sub_sec_invalid_min_val", type="test", parent=sec)
186+
187+
test_msg = "%s (minimum %s values, %s found)" % (msg_base, test_card[0],
188+
len(sec.sections))
189+
190+
# Once ValidationErrors provide validation ids, the following can be simplified.
191+
found = False
192+
for err in validate(doc).errors:
193+
if err.obj.id == sec.id and msg_base in err.msg:
194+
self.assertFalse(err.is_error)
195+
self.assertIn(test_msg, err.msg)
196+
found = True
197+
198+
self.assertTrue(found)
199+
143200
def test_section_in_terminology(self):
144201
doc = samplefile.parse("""s1[T1]""")
145202
res = validate(doc)

0 commit comments

Comments
 (0)