Skip to content

Commit c1bbdcb

Browse files
committed
[test/section] Add prop cardinality test
Adds tests for Section set properties cardinality including a general function that can be reused for Section set sections cardinality tests.
1 parent 97e5d22 commit c1bbdcb

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

test/test_section.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,94 @@ def test_export_leaf(self):
978978
self.assertEqual(len(ex3['first'].sections), 1)
979979
self.assertEqual(len(ex3['first']['third']), 0)
980980

981+
def _test_cardinality_re_assignment(self, obj, obj_attribute):
982+
"""
983+
Tests the basic set of both Section properties and sub-sections cardinality.
984+
985+
:param obj: odml Section
986+
:param obj_attribute: string with the cardinality attribute that is supposed to be tested.
987+
Should be either 'prop_cardinality' or 'sec_cardinality'.
988+
"""
989+
oat = obj_attribute
990+
991+
# Test Section prop/sec cardinality reset
992+
for non_val in [None, "", [], (), {}]:
993+
setattr(obj, oat, non_val)
994+
self.assertIsNone(getattr(obj, oat))
995+
setattr(obj, oat, 1)
996+
997+
# Test Section prop/sec cardinality single int max assignment
998+
setattr(obj, oat, 10)
999+
self.assertEqual(getattr(obj, oat), (None, 10))
1000+
1001+
# Test Section prop/sec cardinality tuple max assignment
1002+
setattr(obj, oat, (None, 5))
1003+
self.assertEqual(getattr(obj, oat), (None, 5))
1004+
1005+
# Test Section prop/sec cardinality tuple min assignment
1006+
setattr(obj, oat, (5, None))
1007+
self.assertEqual(getattr(obj, oat), (5, None))
1008+
1009+
# Test Section prop/sec cardinality min/max assignment
1010+
setattr(obj, oat, (1, 5))
1011+
self.assertEqual(getattr(obj, oat), (1, 5))
1012+
1013+
# -- Test Section prop/sec cardinality assignment failures
1014+
with self.assertRaises(ValueError):
1015+
setattr(obj, oat, "a")
1016+
1017+
with self.assertRaises(ValueError):
1018+
setattr(obj, oat, -1)
1019+
1020+
with self.assertRaises(ValueError):
1021+
setattr(obj, oat, (1, "b"))
1022+
1023+
with self.assertRaises(ValueError):
1024+
setattr(obj, oat, (1, 2, 3))
1025+
1026+
with self.assertRaises(ValueError):
1027+
setattr(obj, oat, [1, 2, 3])
1028+
1029+
with self.assertRaises(ValueError):
1030+
setattr(obj, oat, {1: 2, 3: 4})
1031+
1032+
with self.assertRaises(ValueError):
1033+
setattr(obj, oat, (-1, 1))
1034+
1035+
with self.assertRaises(ValueError):
1036+
setattr(obj, oat, (1, -5))
1037+
1038+
with self.assertRaises(ValueError):
1039+
setattr(obj, oat, (5, 1))
1040+
1041+
def test_properties_cardinality(self):
1042+
"""
1043+
Tests the basic assignment rules for Section Properties cardinality
1044+
on init and re-assignment but does not test properties assignment or
1045+
the actual cardinality validation.
1046+
"""
1047+
doc = Document()
1048+
1049+
# -- Test set cardinality on Section init
1050+
# Test empty init
1051+
sec_prop_card_none = Section(name="sec_prop_card_none", type="test", parent=doc)
1052+
self.assertIsNone(sec_prop_card_none.prop_cardinality)
1053+
1054+
# Test single int max init
1055+
sec_card_max = Section(name="prop_cardinality_max", prop_cardinality=10, parent=doc)
1056+
self.assertEqual(sec_card_max.prop_cardinality, (None, 10))
1057+
1058+
# Test tuple init
1059+
sec_card_min = Section(name="prop_cardinality_min", prop_cardinality=(2, None), parent=doc)
1060+
self.assertEqual(sec_card_min.prop_cardinality, (2, None))
1061+
1062+
# -- Test Section properties cardinality re-assignment
1063+
sec = Section(name="prop", prop_cardinality=(None, 10), parent=doc)
1064+
self.assertEqual(sec.prop_cardinality, (None, 10))
1065+
1066+
# Use general method to reduce redundancy
1067+
self._test_cardinality_re_assignment(sec, 'prop_cardinality')
1068+
9811069
def test_link(self):
9821070
pass
9831071

0 commit comments

Comments
 (0)