1
1

test_rdf_reader.py 5.5 KB

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