|
| 1 | +import datetime |
| 2 | +import unittest |
| 3 | + |
| 4 | +from rdflib import Literal |
| 5 | +import odml.format as format |
| 6 | + |
| 7 | +from odml import Property, Section, Document |
| 8 | +from odml.tools.rdf_converter import RDFWriter, RDFReader |
| 9 | +from odml.tools.parser_utils import ParserException |
| 10 | + |
| 11 | +odmlns = format.Format.namespace() |
| 12 | + |
| 13 | + |
| 14 | +class TestRDFReader(unittest.TestCase): |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + doc = Document() |
| 18 | + sec = Section(name="sec1", type="test", parent=doc) |
| 19 | + Section(name="sec2", type="test", parent=sec) |
| 20 | + Property(name="prop1", values=[1.3], parent=sec) |
| 21 | + |
| 22 | + self.doc = doc |
| 23 | + |
| 24 | + def test_rdf_formats(self): |
| 25 | + """ |
| 26 | + Test if document gets correctly converted to odml for turtle, xml and n3. |
| 27 | + """ |
| 28 | + w = RDFWriter(self.doc).get_rdf_str() |
| 29 | + r = RDFReader().from_string(w, "turtle") |
| 30 | + self.assertEqual(len(r[0].sections), 1) |
| 31 | + self.assertEqual(len(r[0].sections[0].sections), 1) |
| 32 | + self.assertEqual(len(r[0].sections[0].properties), 1) |
| 33 | + |
| 34 | + w = RDFWriter(self.doc).get_rdf_str("xml") |
| 35 | + r = RDFReader().from_string(w, "xml") |
| 36 | + self.assertEqual(len(r[0].sections), 1) |
| 37 | + self.assertEqual(len(r[0].sections[0].sections), 1) |
| 38 | + self.assertEqual(len(r[0].sections[0].properties), 1) |
| 39 | + |
| 40 | + w = RDFWriter(self.doc).get_rdf_str("n3") |
| 41 | + r = RDFReader().from_string(w, "n3") |
| 42 | + self.assertEqual(len(r[0].sections), 1) |
| 43 | + self.assertEqual(len(r[0].sections[0].sections), 1) |
| 44 | + self.assertEqual(len(r[0].sections[0].properties), 1) |
| 45 | + |
| 46 | + def test_doc(self): |
| 47 | + """ |
| 48 | + Test if a document and its attributes get converted correctly from rdf to odml. |
| 49 | + """ |
| 50 | + doc = Document() |
| 51 | + doc.author = "D. N. Adams" |
| 52 | + doc.version = 42 |
| 53 | + doc.date = datetime.date(1979, 10, 12) |
| 54 | + |
| 55 | + w = RDFWriter(doc).get_rdf_str() |
| 56 | + r = RDFReader().from_string(w, "turtle") |
| 57 | + |
| 58 | + self.assertEqual(r[0].author, "D. N. Adams") |
| 59 | + self.assertEqual(r[0].version, "42") |
| 60 | + self.assertEqual(r[0].date, datetime.date(1979, 10, 12)) |
| 61 | + |
| 62 | + def test_section(self): |
| 63 | + """ |
| 64 | + Test if a section and its attributes get converted correctly from rdf to odml. |
| 65 | + """ |
| 66 | + doc = Document() |
| 67 | + sec1 = Section(name="sec1", type="test", parent=doc, definition="Interesting stuff.", |
| 68 | + reference="The Journal") |
| 69 | + Section(name="sec2", type="test", parent=sec1) |
| 70 | + |
| 71 | + w = RDFWriter(doc).get_rdf_str() |
| 72 | + r = RDFReader().from_string(w, "turtle") |
| 73 | + |
| 74 | + self.assertEqual(r[0].sections[0].name, "sec1") |
| 75 | + self.assertEqual(r[0].sections[0].type, "test") |
| 76 | + self.assertEqual(r[0].sections[0].id, sec1.id) |
| 77 | + self.assertEqual(r[0].sections[0].definition, "Interesting stuff.") |
| 78 | + self.assertEqual(r[0].sections[0].reference, "The Journal") |
| 79 | + self.assertEqual(r[0].sections[0].parent, r[0]) |
| 80 | + self.assertEqual(len(r[0].sections[0].sections), 1) |
| 81 | + |
| 82 | + def test_property(self): |
| 83 | + """ |
| 84 | + Test if a property and its attributes get converted correctly from rdf to odml. |
| 85 | + """ |
| 86 | + doc = Document() |
| 87 | + sec1 = Section(name="sec1", type="test", parent=doc) |
| 88 | + prop2 = Property(name="numbers", definition="any number", dtype="float", parent=sec1, |
| 89 | + values=[1, 3.4, 67.8, -12], unit="meter", uncertainty=0.8, |
| 90 | + value_origin="force", reference="Experiment 1") |
| 91 | + |
| 92 | + w = RDFWriter(doc).get_rdf_str() |
| 93 | + r = RDFReader().from_string(w, "turtle") |
| 94 | + |
| 95 | + prop = r[0].sections[0].properties["numbers"] |
| 96 | + |
| 97 | + self.assertEqual(prop.name, "numbers") |
| 98 | + self.assertEqual(prop.dtype, "float") |
| 99 | + self.assertEqual(prop.id, prop2.id) |
| 100 | + self.assertEqual(prop.parent, r[0].sections[0]) |
| 101 | + self.assertEqual(len(prop.values), 4) |
| 102 | + self.assertEqual(prop.values, [1, 3.4, 67.8, -12]) |
| 103 | + self.assertEqual(prop.definition, "any number") |
| 104 | + self.assertEqual(prop.unit, "meter") |
| 105 | + self.assertEqual(prop.uncertainty, "0.8") |
| 106 | + self.assertEqual(prop.value_origin, "force") |
| 107 | + self.assertEqual(prop.reference, "Experiment 1") |
| 108 | + |
| 109 | + def test_mandatory_attrs_section(self): |
| 110 | + """ |
| 111 | + Test if ParserError is thrown if mandatory attributes are missing for section. |
| 112 | + """ |
| 113 | + w = RDFWriter([self.doc]) |
| 114 | + w.convert_to_rdf() |
| 115 | + for rdf_sec in w.graph.subjects(predicate=odmlns.hasName, object=Literal("sec1")): |
| 116 | + w.graph.remove((rdf_sec, odmlns.hasName, Literal("sec1"))) |
| 117 | + |
| 118 | + new_graph = w.graph.serialize(format="turtle").decode("utf-8") |
| 119 | + |
| 120 | + with self.assertRaises(ParserException): |
| 121 | + RDFReader().from_string(new_graph, "turtle") |
| 122 | + |
| 123 | + def test_mandatory_attrs_property(self): |
| 124 | + """ |
| 125 | + Test if ParserError is thrown if mandatory attributes are missing for section. |
| 126 | + """ |
| 127 | + w = RDFWriter([self.doc]) |
| 128 | + w.convert_to_rdf() |
| 129 | + for rdf_sec in w.graph.subjects(predicate=odmlns.hasName, object=Literal("prop1")): |
| 130 | + w.graph.remove((rdf_sec, odmlns.hasName, Literal("prop1"))) |
| 131 | + |
| 132 | + new_graph = w.graph.serialize(format="turtle").decode("utf-8") |
| 133 | + |
| 134 | + with self.assertRaises(ParserException): |
| 135 | + RDFReader().from_string(new_graph, "turtle") |
0 commit comments