def test_get_merged_equivalent(self): sec = Section(name="parent") mersec = Section(name="merged_section") merprop_other = Property(name="other_prop", value="other") merprop = Property(name="prop", value=[1, 2, 3]) # Check None on unset parent. prop = Property(name="prop") self.assertIsNone(prop.get_merged_equivalent()) # Check None on parent without merged Section. prop.parent = sec self.assertIsNone(prop.get_merged_equivalent()) # Check None on parent with merged Section but no attached Property. sec.merge(mersec) self.assertIsNone(prop.get_merged_equivalent()) # Check None on parent with merged Section and unequal Property. merprop_other.parent = mersec self.assertIsNone(prop.get_merged_equivalent()) # Check receiving merged equivalent Property. merprop.parent = mersec self.assertIsNotNone(prop.get_merged_equivalent()) self.assertEqual(prop.get_merged_equivalent(), merprop)
def setUp(self): doc = Document() sec = Section(name="sec1", type="test", parent=doc) Section(name="sec2", type="test", parent=sec) Property(name="prop1", values=[1.3], parent=sec) self.doc = doc
def test_create_section(self): root = Section("root") self.assertEqual(len(root.sections), 0) name = "subsec" type = "subtype" oid = "79b613eb-a256-46bf-84f6-207df465b8f7" subsec = root.create_section(name, type, oid) self.assertEqual(len(root.sections), 1) self.assertEqual(subsec.parent, root) self.assertEqual(root.sections[name], subsec) self.assertEqual(root.sections[name].type, type) self.assertEqual(root.sections[name].oid, oid) name = "othersec" subsec = root.create_section(name) self.assertEqual(len(root.sections), 2) self.assertEqual(subsec.parent, root) self.assertEqual(root.sections[name], subsec) self.assertEqual(root.sections[name].type, "undefined") name = "subsubsec" subsec = root.sections[0].create_section(name) self.assertEqual(len(root.sections), 2) self.assertEqual(subsec.parent, root.sections[0]) self.assertEqual(len(root.sections[0].sections), 1) self.assertEqual(root.sections[0].sections[0].name, name)
def test_extend(self): doc = Document() self.assertListEqual(doc.sections, []) # Test extend with Section list doc.extend([Section(name="sec_one"), Section(name="sec_two")]) self.assertEqual(len(doc), 2) self.assertEqual(len(doc.sections), 2) self.assertEqual(doc.sections[0].name, "sec_one") # Test fail on non iterable with self.assertRaises(TypeError): doc.extend(1) self.assertEqual(len(doc.sections), 2) # Test fail on non Section entry with self.assertRaises(ValueError): doc.extend([Document()]) with self.assertRaises(ValueError): doc.extend([Property(name="prop")]) with self.assertRaises(ValueError): doc.extend([5]) self.assertEqual(len(doc.sections), 2) # Test fail on same name entities with self.assertRaises(KeyError): doc.extend([Section(name="sec_three"), Section(name="sec_one")]) self.assertEqual(len(doc.sections), 2)
def test_insert(self): doc = Document() sec_one = Section(name="sec_one", parent=doc) sec_two = Section(name="sec_two", parent=doc) subsec = Section(name="sec_three") self.assertNotEqual(doc.sections[1].name, subsec.name) doc.insert(1, subsec) self.assertEqual(len(doc.sections), 3) self.assertEqual(doc.sections[1].name, subsec.name) self.assertEqual(doc.sections[0].name, sec_one.name) self.assertEqual(doc.sections[2].name, sec_two.name) self.assertEqual(subsec.parent, doc) # Test invalid object with self.assertRaises(ValueError): doc.insert(1, Document()) with self.assertRaises(ValueError): doc.insert(1, Property(name="prop_one")) with self.assertRaises(ValueError): doc.insert(1, "some info") self.assertEqual(len(doc), 3) # Test same name entries with self.assertRaises(ValueError): doc.insert(0, subsec) self.assertEqual(len(doc), 3)
def test_sections_cardinality(self): """ Tests the basic assignment rules for Section sections cardinality on init and re-assignment but does not test sections assignment or the actual cardinality validation. """ doc = Document() # -- Test set cardinality on Section init # Test empty init sec_card_none = Section(name="sec_cardinality_none", type="test", parent=doc) self.assertIsNone(sec_card_none.sec_cardinality) # Test single int max init sec_card_max = Section(name="sec_cardinality_max", sec_cardinality=10, parent=doc) self.assertEqual(sec_card_max.sec_cardinality, (None, 10)) # Test tuple init sec_card_min = Section(name="sec_cardinality_min", sec_cardinality=(2, None), parent=doc) self.assertEqual(sec_card_min.sec_cardinality, (2, None)) # -- Test Section properties cardinality re-assignment sec = Section(name="sec", sec_cardinality=(None, 10), parent=doc) self.assertEqual(sec.sec_cardinality, (None, 10)) # Use general method to reduce redundancy self._test_cardinality_re_assignment(sec, 'sec_cardinality')
def setUp(self): """ Set up local temporary terminology files in a temporary folder """ tmp_dir = create_test_dir(__file__) tmp_name = os.path.basename(tmp_dir) main_name = "%s_main.xml" % tmp_name main_file_path = os.path.join(tmp_dir, main_name) main_url = "file://%s" % pathname2url(main_file_path) include_name = "%s_include.xml" % tmp_name include_file_path = os.path.join(tmp_dir, include_name) include_url = "file://%s" % pathname2url(include_file_path) include_doc = Document() _ = Section(name="include_sec", type="test", parent=include_doc) save(include_doc, include_file_path) main_doc = Document() _ = Section(name="main_sec", type="test", include=include_url, parent=main_doc) save(main_doc, main_file_path) self.main_terminology_url = main_url self.temp_dir_base = tmp_name
def test_reorder(self): # Test reorder of document sections doc = Document() sec_one = Section(name="sec_one", parent=doc) sec_two = Section(name="sec_two", parent=doc) sec_three = Section(name="sec_three", parent=doc) self.assertEqual(doc.sections[0].name, sec_one.name) self.assertEqual(doc.sections[2].name, sec_three.name) sec_three.reorder(0) self.assertEqual(doc.sections[0].name, sec_three.name) self.assertEqual(doc.sections[2].name, sec_two.name) # Test reorder of document sections sec = Section(name="main") sec_one = Section(name="sec_one", parent=sec) sec_two = Section(name="sec_two", parent=sec) sec_three = Section(name="sec_three", parent=sec) self.assertEqual(sec.sections[0].name, sec_one.name) self.assertEqual(sec.sections[2].name, sec_three.name) sec_three.reorder(0) self.assertEqual(sec.sections[0].name, sec_three.name) self.assertEqual(sec.sections[2].name, sec_two.name) # Test Exception on unconnected section with self.assertRaises(ValueError): sec.reorder(0)
def handle_sub_container(helper, node, sec, sub_sec_type): # We might need to handle the case, when a container holds # only the content of one xml element and does not contain # the content and attributes of this xml element as a sole # list element but as many elements within an OrderedDict. if isinstance(node[helper.section_name], list): for (idx, title_node) in enumerate(node[helper.section_name]): sec_name = "%s_%d" % (helper.section_name, idx + 1) sub_sec = Section(name=sec_name, type=sub_sec_type, parent=sec) helper.item_func(helper, title_node, sub_sec) else: sub_sec_name = "%s_1" % helper.section_name sub_sec = Section(name=sub_sec_name, type=sub_sec_type, parent=sec) helper.item_func(helper, node[helper.section_name], sub_sec)
def test_simple_attributes(self): sec_name = "sec name" sec_type = "sec type" sec_def = "an odml test section" sec_ref = "from over there" sec = Section(name=sec_name, type=sec_type, definition=sec_def, reference=sec_ref) self.assertEqual(sec.name, sec_name) self.assertEqual(sec.type, sec_type) self.assertEqual(sec.definition, sec_def) self.assertEqual(sec.reference, sec_ref) # Test setting attributes sec.name = "%s_edit" % sec_name self.assertEqual(sec.name, "%s_edit" % sec_name) sec.type = "%s_edit" % sec_type self.assertEqual(sec.type, "%s_edit" % sec_type) sec.definition = "%s_edit" % sec_def self.assertEqual(sec.definition, "%s_edit" % sec_def) sec.reference = "%s_edit" % sec_ref self.assertEqual(sec.reference, "%s_edit" % sec_ref) # Test setting attributes to None when '' is passed. sec.reference = "" self.assertIsNone(sec.reference) sec.definition = "" self.assertIsNone(sec.definition)
def test_parent(self): s = Section("Section") self.assertIsNone(s.parent) s.parent = None self.assertIsNone(s.parent) s.parent = Document() self.assertIsInstance(s.parent, BaseDocument) self.assertIsInstance(s.parent._sections[0], BaseSection) """ Test if child is removed from _sections of a parent after assigning a new parent to the child """ p = s.parent s.parent = Section("S") self.assertEqual(len(p._sections), 0) s = Section("section_doc", parent=Document()) self.assertIsInstance(s.parent, BaseDocument) self.assertEqual(len(s.parent._sections), 1) p = s.parent s.parent = None self.assertEqual(len(p._sections), 0) self.assertEqual(s.parent, None) s = Section("section_sec", parent=Section("S")) self.assertIsInstance(s.parent, BaseSection) self.assertEqual(len(s.parent._sections), 1) p = s.parent s.parent = None self.assertEqual(len(p._sections), 0) self.assertEqual(s.parent, None) with self.assertRaises(ValueError): Section("section_property", parent=Property("P"))
def test_property(self): """ Test if a property and its attributes get converted correctly from rdf to odml. """ doc = Document() sec1 = Section(name="sec1", type="test", parent=doc) prop2 = Property(name="numbers", definition="any number", dtype="float", parent=sec1, values=[1, 3.4, 67.8, -12], unit="meter", uncertainty=0.8, value_origin="force", reference="Experiment 1") rdf_writer = RDFWriter(doc).get_rdf_str() rdf_reader = RDFReader().from_string(rdf_writer, "turtle") prop = rdf_reader[0].sections[0].properties["numbers"] self.assertEqual(prop.name, "numbers") self.assertEqual(prop.dtype, "float") self.assertEqual(prop.id, prop2.id) self.assertEqual(prop.parent, rdf_reader[0].sections[0]) self.assertEqual(len(prop.values), 4) self.assertEqual(prop.values, [1, 3.4, 67.8, -12]) self.assertEqual(prop.definition, "any number") self.assertEqual(prop.unit, "meter") self.assertEqual(prop.uncertainty, "0.8") self.assertEqual(prop.value_origin, "force") self.assertEqual(prop.reference, "Experiment 1")
def test_name(self): # Test id is used when name is not provided prop = Property() self.assertIsNotNone(prop.name) self.assertEqual(prop.name, prop.id) # Test name is properly set on init name = "rumpelstilzchen" prop = Property(name) self.assertEqual(prop.name, name) # Test name can be properly set on single and connected Properties prop = Property() self.assertNotEqual(prop.name, "prop") prop.name = "prop" self.assertEqual(prop.name, "prop") sec = Section() prop_a = Property(parent=sec) self.assertNotEqual(prop_a.name, "prop_a") prop_a.name = "prop_a" self.assertEqual(prop_a.name, "prop_a") # Test property name can be changed with siblings prop_b = Property(name="prop_b", parent=sec) self.assertEqual(prop_b.name, "prop_b") prop_b.name = "prop" self.assertEqual(prop_b.name, "prop") # Test property name set will fail on existing sibling with same name with self.assertRaises(KeyError): prop_b.name = "prop_a" self.assertEqual(prop_b.name, "prop")
def test_reorder(self): sec = Section() prop_zero = Property(name="prop_zero", parent=sec) prop_one = Property(name="prop_one", parent=sec) prop_two = Property(name="prop_two", parent=sec) prop_three = Property(name="prop_three", parent=sec) self.assertEqual(sec.properties[0].name, prop_zero.name) self.assertEqual(sec.properties[2].name, prop_two.name) prop_two.reorder(0) self.assertEqual(sec.properties[0].name, prop_two.name) self.assertEqual(sec.properties[1].name, prop_zero.name) self.assertEqual(sec.properties[2].name, prop_one.name) self.assertEqual(sec.properties[3].name, prop_three.name) prop_two.reorder(2) self.assertEqual(sec.properties[0].name, prop_zero.name) self.assertEqual(sec.properties[1].name, prop_one.name) self.assertEqual(sec.properties[2].name, prop_two.name) self.assertEqual(sec.properties[3].name, prop_three.name) # Test Exception on unconnected property prop = Property(name="main") with self.assertRaises(ValueError): prop.reorder(0)
def parse_datacite_dict(doc): """ :param doc: python dict containing datacite conform data to be parsed. """ if not doc or "resource" not in doc: msg = "Could not find root. " msg += "Please escape any XML namespace using the '-n' command line option." raise ParserException(msg) datacite_root = doc["resource"] if "identifier" not in datacite_root: raise ParserException("Could not find identifier (DOI) node") odml_doc = Document() odml_doc.repository = "https://terminologies.g-node.org/v1.1/terminologies.xml" odml_doc.date = date.today().isoformat() root_sec = Section(name="DataCite", type="data_reference", parent=odml_doc) supported_tags = setup_supported_tags() for node_tag in datacite_root: if node_tag in supported_tags: helper = supported_tags[node_tag] helper.func(helper, datacite_root[node_tag], root_sec) else: print("[Warning] Ignoring unsupported root node '%s'" % node_tag) return odml_doc
def handle_sec(helper, node, root_sec): if not node: return sec_type = "datacite/%s" % camel_to_snake(helper.section_name) sec = Section(name=helper.section_name, type=sec_type, parent=root_sec) handle_props(helper, node, sec)
def handle_container(helper, node, root_sec): if not node or helper.section_name not in node: return sec_cont_type = "datacite/%s" % camel_to_snake(helper.container_name) sub_sec_type = "datacite/%s" % camel_to_snake(helper.section_name) sec = Section(name=helper.container_name, type=sec_cont_type, parent=root_sec) handle_sub_container(helper, node, sec, sub_sec_type)
def test_get_path(self): doc = Document() sec = Section(name="parent", parent=doc) # Check root path for a detached Property. prop = Property(name="prop") self.assertEqual("/", prop.get_path()) # Check absolute path of Property in a Document. prop.parent = sec self.assertEqual("/%s:%s" % (sec.name, prop.name), prop.get_path())
def handle_geo_entry(helper_list, node, sec, sub_sec_name, sub_sec_type): sub_sec = Section(name=sub_sec_name, type=sub_sec_type, parent=sec) for entry in node: if entry in helper_list: try: Property(name=entry, dtype=DType.float, values=node[entry], parent=sub_sec) except ValueError: print("[Warning] Skipping invalid '%s' value '%s'" % (entry, node[entry]))
def test_parent(self): p = Property("property_section", parent=Section("S")) self.assertIsInstance(p.parent, BaseSection) self.assertEqual(len(p.parent._props), 1) """ Test if child is removed from _props of a parent after assigning a new parent to the child """ prop_parent = p.parent p.parent = Section("S1") self.assertEqual(len(prop_parent._props), 0) self.assertIsInstance(p.parent, BaseSection) self.assertIsInstance(p.parent._props[0], BaseProperty) prop_parent = p.parent p.parent = None self.assertIsNone(p.parent) self.assertEqual(len(prop_parent._props), 0) with self.assertRaises(ValueError): Property("property_prop", parent=Property("P")) with self.assertRaises(ValueError): Property("property_doc", parent=Document())
def test_append(self): doc = Document() self.assertListEqual(doc.sections, []) # Test append Section sec = Section(name="sec_one") doc.append(sec) self.assertEqual(len(doc.sections), 1) self.assertEqual(sec.parent, doc) # Test fail on Section list or tuple append with self.assertRaises(ValueError): doc.append([Section(name="sec_two"), Section(name="sec_three")]) with self.assertRaises(ValueError): doc.append((Section(name="sec_two"), Section(name="sec_three"))) self.assertEqual(len(doc.sections), 1) # Test fail on unsupported value with self.assertRaises(ValueError): doc.append(Document()) with self.assertRaises(ValueError): doc.append("Section") with self.assertRaises(ValueError): doc.append(Property(name="prop")) # Test fail on same name entities with self.assertRaises(KeyError): doc.append(Section(name="sec_one")) self.assertEqual(len(doc.sections), 1)
def test_create_property(self): root = Section("root") self.assertEqual(len(root.properties), 0) name = "prop" oid = "79b613eb-a256-46bf-84f6-207df465b8f7" prop = root.create_property(name, oid=oid) self.assertEqual(len(root.properties), 1) self.assertEqual(prop.parent, root) self.assertEqual(root.properties[name].oid, oid) name = "test_values" values = ["a", "b"] prop = root.create_property(name, value=values) self.assertEqual(len(root.properties), 2) self.assertEqual(root.properties[name].values, values) name = "test_dtype" dtype = "str" prop = root.create_property(name, dtype=dtype) self.assertEqual(len(root.properties), 3) self.assertEqual(root.properties[name].dtype, dtype) name = "test_dtype_fail" dtype = "I do not exist" prop = root.create_property(name, dtype=dtype) self.assertIsNone(prop.dtype)
def test_name(self): # Test id is used when name is not provided s = Section() self.assertIsNotNone(s.name) self.assertEqual(s.name, s.id) # Test name is properly set on init name = "rumpelstilzchen" s = Section(name) self.assertEqual(s.name, name) name = "rumpelstilzchen" s = Section(name=name) self.assertEqual(s.name, name) # Test name can be properly set on single and connected Sections sec = Section() self.assertNotEqual(sec.name, "sec") sec.name = "sec" self.assertEqual(sec.name, "sec") subsec_a = Section(parent=sec) self.assertNotEqual(subsec_a.name, "subsec_a") subsec_a.name = "subsec_a" self.assertEqual(subsec_a.name, "subsec_a") # Test subsection name can be changed with siblings subsec_b = Section(name="subsec_b", parent=sec) self.assertEqual(subsec_b.name, "subsec_b") subsec_b.name = "subsec" self.assertEqual(subsec_b.name, "subsec") # Test subsection name set will fail on existing sibling with same name with self.assertRaises(KeyError): subsec_b.name = "subsec_a" self.assertEqual(subsec_b.name, "subsec") # Test section name set will fail on existing same name document sibling doc = Document() sec_a = Section(name="a", parent=doc) sec_b = Section(name="b", parent=doc) with self.assertRaises(KeyError): sec_b.name = "a"
def test_section(self): """ Test if a section and its attributes get converted correctly from rdf to odml. """ doc = Document() sec1 = Section(name="sec1", type="test", parent=doc, definition="Interesting stuff.", reference="The Journal") Section(name="sec2", type="test", parent=sec1) rdf_writer = RDFWriter(doc).get_rdf_str() rdf_reader = RDFReader().from_string(rdf_writer, "turtle") self.assertEqual(rdf_reader[0].sections[0].name, "sec1") self.assertEqual(rdf_reader[0].sections[0].type, "test") self.assertEqual(rdf_reader[0].sections[0].id, sec1.id) self.assertEqual(rdf_reader[0].sections[0].definition, "Interesting stuff.") self.assertEqual(rdf_reader[0].sections[0].reference, "The Journal") self.assertEqual(rdf_reader[0].sections[0].parent, rdf_reader[0]) self.assertEqual(len(rdf_reader[0].sections[0].sections), 1)
def test_id(self): s = Section(name="S") self.assertIsNotNone(s.id) s = Section("S", oid="79b613eb-a256-46bf-84f6-207df465b8f7") self.assertEqual(s.id, "79b613eb-a256-46bf-84f6-207df465b8f7") s = Section("S", oid="id") self.assertNotEqual(s.id, "id") # Make sure id cannot be reset programmatically. with self.assertRaises(AttributeError): s.id = "someId"
def test_new_id(self): sec = Section(name="sec") old_id = sec.id # Test assign new generated id. sec.new_id() self.assertNotEqual(old_id, sec.id) # Test assign new custom id. old_id = sec.id sec.new_id("79b613eb-a256-46bf-84f6-207df465b8f7") self.assertNotEqual(old_id, sec.id) self.assertEqual("79b613eb-a256-46bf-84f6-207df465b8f7", sec.id) # Test invalid custom id exception. with self.assertRaises(ValueError): sec.new_id("crash and burn")
def test_clone(self): sec = Section(name="parent") # Check different id. prop = Property(name="original") clone_prop = prop.clone() self.assertNotEqual(prop.id, clone_prop.id) # Check parent removal in clone. prop.parent = sec clone_prop = prop.clone() self.assertIsNotNone(prop.parent) self.assertIsNone(clone_prop.parent) # Check keep_id prop = Property(name="keepid") clone_prop = prop.clone(True) self.assertEqual(prop.id, clone_prop.id)
def test_set_values_cardinality(self): doc = Document() sec = Section(name="sec", type="sec_type", parent=doc) prop = Property(name="prop", val_cardinality=1, parent=sec) # Test Property values cardinality min assignment prop.set_values_cardinality(1) self.assertEqual(prop.val_cardinality, (1, None)) # Test Property values cardinality keyword min assignment prop.set_values_cardinality(min_val=2) self.assertEqual(prop.val_cardinality, (2, None)) # Test Property values cardinality max assignment prop.set_values_cardinality(None, 1) self.assertEqual(prop.val_cardinality, (None, 1)) # Test Property values cardinality keyword max assignment prop.set_values_cardinality(max_val=2) self.assertEqual(prop.val_cardinality, (None, 2)) # Test Property values cardinality min max assignment prop.set_values_cardinality(1, 2) self.assertEqual(prop.val_cardinality, (1, 2)) # Test Property values cardinality keyword min max assignment prop.set_values_cardinality(min_val=2, max_val=5) self.assertEqual(prop.val_cardinality, (2, 5)) # Test Property values cardinality empty reset prop.set_values_cardinality() self.assertIsNone(prop.val_cardinality) # Test Property values cardinality keyword empty reset prop.set_values_cardinality(1) self.assertIsNotNone(prop.val_cardinality) prop.set_values_cardinality(min_val=None, max_val=None) self.assertIsNone(prop.val_cardinality)
def handle_geo_locations(_, node, sec): sub_type_base = "datacite/geo_location" point_list = ["pointLongitude", "pointLatitude"] box_list = ["westBoundLongitude", "eastBoundLongitude", "southBoundLatitude", "northBoundLatitude"] for elem in node: if elem == "geoLocationPlace": Property(name=elem, values=node[elem], parent=sec) elif elem == "geoLocationPoint": sec_type = "%s/%s" % (sub_type_base, camel_to_snake(elem)) handle_geo_entry(point_list, node[elem], sec, elem, sec_type) elif elem == "geoLocationBox": sec_type = "%s/%s" % (sub_type_base, camel_to_snake(elem)) handle_geo_entry(box_list, node[elem], sec, elem, sec_type) elif elem == "geoLocationPolygon": sub_type = "%s/%s" % (sub_type_base, camel_to_snake(elem)) sub_sec = Section(name=elem, type=sub_type, parent=sec) for (idx, point) in enumerate(node[elem]["polygonPoint"]): point_name = "polygonPoint_%d" % (idx + 1) sec_type = "%s/%s" % (sub_type_base, camel_to_snake("polygonPoint")) handle_geo_entry(point_list, point, sub_sec, point_name, sec_type)
def test_clone_keep_id(self): # Check id kept in clone. sec = Section(name="original") clone_sec = sec.clone(keep_id=True) self.assertEqual(sec, clone_sec) self.assertEqual(sec.id, clone_sec.id) # Check cloned child Sections keep their ids. Section(name="sec_one", parent=sec) Section(name="sec_two", parent=sec) clone_sec = sec.clone(keep_id=True) self.assertListEqual(sec.sections, clone_sec.sections) self.assertEqual(sec.sections["sec_one"], clone_sec.sections["sec_one"]) self.assertEqual(sec.sections["sec_one"].id, clone_sec.sections["sec_one"].id) # Check cloned child Properties keep their ids. Property(name="prop_one", parent=sec) Property(name="prop_two", parent=sec) clone_sec = sec.clone(keep_id=True) self.assertListEqual(sec.properties, clone_sec.properties) self.assertEqual(sec.properties["prop_one"], clone_sec.properties["prop_one"]) self.assertEqual(sec.properties["prop_one"].id, clone_sec.properties["prop_one"].id)
def test_clone(self): # Check parent removal in clone. psec = Section(name="parent") sec = Section(name="original", parent=psec) clone_sec = sec.clone() self.assertEqual(sec.parent, psec) self.assertIsNone(clone_sec.parent) # Check new id in clone. sec = Section(name="original") clone_sec = sec.clone() self.assertEqual(sec, clone_sec) self.assertNotEqual(sec.id, clone_sec.id) # Check child Sections and Properties are cloned and have new ids. Section(name="sec_one", parent=sec) Section(name="sec_two", parent=sec) Property(name="prop_one", parent=sec) Property(name="prop_two", parent=sec) clone_sec = sec.clone() # Check sections self.assertListEqual(sec.sections, clone_sec.sections) self.assertEqual(sec.sections["sec_one"], clone_sec.sections["sec_one"]) self.assertNotEqual(sec.sections["sec_one"].id, clone_sec.sections["sec_one"].id) # Check properties self.assertListEqual(sec.properties, clone_sec.properties) self.assertEqual(sec.properties["prop_one"], clone_sec.properties["prop_one"]) self.assertNotEqual(sec.properties["prop_one"].id, clone_sec.properties["prop_one"].id) # Check child Sections and Properties are not cloned. clone_sec = sec.clone(children=False) self.assertListEqual(clone_sec.sections, []) self.assertListEqual(clone_sec.properties, [])
def setUp(self): self.root_section = Section("Type test", "test") self.root_section.append(Section("sub_1", "sub_1")) self.root_section.append(Section("sub_2", "sub_2")) self.root_section.append(Section("sub_2_b", "sub_2")) self.root_section.append(Section("sub_3", "sub_2/sub_3"))
class TestFindSections(unittest.TestCase): def setUp(self): self.root_section = Section("Type test", "test") self.root_section.append(Section("sub_1", "sub_1")) self.root_section.append(Section("sub_2", "sub_2")) self.root_section.append(Section("sub_2_b", "sub_2")) self.root_section.append(Section("sub_3", "sub_2/sub_3")) def test_find_by_name(self): ret = self.root_section.find("sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find("unknown_type") self.assertIsNone(ret) def test_find_by_type(self): ret = self.root_section.find(type="sub_1") self.assertTrue(ret is not None and ret.type == "sub_1") ret = self.root_section.find(type="sub_2", findAll=True) self.assertTrue(len(ret) == 2) ret = self.root_section.find(key=None, type="sub_2", findAll=True, include_subtype=True) self.assertTrue(len(ret) == 3) def test_find_by_name_and_type(self): ret = self.root_section.find(key="sub_1", type="sub_1") self.assertTrue(ret.name == "sub_1") ret = self.root_section.find(key="sub_1", type="sub_2") self.assertIsNone(ret)
def test_path(self): sec = Section(name="center") self.assertEqual(sec.get_path(), "/") subsec = Section(name="leaf", parent=sec) self.assertEqual(subsec.get_path(), "/leaf") doc = Document() sec.parent = doc self.assertEqual(sec.get_path(), "/center") self.assertEqual(subsec.get_path(), "/center/leaf") top = Section(name="top", parent=doc) sec.parent = top self.assertEqual(sec.get_path(), "/top/center") self.assertEqual(subsec.get_path(), "/top/center/leaf") subsec.parent = None self.assertEqual(subsec.get_path(), "/")
def test_comparison(self): sec_name = "sec name" sec_type = "sec type" sec_def = "an odml test section" sec_ref = "from over there" sec_a = Section(name=sec_name, type=sec_type, definition=sec_def, reference=sec_ref) sec_b = Section(name=sec_name, type=sec_type, definition=sec_def, reference=sec_ref) self.assertEqual(sec_a, sec_b) sec_b.name = "newSecName" self.assertNotEqual(sec_a, sec_b) sec_b.name = sec_name # Test section equality with subsections # Test equality with subsection of different entities # with same content and same children order subsec_aa = Section(name="subsecA", type=sec_type, definition=sec_def, reference=sec_ref) subsec_ab = Section(name="subsecB", type=sec_type, definition=sec_def, reference=sec_ref) subsec_ba = Section(name="subsecA", type=sec_type, definition=sec_def, reference=sec_ref) subsec_bb = Section(name="subsecB", type=sec_type, definition=sec_def, reference=sec_ref) sec_a.extend([subsec_aa, subsec_ab]) sec_b.extend([subsec_ba, subsec_bb]) self.assertEqual(sec_a, sec_b) self.assertEqual(sec_a.sections, sec_b.sections) sec_b.sections[0].name = "newSubsecA" self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.sections, sec_b.sections) sec_b.sections[0].name = "subsecA" # Test inequality with different number of children sec_b.remove(sec_b.sections[1]) self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.sections, sec_b.sections) # Test equality with subsection of different entities # with same content and different children order sec_b.remove(sec_b.sections[0]) sec_b.extend([subsec_bb, subsec_ba]) self.assertEqual(sec_a, sec_b) self.assertEqual(sec_a.sections, sec_b.sections) sec_b.sections[0].name = "newSubsecB" self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.sections, sec_b.sections) sec_b.sections[0].name = "subsecB" # Test section equality with properties # Test equality with properties of different entities # with same content and same children order prop_aa = Property(name="propA", definition="propDef") prop_ab = Property(name="propB", definition="propDef") prop_ba = Property(name="propA", definition="propDef") prop_bb = Property(name="propB", definition="propDef") sec_a.extend([prop_aa, prop_ab]) sec_b.extend([prop_ba, prop_bb]) self.assertEqual(sec_a, sec_b) self.assertEqual(sec_a.properties, sec_b.properties) sec_b.properties[0].name = "newPropA" self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.properties, sec_b.properties) sec_b.properties[0].name = "propA" # Test inequality with different number of children sec_b.remove(sec_b.properties[1]) self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.properties, sec_b.properties) # Test equality with properties of different entities # with same content and different children order sec_b.remove(sec_b.properties[0]) sec_b.extend([prop_bb, prop_ba]) self.assertEqual(sec_a, sec_b) self.assertEqual(sec_a.properties, sec_b.properties) sec_b.properties[0].name = "newPropB" self.assertNotEqual(sec_a, sec_b) self.assertNotEqual(sec_a.properties, sec_b.properties)
def test_merge(self): # -- Root level Section merge tests source = Section(name="source", definition="def", reference="ref") destination = Section(name="destination") destination.merge(source) self.assertEqual(destination.definition, source.definition) self.assertEqual(destination.reference, source.reference) # -- First child level Section merge tests s_sec_one = Section(name="lvl", type="one", definition="def", parent=source) s_sec_two = Section(name="other", type="one", parent=source) d_sec_one = Section(name="lvl", type="one", parent=destination) self.assertEqual(len(destination), 1) self.assertIsNone(destination.sections["lvl"].definition) self.assertIsNone(destination.sections["lvl"].reference) destination.merge(source) self.assertEqual(len(destination), 2) self.assertEqual(destination.sections["lvl"].definition, s_sec_one.definition) self.assertEqual(destination.sections["lvl"].reference, s_sec_one.reference) self.assertEqual(destination.sections["other"], s_sec_two) # -- Root level Property merge tests source = Section(name="source") destination = Section(name="destination") s_prop_one = Property(name="prop_one", unit="Hz", parent=source) s_prop_two = Property(name="prop_two", parent=source) d_prop_one = Property(name="prop_one", parent=destination) self.assertEqual(len(destination.properties), 1) self.assertIsNone(destination.properties["prop_one"].unit) destination.merge(source) self.assertEqual(len(destination.properties), 2) self.assertEqual(destination.properties["prop_one"].unit, s_prop_one.unit) self.assertEqual(destination.properties["prop_two"], s_prop_two) # -- First child level Property merge tests source = Section(name="source") destination = Section(name="destination") s_sec_one = Section(name="lvl", type="one", definition="def", parent=source) s_prop_one = Property(name="prop_one", unit="Hz", parent=s_sec_one) s_prop_two = Property(name="prop_two", parent=s_sec_one) d_sec_one = Section(name="lvl", type="one", parent=destination) d_prop_one = Property(name="prop_one", parent=d_sec_one) self.assertEqual(len(destination.properties), 0) self.assertEqual(len(destination.sections["lvl"].properties), 1) self.assertIsNone(destination.sections["lvl"].properties["prop_one"].unit) destination.merge(source) self.assertEqual(len(destination.properties), 0) self.assertEqual(len(destination.sections["lvl"].properties), 2) self.assertEqual(destination.sections["lvl"].properties["prop_one"].unit, s_prop_one.unit) self.assertEqual(destination.sections["lvl"].properties["prop_two"], s_prop_two) # -- Test nothing merged on second child level ValueError source = Section(name="source", definition="def", reference="ref") destination = Section(name="destination") s_sec_one = Section(name="lvl", type="one", definition="def", parent=source) s_sec_two = Section(name="other", type="one", parent=source) d_sec_one = Section(name="lvl", type="one", parent=destination) s_subprop_one = Property(name="prop", value=[1, 2, 3], parent=s_sec_one) d_subprop_one = Property(name="prop", value=["four", "five"], parent=d_sec_one) self.assertEqual(len(destination.sections), 1) self.assertEqual(len(destination.sections["lvl"].properties), 1) self.assertIsNone(destination.definition) self.assertIsNone(destination.sections["lvl"].definition) self.assertEqual(destination.sections["lvl"].properties[0].values, d_subprop_one.values) with self.assertRaises(ValueError): destination.merge(source) self.assertEqual(len(destination.sections), 1) self.assertEqual(len(destination.sections["lvl"].properties), 1) self.assertIsNone(destination.definition) self.assertIsNone(destination.sections["lvl"].definition) self.assertEqual(destination.sections["lvl"].properties[0].values, d_subprop_one.values)
def test_merge_check(self): # -- Root level Section checks # Test empty Section check source = Section(name="source") destination = Section(name="destination") destination.merge_check(source, True) # Test definition check source = Section(name="source", definition="def") destination = Section(name="destination", definition="def") destination.merge_check(source, True) source.definition = "other def" with self.assertRaises(ValueError): destination.merge_check(source, True) # Test reference check source = Section(name="source", reference="ref") destination = Section(name="destination", reference="ref") destination.merge_check(source, True) source.reference = "other ref" with self.assertRaises(ValueError): destination.merge_check(source, True) # -- First child level Section checks source = Section(name="source") destination = Section(name="destination") s_sec_one = Section(name="lvl", type="one", reference="ref", definition="def", parent=source) s_sec_two = Section(name="unrelated", type="one", reference="one", definition="one", parent=source) d_sec_one = Section(name="lvl", type="one", reference="ref", definition="def", parent=destination) d_sec_two = Section(name="unrelated", type="two", reference="two", definition="two", parent=destination) # Test Section child level definition check destination.merge_check(source, True) s_sec_one.definition = "other def" with self.assertRaises(ValueError): destination.merge_check(source, True) # Test Section child level reference check s_sec_one.definition = "def" s_sec_one.reference = "other ref" with self.assertRaises(ValueError): destination.merge_check(source, True) # -- Second child level Section checks source = Section(name="source") destination = Section(name="destination") s_sec_one = Section(name="lvl", type="one", reference="ref", definition="def", parent=source) s_subsec_one = Section(name="lvl", type="two", reference="ref2", definition="def2", parent=s_sec_one) s_sec_two = Section(name="unrelated", type="one", reference="one", definition="one", parent=source) s_subsec_two = Section(name="lvl", type="two", reference="none1", definition="none1", parent=s_sec_two) d_sec_one = Section(name="lvl", type="one", reference="ref", definition="def", parent=destination) d_subsec_one = Section(name="lvl", type="two", reference="ref2", definition="def2", parent=d_sec_one) d_sec_two = Section(name="unrelated", type="two", reference="two", definition="two", parent=destination) d_subsec_two = Section(name="lvl", type="two", reference="none2", definition="none2", parent=d_sec_two) # Test Section 2nd child level definition check # Check no definition/reference ValueError between s_subsec_two and d_subsec_one # since their parents will not be merged. destination.merge_check(source, True) # Raise a definition ValueError between s_subsec_one and d_subsec_one # since their parents will be merged. s_subsec_one.definition = "other def" with self.assertRaises(ValueError): destination.merge_check(source, True) # Test Section 2nd child level reference check s_subsec_one.definition = "def2" # Raise a reference ValueError between s_subsec_one and d_subsec_one # since their parents will be merged. s_subsec_one.reference = "other ref" with self.assertRaises(ValueError): destination.merge_check(source, True) # -- Root level Property checks # All Property checks will only test unit failure in the Section merge context. # Other failures are covered by the specific Property merge check tests. source = Section(name="source") destination = Section(name="destination") s_prop = Property(name="prop", parent=source) d_prop = Property(name="prop", parent=destination) destination.merge_check(source, True) s_prop.unit = "Hz" d_prop.unit = "s" with self.assertRaises(ValueError): destination.merge_check(source, True) # -- First child level Property checks source = Section(name="source") destination = Section(name="destination") s_prop_one = Property(name="lvl one", unit="Hz", parent=source) s_prop_two = Property(name="unrelated one", unit="one", parent=source) d_prop_one = Property(name="lvl one", unit="Hz", parent=destination) d_prop_two = Property(name="unrelated two", unit="two", parent=destination) # Test Property child level check destination.merge_check(source, True) # Test raise ValueError between s_prop_one and d_prop_one s_prop_one.unit = "other unit" with self.assertRaises(ValueError): destination.merge_check(source, True) # -- Second child level Property checks source = Section(name="source") destination = Section(name="destination") s_sec_one = Section(name="lvl", type="one", parent=source) s_subprop_one = Property(name="lvl one", unit="Hz", parent=s_sec_one) s_sec_two = Section(name="unrelated", type="one", parent=source) s_subprop_two = Property(name="unrelated one", unit="one", parent=s_sec_two) d_sec_one = Section(name="lvl", type="one", parent=destination) d_subprop_one = Property(name="lvl one", unit="Hz", parent=d_sec_one) d_sec_two = Section(name="unrelated", type="two", parent=destination) d_subprop_two = Property(name="unrelated one", unit="two", parent=d_sec_two) # Test Property 2nd child level definition check # Check no unit ValueError between s_subprop_two and d_subprop_one # since their parents will not be merged. destination.merge_check(source, True) # Raise a unit ValueError between s_subprop_one and d_subprop_one # since their parents will be merged. s_subprop_one.unit = "other unit" with self.assertRaises(ValueError): destination.merge_check(source, True)
def test_contains(self): sec = Section(name="root") subsec = Section(name="subsec", type="test") prop = Property(name="prop") # Test not contains on empty child-lists. self.assertIsNone(sec.contains(subsec)) self.assertIsNone(sec.contains(prop)) # Test contains of Section and Property subsec.parent = sec simisec = Section(name="subsec", type="test") self.assertEqual(sec.contains(simisec).name, subsec.name) prop.parent = sec simiprop = Property(name="prop") self.assertEqual(sec.contains(simiprop).name, prop.name) # Test not contains on mismatching Section name/type and Property name self.assertIsNone(sec.contains(Section(name="subsec", type="typetest"))) self.assertIsNone(sec.contains(Section(name="typesec", type="test"))) self.assertIsNone(sec.contains(Property(name="prop_two"))) # Test fail on non-Section/Property objects with self.assertRaises(ValueError): sec.contains(Document()) with self.assertRaises(ValueError): sec.contains("some info")
def setUp(self): """ doc -- <section foo> -- <section subfoo> \ -- <section bar> -- <section subbar> """ doc = Document("author") foo = Section("foo", "footype") doc.append(foo) foo.append(Property("strprop", "somestring")) foo.append(Property("txtprop", "some\ntext")) subfoo = Section("subfoo", "footype") foo.append(subfoo) subfoo.append(Property("strprop", "somestring")) subfoo.append(Property("txtprop", "some\ntext")) bar = Section("bar", "bartype") foo.append(bar) bar.append(Property("strprop", "otherstring")) bar.append(Property("txtprop", "other\ntext")) subbar = Section("subbar", "bartype") bar.append(subbar) subbar.append(Property("strprop", "otherstring")) subbar.append(Property("txtprop", "other\ntext")) self.doc = doc
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = unicode(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] v = new_sec.properties["strprop"].value assert(v.dtype == "string") assert(type(v.data) == unicode) v = new_sec.properties["txtprop"].value assert(v.dtype == "text") assert(type(v.data) == unicode) v = new_sec.properties["intprop"].value assert(v.dtype == "int") assert(type(v.data) == int) v = new_sec.properties["floatprop"].value assert(v.dtype == "float") assert(type(v.data) == float) v = new_sec.properties["datetimeprop"].value assert(v.dtype == "datetime") assert(type(v.data) == dt) v = new_sec.properties["dateprop"].value assert(v.dtype == "date") assert(type(v.data) == date) v = new_sec.properties["timeprop"].value assert(v.dtype == "time") assert(type(v.data) == time) v = new_sec.properties["boolprop"].value assert(v.dtype == "boolean") assert(type(v.data) == bool)
def test_children(self): sec = Section(name="sec") # Test set sections subsec = Section(name="subsec", parent=sec) newsec = Section(name="newsec") self.assertEqual(subsec.parent, sec) self.assertEqual(sec.sections[0], subsec) self.assertEqual(len(sec.sections), 1) self.assertIsNone(newsec.parent) sec.sections[0] = newsec self.assertEqual(newsec.parent, sec) self.assertEqual(sec.sections[0], newsec) self.assertEqual(len(sec.sections), 1) self.assertIsNone(subsec.parent) # Test parent cleanup root = Section(name="root") sec.parent = root subsec.parent = newsec self.assertEqual(len(newsec.sections), 1) self.assertEqual(newsec.sections[0], subsec) self.assertEqual(subsec.parent, newsec) self.assertEqual(len(root.sections), 1) self.assertEqual(root.sections[0], sec) subsec.parent = root self.assertEqual(len(newsec.sections), 0) self.assertEqual(subsec.parent, root) self.assertEqual(len(root.sections), 2) self.assertEqual(root.sections[1], subsec) # Test set section fails with self.assertRaises(ValueError): sec.sections[0] = Document() with self.assertRaises(ValueError): sec.sections[0] = Property("fail") with self.assertRaises(ValueError): sec.sections[0] = "subsec" # Test set properties prop = Property(name="prop", parent=sec) newprop = Property(name="newprop") self.assertEqual(prop.parent, sec) self.assertEqual(sec.properties[0], prop) self.assertEqual(len(sec.properties), 1) self.assertIsNone(newprop.parent) sec.properties[0] = newprop self.assertEqual(newprop.parent, sec) self.assertEqual(sec.properties[0], newprop) self.assertEqual(len(sec.properties), 1) self.assertIsNone(prop.parent) # Test set property fails with self.assertRaises(ValueError): sec.properties[0] = Document() with self.assertRaises(ValueError): sec.properties[0] = newsec with self.assertRaises(ValueError): sec.properties[0] = "prop" # same tests with props alias prop = Property(name="prop2", parent=sec) newprop = Property(name="newprop2") self.assertEqual(prop.parent, sec) self.assertEqual(sec.props[1], prop) self.assertEqual(len(sec.props), 2) self.assertIsNone(newprop.parent) sec.props[1] = newprop self.assertEqual(newprop.parent, sec) self.assertEqual(sec.props[1], newprop) self.assertEqual(len(sec.props), 2) self.assertIsNone(prop.parent) # Test set property fails with self.assertRaises(ValueError): sec.props[1] = Document() with self.assertRaises(ValueError): sec.props[1] = newsec with self.assertRaises(ValueError): sec.props[1] = "prop2"
def test_extend(self): sec = Section(name="main") self.assertListEqual(sec.sections, []) self.assertListEqual(sec.properties, []) # Test extend with Section list sec.extend([Section(name="sec1"), Section(name="sec2")]) self.assertEqual(len(sec), 2) self.assertEqual(len(sec.sections), 2) self.assertEqual(sec.sections[0].name, "sec1") # Test extend with Property list sec.extend((Property(name="prop1"), Property(name="prop2"))) self.assertEqual(len(sec), 4) self.assertEqual(len(sec.properties), 2) self.assertEqual(sec.properties[0].name, "prop1") # Test extend with mixed list sec.extend([Section(name="sec3"), Property(name="prop3")]) self.assertEqual(len(sec), 6) self.assertEqual(len(sec.sections), 3) self.assertEqual(len(sec.properties), 3) # Test fail on non iterable with self.assertRaises(TypeError): sec.extend(1) # Test fail on non Section/Property list entry with self.assertRaises(ValueError): sec.extend([Property(name="prop4"), 5]) # Test fail on same name entities with self.assertRaises(KeyError): sec.extend([Property(name="new"), Property(name="prop3")]) self.assertEqual(len(sec.properties), 3) with self.assertRaises(KeyError): sec.extend([Section(name="new"), Section(name="sec3")]) self.assertEqual(len(sec.sections), 3)
def test_append(self): main = Section(name="main") self.assertListEqual(main.sections, []) self.assertListEqual(main.properties, []) # Test append Section sec = Section(name="sec1") main.append(sec) self.assertEqual(len(main.sections), 1) self.assertEqual(sec.parent, main) # Test fail on Section list or tuple append with self.assertRaises(ValueError): main.append([Section(name="sec2"), Section(name="sec3")]) with self.assertRaises(ValueError): main.append((Section(name="sec2"), Section(name="sec3"))) self.assertEqual(len(main.sections), 1) # Test append Property prop = Property(name="prop") main.append(prop) self.assertEqual(len(main.properties), 1) self.assertEqual(prop.parent, main) # Test fail on Property list or tuple append with self.assertRaises(ValueError): main.append([Property(name="prop2"), Property(name="prop3")]) with self.assertRaises(ValueError): main.append((Property(name="prop2"), Property(name="prop3"))) self.assertEqual(len(main.properties), 1) # Test fail on unsupported value with self.assertRaises(ValueError): main.append(Document()) with self.assertRaises(ValueError): main.append("Section") # Test fail on same name entities with self.assertRaises(KeyError): main.append(Section(name="sec1")) self.assertEqual(len(main.sections), 1) with self.assertRaises(KeyError): main.append(Property(name="prop")) self.assertEqual(len(main.properties), 1)
def test_remove(self): sec = Section(name="remsec") ssec_one = Section(name="subsec_one", parent=sec) ssec_two = Section(name="subsec_two", parent=sec) self.assertEqual(len(sec.sections), 2) self.assertIsNotNone(ssec_one.parent) sec.remove(ssec_one) self.assertEqual(len(sec.sections), 1) self.assertEqual(sec.sections[0].name, ssec_two.name) self.assertIsNone(ssec_one.parent) with self.assertRaises(ValueError): sec.remove(ssec_one) self.assertEqual(len(sec.sections), 1) prop_one = Property(name="prop_one", parent=sec) prop_two = Property(name="prop_two", parent=sec) self.assertEqual(len(sec.properties), 2) self.assertIsNotNone(prop_one.parent) sec.remove(prop_one) self.assertEqual(len(sec.properties), 1) self.assertEqual(sec.properties[0].name, prop_two.name) self.assertIsNone(prop_one.parent) with self.assertRaises(ValueError): sec.remove(prop_one) self.assertEqual(len(sec.properties), 1) with self.assertRaises(ValueError): sec.remove("prop_two")
def test_insert(self): sec = Section(name="root") _ = Section(name="subsec_one", parent=sec) _ = Section(name="subsec_two", parent=sec) subsec = Section(name="subsec_three") self.assertNotEqual(sec.sections[1].name, subsec.name) sec.insert(1, subsec) self.assertEqual(len(sec.sections), 3) self.assertEqual(sec.sections[1].name, subsec.name) self.assertEqual(subsec.parent.name, sec.name) _ = Property(name="prop_one", parent=sec) _ = Property(name="prop_two", parent=sec) prop = Property(name="prop_three") self.assertNotEqual(sec.properties[1].name, prop.name) sec.insert(1, prop) self.assertEqual(len(sec.properties), 3) self.assertEqual(sec.properties[1].name, prop.name) self.assertEqual(prop.parent.name, sec.name) # Test invalid object with self.assertRaises(ValueError): sec.insert(1, Document()) with self.assertRaises(ValueError): sec.insert(1, "some info") self.assertEqual(len(sec), 6) # Test same name entries with self.assertRaises(ValueError): sec.insert(0, subsec) with self.assertRaises(ValueError): sec.insert(0, prop) self.assertEqual(len(sec), 6)
def test_comparison(self): doc_auth = "author" doc_ver = "ver1.0" doc_a = Document(author=doc_auth, version=doc_ver) doc_b = Document(author=doc_auth, version=doc_ver) self.assertEqual(doc_a, doc_b) doc_b.author = "someone else" self.assertNotEqual(doc_a, doc_b) doc_b.author = doc_auth # Test section equality with subsections # Test equality with subsection of different entities # with same content and same children order sec_type = "sec type" sec_def = "an odml test section" sec_ref = "from over there" subsec_aa = Section(name="subsecA", type=sec_type, definition=sec_def, reference=sec_ref) subsec_ab = Section(name="subsecB", type=sec_type, definition=sec_def, reference=sec_ref) subsec_ba = Section(name="subsecA", type=sec_type, definition=sec_def, reference=sec_ref) subsec_bb = Section(name="subsecB", type=sec_type, definition=sec_def, reference=sec_ref) doc_a.extend([subsec_aa, subsec_ab]) doc_b.extend([subsec_ba, subsec_bb]) self.assertEqual(doc_a, doc_b) self.assertEqual(doc_a.sections, doc_b.sections) doc_b.sections[0].name = "newSubsecA" self.assertNotEqual(doc_a, doc_b) self.assertNotEqual(doc_a.sections, doc_b.sections) doc_b.sections[0].name = "subsecA" # Test inequality with different number of children doc_b.remove(doc_b.sections[1]) self.assertNotEqual(doc_a, doc_b) self.assertNotEqual(doc_a.sections, doc_b.sections) # Test equality with subsection of different entities # with same content and different children order doc_b.remove(doc_b.sections[0]) doc_b.extend([subsec_bb, subsec_ba]) self.assertEqual(doc_a, doc_b) self.assertEqual(doc_a.sections, doc_b.sections) doc_b.sections[0].name = "newSubsecB" self.assertNotEqual(doc_a, doc_b) self.assertNotEqual(doc_a.sections, doc_b.sections) doc_b.sections[0].name = "subsecB" # Test section equality with properties # Test equality with properties of different entities # with same content and same children order prop_aa = Property(name="propA", definition="propDef") prop_ab = Property(name="propB", definition="propDef") prop_ba = Property(name="propA", definition="propDef") prop_bb = Property(name="propB", definition="propDef") doc_a.sections["subsecA"].extend([prop_aa, prop_ab]) doc_b.sections["subsecA"].extend([prop_ba, prop_bb]) self.assertEqual(doc_a, doc_b) doc_b.sections["subsecA"].properties[0].name = "newPropA" self.assertNotEqual(doc_a, doc_b) doc_b.sections["subsecA"].properties[0].name = "propA" # Test inequality with different number of children doc_b.sections["subsecA"].remove( doc_b.sections["subsecA"].properties[1]) self.assertNotEqual(doc_a, doc_b) # Test equality with properties of different entities # with same content and different children order doc_b.sections["subsecA"].remove( doc_b.sections["subsecA"].properties[0]) doc_b.sections["subsecA"].extend([prop_bb, prop_ba]) self.assertEqual(doc_a, doc_b) doc_b.sections["subsecA"].properties[0].name = "newPropB" self.assertNotEqual(doc_a, doc_b)