@@ -35,3 +35,81 @@ def _get_captured_output(self):
35
35
36
36
return out
37
37
38
+ def test_property_values_cardinality (self ):
39
+ # -- Test assignment validation warnings
40
+ doc = odml .Document ()
41
+ sec = odml .Section (name = "sec" , type = "sec_type" , parent = doc )
42
+
43
+ # -- Test cardinality validation warnings on Property init
44
+ # Test warning when setting invalid minimum
45
+ _ = odml .Property (name = "prop_card_min" , values = [1 ], val_cardinality = (2 , None ), parent = sec )
46
+ output = self ._get_captured_output ()
47
+ test_msg = "%s (minimum %s values, %s found)" % (self .msg_base , 2 , 1 )
48
+ self .assertEqual (len (output ), 1 )
49
+ self .assertIn (test_msg , output [0 ])
50
+
51
+ # Test warning when setting invalid maximum
52
+ _ = odml .Property (name = "prop_card_max" , values = [1 , 2 , 3 ], val_cardinality = 2 , parent = sec )
53
+ output = self ._get_captured_output ()
54
+ test_msg = "%s (maximum %s values, %s found)" % (self .msg_base , 2 , 3 )
55
+ self .assertEqual (len (output ), 1 )
56
+ self .assertIn (test_msg , output [0 ])
57
+
58
+ # Test no warning on valid init
59
+ prop_card = odml .Property (name = "prop_card" , values = [1 , 2 ],
60
+ val_cardinality = (1 , 5 ), parent = sec )
61
+ output = self ._get_captured_output ()
62
+ self .assertEqual (output , [])
63
+
64
+ # -- Test cardinality validation warnings on cardinality updates
65
+ # Test warning when setting minimally required values cardinality
66
+ prop_card .val_cardinality = (3 , None )
67
+ output = self ._get_captured_output ()
68
+ test_msg = "%s (minimum %s values, %s found)" % (self .msg_base , 3 , 2 )
69
+ self .assertEqual (len (output ), 1 )
70
+ self .assertIn (test_msg , output [0 ])
71
+
72
+ # Test warning when setting maximally required values cardinality
73
+ prop_card .values = [1 , 2 , 3 ]
74
+ prop_card .val_cardinality = 2
75
+ output = self ._get_captured_output ()
76
+ test_msg = "%s (maximum %s values, %s found)" % (self .msg_base , 2 , 3 )
77
+ self .assertEqual (len (output ), 1 )
78
+ self .assertIn (test_msg , output [0 ])
79
+
80
+ # Test no warning on valid cardinality
81
+ prop_card .val_cardinality = (1 , 10 )
82
+ output = self ._get_captured_output ()
83
+ self .assertEqual (output , [])
84
+
85
+ # Test no warning when setting cardinality to None
86
+ prop_card .val_cardinality = None
87
+ output = self ._get_captured_output ()
88
+ self .assertEqual (output , [])
89
+
90
+ # -- Test cardinality validation warnings on values updates
91
+ # Test warning when violating minimally required values cardinality
92
+ prop_card .val_cardinality = (3 , None )
93
+ prop_card .values = [1 , 2 ]
94
+ output = self ._get_captured_output ()
95
+ test_msg = "%s (minimum %s values, %s found)" % (self .msg_base , 3 , 2 )
96
+ self .assertEqual (len (output ), 1 )
97
+ self .assertIn (test_msg , output [0 ])
98
+
99
+ # Test warning when violating maximally required values cardinality
100
+ prop_card .val_cardinality = (None , 2 )
101
+ prop_card .values = [1 , 2 , 3 ]
102
+ output = self ._get_captured_output ()
103
+ test_msg = "%s (maximum %s values, %s found)" % (self .msg_base , 2 , 3 )
104
+ self .assertEqual (len (output ), 1 )
105
+ self .assertIn (test_msg , output [0 ])
106
+
107
+ # Test no warning when setting correct number of values
108
+ prop_card .values = [1 , 2 ]
109
+ output = self ._get_captured_output ()
110
+ self .assertEqual (output , [])
111
+
112
+ # Test no warning when setting values to None
113
+ prop_card .values = None
114
+ output = self ._get_captured_output ()
115
+ self .assertEqual (output , [])
0 commit comments