@@ -978,6 +978,147 @@ 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+
1069+ def _test_set_cardinality_method (self , obj , obj_attribute , set_cardinality_method ):
1070+ """
1071+ Tests the basic set convenience method of both Section properties and
1072+ sub-sections cardinality.
1073+
1074+ :param obj: odml Section
1075+ :param obj_attribute: string with the cardinality attribute that is supposed to be tested.
1076+ Should be either 'prop_cardinality' or 'sec_cardinality'.
1077+ :param set_cardinality_method: The convenience method used to set the cardinality.
1078+ """
1079+ oba = obj_attribute
1080+
1081+ # Test Section prop/sec cardinality min assignment
1082+ set_cardinality_method (1 )
1083+ self .assertEqual (getattr (obj , oba ), (1 , None ))
1084+
1085+ # Test Section prop/sec cardinality keyword min assignment
1086+ set_cardinality_method (min_val = 2 )
1087+ self .assertEqual (getattr (obj , oba ), (2 , None ))
1088+
1089+ # Test Section prop/sec cardinality max assignment
1090+ set_cardinality_method (None , 1 )
1091+ self .assertEqual (getattr (obj , oba ), (None , 1 ))
1092+
1093+ # Test Section prop/sec cardinality keyword max assignment
1094+ set_cardinality_method (max_val = 2 )
1095+ self .assertEqual (getattr (obj , oba ), (None , 2 ))
1096+
1097+ # Test Section prop/sec cardinality min max assignment
1098+ set_cardinality_method (1 , 2 )
1099+ self .assertEqual (getattr (obj , oba ), (1 , 2 ))
1100+
1101+ # Test Section prop/sec cardinality keyword min max assignment
1102+ set_cardinality_method (min_val = 2 , max_val = 5 )
1103+ self .assertEqual (getattr (obj , oba ), (2 , 5 ))
1104+
1105+ # Test Section prop/sec cardinality empty reset
1106+ set_cardinality_method ()
1107+ self .assertIsNone (getattr (obj , oba ))
1108+
1109+ # Test Section prop/sec cardinality keyword empty reset
1110+ set_cardinality_method (1 )
1111+ self .assertIsNotNone (getattr (obj , oba ))
1112+ set_cardinality_method (min_val = None , max_val = None )
1113+ self .assertIsNone (getattr (obj , oba ))
1114+
1115+ def test_set_properties_cardinality (self ):
1116+ doc = Document ()
1117+ sec = Section (name = "sec" , type = "test" , parent = doc )
1118+
1119+ # Use general method to reduce redundancy
1120+ self ._test_set_cardinality_method (sec , 'prop_cardinality' , sec .set_properties_cardinality )
1121+
9811122 def test_link (self ):
9821123 pass
9831124
0 commit comments