@@ -191,6 +191,72 @@ def test_children(self):
191191 self .assertEqual (len (ysec_lvl_2 .sections ), 4 )
192192 self .assertEqual (len (ysec_lvl_2 .properties ), 4 )
193193
194+ def _test_cardinality_load (self , obj_attribute , doc , card_dict ,
195+ sec_empty , sec_max , sec_min , sec_full ):
196+ """
197+ Tests the basic set of both Section properties and sub-sections cardinality.
198+
199+ :param obj_attribute: string with the cardinality attribute that is supposed to be tested.
200+ Should be either 'prop_cardinality' or 'sec_cardinality'.
201+ :param doc: loaded odml Document to be tested.
202+ :param card_dict: dictionary containing cardinality conditions mapped to the values that
203+ should have been restored.
204+ """
205+ oat = obj_attribute
206+
207+ sec = doc .sections [sec_empty ]
208+ self .assertIsNone (getattr (sec , oat ))
209+
210+ sec = doc .sections [sec_max ]
211+ self .assertEqual (getattr (sec , oat ), card_dict [sec_max ])
212+
213+ sec = doc .sections [sec_min ]
214+ self .assertEqual (getattr (sec , oat ), card_dict [sec_min ])
215+
216+ sec = doc .sections [sec_full ]
217+ self .assertEqual (getattr (sec , oat ), card_dict [sec_full ])
218+
219+ def test_prop_cardinality (self ):
220+ """
221+ Test saving and loading of Section properties cardinality variants to
222+ and from all supported file formats.
223+ """
224+ doc = odml .Document ()
225+ sec_empty = "card_empty"
226+ sec_max = "card_max"
227+ sec_min = "card_min"
228+ sec_full = "card_full"
229+ card_dict = {
230+ sec_empty : None ,
231+ sec_max : (None , 10 ),
232+ sec_min : (2 , None ),
233+ sec_full : (1 , 5 )
234+ }
235+
236+ _ = odml .Section (name = sec_empty , type = "test" , parent = doc )
237+ _ = odml .Section (name = sec_max , prop_cardinality = card_dict [sec_max ], type = "test" , parent = doc )
238+ _ = odml .Section (name = sec_min , prop_cardinality = card_dict [sec_min ], type = "test" , parent = doc )
239+ _ = odml .Section (name = sec_full , prop_cardinality = card_dict [sec_full ],
240+ type = "test" , parent = doc )
241+
242+ # Test saving to and loading from an XML file
243+ odml .save (doc , self .xml_file )
244+ xml_doc = odml .load (self .xml_file )
245+ self ._test_cardinality_load ("prop_cardinality" , xml_doc , card_dict ,
246+ sec_empty , sec_max , sec_min , sec_full )
247+
248+ # Test saving to and loading from a JSON file
249+ odml .save (doc , self .json_file , "JSON" )
250+ json_doc = odml .load (self .json_file , "JSON" )
251+ self ._test_cardinality_load ("prop_cardinality" , json_doc , card_dict ,
252+ sec_empty , sec_max , sec_min , sec_full )
253+
254+ # Test saving to and loading from a YAML file
255+ odml .save (doc , self .yaml_file , "YAML" )
256+ yaml_doc = odml .load (self .yaml_file , "YAML" )
257+ self ._test_cardinality_load ("prop_cardinality" , yaml_doc , card_dict ,
258+ sec_empty , sec_max , sec_min , sec_full )
259+
194260 def test_link (self ):
195261 pass
196262
0 commit comments