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 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 setUp(self): """ doc -- <section sec_main> -- <section sub_main> \ -- <section sec_branch> -- <section sub_branch> """ doc = Document("author") sec_main = Section("sec_main", "maintype") doc.append(sec_main) sec_main.append(Property("strprop", "somestring")) sec_main.append(Property("txtprop", "some\ntext")) sub_main = Section("sub_main", "maintype") sec_main.append(sub_main) sub_main.append(Property("strprop", "somestring")) sub_main.append(Property("txtprop", "some\ntext")) sec_branch = Section("sec_branch", "branchtype") sec_main.append(sec_branch) sec_branch.append(Property("strprop", "otherstring")) sec_branch.append(Property("txtprop", "other\ntext")) sub_branch = Section("sub_branch", "branchtype") sec_branch.append(sub_branch) sub_branch.append(Property("strprop", "otherstring")) sub_branch.append(Property("txtprop", "other\ntext")) self.doc = doc
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 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 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_create_section(self): root = Document() self.assertEqual(len(root.sections), 0) name = "subsec" sec_type = "subtype" oid = "79b613eb-a256-46bf-84f6-207df465b8f7" subsec = root.create_section(name, sec_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, sec_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, "n.s.") 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_export_leaf(self): doc = Document() first = doc.create_section("first") second = first.create_section("second") third = first.create_section("third") name = "prop1" values = [1.3] first.create_property(name, value=values) name = "prop2" values = ["words"] first.create_property(name, value=values) name = "prop3" values = ["a", "b"] second.create_property(name, value=values) ex1 = first.export_leaf() self.assertEqual(len(ex1.sections), 1) self.assertEqual(len(ex1['first'].properties), 2) self.assertEqual(len(ex1['first'].sections), 0) ex2 = second.export_leaf() self.assertEqual(len(ex2.sections), 1) self.assertEqual(len(ex2['first'].properties), 2) self.assertEqual(len(ex2['first'].sections), 1) self.assertEqual(len(ex2['first']['second'].properties), 1) ex3 = third.export_leaf() self.assertEqual(len(ex3.sections), 1) self.assertEqual(len(ex3['first'].properties), 2) self.assertEqual(len(ex3['first'].sections), 1) self.assertEqual(len(ex3['first']['third']), 0)
def test_create_section(self): root = Document() 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_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)) if sys.version_info < (3, 0): str_doc = unicode(XMLWriter(doc)) else: str_doc = str(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] p = new_sec.properties["strprop"] assert (p.dtype == "string") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["txtprop"] assert (p.dtype == "text") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["intprop"] assert (p.dtype == "int") assert (type(p.value[0]) == int) p = new_sec.properties["floatprop"] assert (p.dtype == "float") assert (type(p.value[0]) == float) p = new_sec.properties["datetimeprop"] assert (p.dtype == "datetime") assert (type(p.value[0]) == dt) p = new_sec.properties["dateprop"] assert (p.dtype == "date") assert (type(p.value[0]) == date) p = new_sec.properties["timeprop"] assert (p.dtype == "time") assert (type(p.value[0]) == time) p = new_sec.properties["boolprop"] assert (p.dtype == "boolean") assert (type(p.value[0]) == bool)
def test_date(self): datestring = "2000-01-02" doc = Document(date=datestring) self.assertIsInstance(doc.date, datetime.date) self.assertEqual( doc.date, datetime.datetime.strptime(datestring, FORMAT_DATE).date()) doc.date = None self.assertIsNone(doc.date) doc.date = datestring self.assertIsInstance(doc.date, datetime.date) self.assertEqual( doc.date, datetime.datetime.strptime(datestring, FORMAT_DATE).date()) doc.date = [] self.assertIsNone(doc.date) doc.date = {} self.assertIsNone(doc.date) doc.date = () self.assertIsNone(doc.date) doc.date = "" self.assertIsNone(doc.date) with self.assertRaises(ValueError): doc.date = "some format"
def test_get_terminology_equivalent(self): repo_file = os.path.join(RES_DIR, self.local_repo_file) local_url = "file://%s" % pathname2url(repo_file) doc = Document(repository=local_url) teq = doc.get_terminology_equivalent() self.assertIsInstance(teq, BaseDocument) self.assertEqual(len(teq), 1) self.assertEqual(teq.sections[0].name, "Repository test") doc.repository = None self.assertIsNone(doc.get_terminology_equivalent())
def test_id(self): doc = Document() self.assertIsNotNone(doc.id) doc = Document("D", oid="79b613eb-a256-46bf-84f6-207df465b8f7") self.assertEqual(doc.id, "79b613eb-a256-46bf-84f6-207df465b8f7") doc = Document("D", oid="id") self.assertNotEqual(doc.id, "id") # Make sure id cannot be reset programmatically. with self.assertRaises(AttributeError): doc.id = "someId"
def test_date(self): datestring = "2000-01-02" doc = Document(date=datestring) self.assertIsInstance(doc.date, datetime.date) self.assertEqual(doc.date, datetime.datetime.strptime(datestring, FORMAT_DATE).date()) doc.date = None self.assertIsNone(doc.date) doc.date = datestring self.assertIsInstance(doc.date, datetime.date) self.assertEqual(doc.date, datetime.datetime.strptime(datestring, FORMAT_DATE).date()) doc.date = [] self.assertIsNone(doc.date) doc.date = {} self.assertIsNone(doc.date) doc.date = () self.assertIsNone(doc.date) doc.date = "" self.assertIsNone(doc.date) with self.assertRaises(ValueError): doc.date = "some format"
def test_doc(self): """ Test if a document and its attributes get converted correctly from rdf to odml. """ doc = Document() doc.author = "D. N. Adams" doc.version = 42 doc.date = datetime.date(1979, 10, 12) rdf_writer = RDFWriter(doc).get_rdf_str() rdf_reader = RDFReader().from_string(rdf_writer, "turtle") self.assertEqual(rdf_reader[0].author, "D. N. Adams") self.assertEqual(rdf_reader[0].version, "42") self.assertEqual(rdf_reader[0].date, datetime.date(1979, 10, 12))
def test_get_terminology_equivalent(self): dir_path = os.path.dirname(os.path.realpath(__file__)) repo_file = os.path.join(dir_path, "resources", "local_repository_file_v1.1.xml") local_url = "file://%s" % pathname2url(repo_file) doc = Document(repository=local_url) teq = doc.get_terminology_equivalent() self.assertIsInstance(teq, BaseDocument) self.assertEqual(len(teq), 1) self.assertEqual(teq.sections[0].name, "Repository test") doc.repository = None self.assertIsNone(doc.get_terminology_equivalent())
def test_get_terminology_equivalent(self): dir_path = os.path.dirname(os.path.realpath(__file__)) repo_file = os.path.join(dir_path, "resources", "local_repository_file_v1.1.xml") local_url = "file://%s" % pathname2url(repo_file) doc = Document(repository=local_url) teq = doc.get_terminology_equivalent() self.assertIsInstance(teq, BaseDocument) self.assertEqual(len(teq), 1) self.assertEqual(teq.sections[0].name, "Repository test") doc.repository = None self.assertIsNone(doc.get_terminology_equivalent())
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_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 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_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 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_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 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 test_simple_attributes(self): author = "HPL" version = "4.8.15" doc = Document(author=author, version=version) self.assertEqual(doc.author, author) self.assertEqual(doc.version, version) doc.author = "" doc.version = "" self.assertIsNone(doc.author) self.assertIsNone(doc.version) doc.author = author doc.version = version self.assertEqual(doc.author, author) self.assertEqual(doc.version, version)
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 test_read_write(self): doc = Document("author") sec = Section("name", "type", parent=doc) 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 = str(XMLWriter(doc)) new_doc = XMLReader().from_string(str_doc) new_sec = new_doc.sections[0] prop = new_sec.properties["strprop"] self.assertEqual(prop.dtype, "string") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["txtprop"] self.assertEqual(prop.dtype, "text") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["intprop"] self.assertEqual(prop.dtype, "int") self.assertIsInstance(prop.values[0], int) prop = new_sec.properties["floatprop"] self.assertEqual(prop.dtype, "float") self.assertIsInstance(prop.values[0], float) prop = new_sec.properties["datetimeprop"] self.assertEqual(prop.dtype, "datetime") self.assertIsInstance(prop.values[0], dt) prop = new_sec.properties["dateprop"] self.assertEqual(prop.dtype, "date") self.assertIsInstance(prop.values[0], date) prop = new_sec.properties["timeprop"] self.assertEqual(prop.dtype, "time") self.assertIsInstance(prop.values[0], time) prop = new_sec.properties["boolprop"] self.assertEqual(prop.dtype, "boolean") self.assertIsInstance(prop.values[0], bool)
def test_id(self): doc = Document() self.assertIsNotNone(doc.id) doc = Document("D", oid="79b613eb-a256-46bf-84f6-207df465b8f7") self.assertEqual(doc.id, "79b613eb-a256-46bf-84f6-207df465b8f7") doc = Document("D", oid="id") self.assertNotEqual(doc.id, "id") # Make sure id cannot be reset programmatically. with self.assertRaises(AttributeError): doc.id = "someId"
def test_new_id(self): doc = Document() old_id = doc.id # Test assign new generated id. doc.new_id() self.assertNotEqual(old_id, doc.id) # Test assign new custom id. old_id = doc.id doc.new_id("79b613eb-a256-46bf-84f6-207df465b8f7") self.assertNotEqual(old_id, doc.id) self.assertEqual("79b613eb-a256-46bf-84f6-207df465b8f7", doc.id) # Test invalid custom id exception. with self.assertRaises(ValueError): doc.new_id("crash and burn")
def test_export_leaf(self): doc = Document() sec_a_name = "first" sec_b_name = "second" first = doc.create_section(sec_a_name) second = first.create_section(sec_b_name) _ = first.create_section("third") prop_aa = first.create_property("prop1", value=[1.3]) _ = first.create_property("prop5", value=["abc"]) prop_ba = second.create_property("prop2", value=["words"]) _ = second.create_property("prop3", value=["a", "b"]) _ = second.create_property("prop4", value=[3]) export_doc = prop_aa.export_leaf() self.assertEqual(len(export_doc[sec_a_name].properties), 2) self.assertEqual(len(export_doc[sec_a_name].sections), 0) export_doc = prop_ba.export_leaf() self.assertEqual(len(export_doc.sections), 1) self.assertEqual(len(export_doc[sec_a_name].properties), 2) self.assertEqual(len(export_doc[sec_a_name].sections), 1) self.assertEqual(len(export_doc[sec_a_name][sec_b_name].properties), 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_new_id(self): doc = Document() old_id = doc.id # Test assign new generated id. doc.new_id() self.assertNotEqual(old_id, doc.id) # Test assign new custom id. old_id = doc.id doc.new_id("79b613eb-a256-46bf-84f6-207df465b8f7") self.assertNotEqual(old_id, doc.id) self.assertEqual("79b613eb-a256-46bf-84f6-207df465b8f7", doc.id) # Test invalid custom id exception. with self.assertRaises(ValueError): doc.new_id("crash and burn")
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_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_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_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 test_simple_attributes(self): author = "HPL" version = "4.8.15" doc = Document(author=author, version=version) self.assertEqual(doc.author, author) self.assertEqual(doc.version, version) doc.author = "" doc.version = "" self.assertIsNone(doc.author) self.assertIsNone(doc.version) doc.author = author doc.version = version self.assertEqual(doc.author, author) self.assertEqual(doc.version, version)
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_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)
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_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_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)