test_parser_odml.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. """
  2. This file tests proper saving and loading of odML Documents
  3. with all supported odML parsers via the tools.odmlparser classes.
  4. """
  5. import os
  6. import shutil
  7. import unittest
  8. from odml import Document, Section, Property
  9. from odml.tools import odmlparser
  10. from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
  11. class TestOdmlParser(unittest.TestCase):
  12. def setUp(self):
  13. # Set up test environment
  14. base_file = os.path.join(RES_DIR, "example.odml")
  15. self.tmp_dir = create_test_dir(__file__)
  16. self.json_file = os.path.join(self.tmp_dir, "test.json")
  17. self.xml_file = os.path.join(self.tmp_dir, "test.xml")
  18. self.yaml_file = os.path.join(self.tmp_dir, "test.yaml")
  19. self.rdf_file = os.path.join(self.tmp_dir, "test.ttl")
  20. self.xml_reader = odmlparser.ODMLReader(parser='XML')
  21. self.yaml_reader = odmlparser.ODMLReader(parser='YAML')
  22. self.json_reader = odmlparser.ODMLReader(parser='JSON')
  23. self.rdf_reader = odmlparser.ODMLReader(parser='RDF')
  24. self.xml_writer = odmlparser.ODMLWriter(parser='XML')
  25. self.yaml_writer = odmlparser.ODMLWriter(parser='YAML')
  26. self.json_writer = odmlparser.ODMLWriter(parser='JSON')
  27. self.rdf_writer = odmlparser.ODMLWriter(parser='RDF')
  28. self.odml_doc = self.xml_reader.from_file(base_file)
  29. def tearDown(self):
  30. if self.tmp_dir and os.path.exists(self.tmp_dir):
  31. shutil.rmtree(self.tmp_dir)
  32. def test_json_yaml_xml(self):
  33. self.json_writer.write_file(self.odml_doc, self.json_file)
  34. json_doc = self.json_reader.from_file(self.json_file)
  35. self.yaml_writer.write_file(json_doc, self.yaml_file)
  36. yaml_doc = self.yaml_reader.from_file(self.yaml_file)
  37. self.xml_writer.write_file(yaml_doc, self.xml_file)
  38. xml_doc = self.xml_reader.from_file(self.xml_file)
  39. self.assertEqual(json_doc, self.odml_doc)
  40. self.assertEqual(json_doc, yaml_doc)
  41. self.assertEqual(json_doc, xml_doc)
  42. self.assertEqual(yaml_doc, self.odml_doc)
  43. self.assertEqual(yaml_doc, xml_doc)
  44. self.assertEqual(yaml_doc, json_doc)
  45. self.assertEqual(xml_doc, self.odml_doc)
  46. self.assertEqual(xml_doc, json_doc)
  47. self.assertEqual(xml_doc, yaml_doc)
  48. def test_xml_file(self):
  49. self.xml_writer.write_file(self.odml_doc, self.xml_file)
  50. xml_doc = self.xml_reader.from_file(self.xml_file)
  51. self.assertEqual(xml_doc, self.odml_doc)
  52. def test_yaml_file(self):
  53. self.yaml_writer.write_file(self.odml_doc, self.yaml_file)
  54. yaml_doc = self.yaml_reader.from_file(self.yaml_file)
  55. self.assertEqual(yaml_doc, self.odml_doc)
  56. def test_json_file(self):
  57. self.json_writer.write_file(self.odml_doc, self.json_file)
  58. json_doc = self.json_reader.from_file(self.json_file)
  59. self.assertEqual(json_doc, self.odml_doc)
  60. def test_rdf_file(self):
  61. """
  62. Test comparison of document before and after rdf-conversion
  63. """
  64. self.rdf_writer.write_file(self.odml_doc, self.rdf_file)
  65. rdf_doc = self.rdf_reader.from_file(self.rdf_file, "xml")
  66. self.assertEqual(self.odml_doc, rdf_doc[0])
  67. # RDF does not preserve the order of sections or properties,
  68. # check the attributes by hand to make sure everything
  69. # was correctly imported.
  70. self.assertEqual(len(rdf_doc), 1)
  71. self.assertEqual(rdf_doc[0].author, self.odml_doc.author)
  72. self.assertEqual(rdf_doc[0].version, self.odml_doc.version)
  73. self.assertEqual(rdf_doc[0].date, self.odml_doc.date)
  74. self.assertIn(self.odml_doc.sections[0].name, rdf_doc[0].sections)
  75. self.assertIn(self.odml_doc.sections[1].name, rdf_doc[0].sections)
  76. # Check error on missing document format
  77. with self.assertRaises(ValueError):
  78. self.rdf_reader.from_file(self.rdf_file)
  79. doc = Document()
  80. sec1 = Section(name="sec1", type="int", parent=doc)
  81. Property(name="prop11", values=[1, 2, 3], dtype="int", parent=sec1)
  82. Property(name="prop12", values=[1.1, 2.2, 3.3], dtype="float", parent=sec1)
  83. Property(name="prop13", values=["a", "b", "c"], dtype="string", parent=sec1)
  84. sec2 = Section(name="sec2", type="int", parent=doc)
  85. Property(name="prop21", values=[1, 2, 3], dtype="int", parent=sec2)
  86. Property(name="prop22", values=[1.1, 2.2, 3.3], dtype="float", parent=sec2)
  87. Property(name="prop23", values=["a", "b", "c"], dtype="string", parent=sec2)
  88. sec3 = Section(name="sec3", type="int", parent=doc)
  89. Property(name="prop31", values=[1, 2, 3], dtype="int", parent=sec3)
  90. Property(name="prop32", values=[1.1, 2.2, 3.3], dtype="float", parent=sec3)
  91. Property(name="prop33", values=["a", "b", "c"], dtype="string", parent=sec3)
  92. self.rdf_writer.write_file(doc, self.rdf_file)
  93. rdf_doc = self.rdf_reader.from_file(self.rdf_file, "xml")
  94. self.assertEqual(doc, rdf_doc[0])
  95. self.assertEqual(len(rdf_doc), 1)
  96. self.assertEqual(len(rdf_doc[0].sections), 3)
  97. self.assertIn(doc.sections[0].name, rdf_doc[0].sections)
  98. self.assertIn(doc.sections[1].name, rdf_doc[0].sections)
  99. self.assertIn(doc.sections[2].name, rdf_doc[0].sections)
  100. rdf_sec1 = rdf_doc[0].sections[doc.sections[0].name]
  101. self.assertEqual(len(rdf_sec1.properties), 3)
  102. self.assertIn(doc.sections[0].properties[0].name, rdf_sec1.properties)
  103. self.assertIn(doc.sections[0].properties[1].name, rdf_sec1.properties)
  104. self.assertIn(doc.sections[0].properties[1].name, rdf_sec1.properties)
  105. rdf_sec2 = rdf_doc[0].sections[doc.sections[1].name]
  106. self.assertEqual(len(rdf_sec2.properties), 3)
  107. self.assertIn(doc.sections[1].properties[0].name, rdf_sec2.properties)
  108. self.assertIn(doc.sections[1].properties[1].name, rdf_sec2.properties)
  109. self.assertIn(doc.sections[1].properties[1].name, rdf_sec2.properties)
  110. rdf_sec3 = rdf_doc[0].sections[doc.sections[2].name]
  111. self.assertEqual(len(rdf_sec3.properties), 3)
  112. self.assertIn(doc.sections[2].properties[0].name, rdf_sec3.properties)
  113. self.assertIn(doc.sections[2].properties[1].name, rdf_sec3.properties)
  114. self.assertIn(doc.sections[2].properties[1].name, rdf_sec3.properties)
  115. def test_xml_string(self):
  116. # Read from string
  117. author = "HPL"
  118. date = "1890-08-20"
  119. sec_name = "section name"
  120. sec_type = "section type"
  121. doc = """
  122. <odML version="1.1">
  123. <author>%s</author>
  124. <date>%s</date>
  125. <section>
  126. <name>%s</name>
  127. <type>%s</type>
  128. </section>
  129. </odML>
  130. """ % (author, date, sec_name, sec_type)
  131. xml_doc = self.xml_reader.from_string(doc)
  132. self.assertEqual(xml_doc.author, author)
  133. self.assertEqual(str(xml_doc.date), date)
  134. self.assertEqual(len(xml_doc.sections), 1)
  135. self.assertEqual(xml_doc.sections[0].name, sec_name)
  136. def test_json_string(self):
  137. author = "HPL"
  138. date = "1890-08-20"
  139. sec_name = "section name"
  140. sec_type = "section type"
  141. doc = """
  142. {
  143. "odml-version": "1.1",
  144. "Document": {
  145. "author": "%s",
  146. "date": "%s",
  147. "sections": [{
  148. "name": "%s",
  149. "type": "%s"
  150. }]
  151. }
  152. }
  153. """ % (author, date, sec_name, sec_type)
  154. json_doc = self.json_reader.from_string(doc)
  155. self.assertEqual(json_doc.author, author)
  156. self.assertEqual(str(json_doc.date), date)
  157. self.assertEqual(len(json_doc.sections), 1)
  158. self.assertEqual(json_doc.sections[0].name, sec_name)
  159. # Test empty return on broken json document
  160. self.assertIsNone(self.json_reader.from_string("{"))
  161. def test_yaml_string(self):
  162. author = "HPL"
  163. date = "1890-08-20"
  164. sec_name = "section name"
  165. sec_type = "section type"
  166. yaml_doc = """
  167. odml-version: '1.1'
  168. Document:
  169. author: %s
  170. date: %s
  171. sections:
  172. - name: %s
  173. type: %s
  174. """ % (author, date, sec_name, sec_type)
  175. ydoc = self.yaml_reader.from_string(yaml_doc)
  176. self.assertEqual(ydoc.author, author)
  177. self.assertEqual(str(ydoc.date), date)
  178. self.assertEqual(len(ydoc.sections), 1)
  179. self.assertEqual(ydoc.sections[0].name, sec_name)
  180. def test_rdf_string(self):
  181. author = "HPL"
  182. date = "1890-08-20"
  183. sec_name = "section name"
  184. sec_type = "section type"
  185. rdf_doc = u"""
  186. @prefix odml: <https://g-node.org/odml-rdf#> .
  187. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
  188. @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
  189. @prefix xml: <http://www.w3.org/XML/1998/namespace> .
  190. @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
  191. odml:Hub odml:hasDocument <https://g-node.org/odml-rdf#1c9ca24a-1d2c-40b6-a096-5a48efbd77d0> .
  192. <https://g-node.org/odml-rdf#1c9ca24a-1d2c-40b6-a096-5a48efbd77d0> a odml:Document ;
  193. odml:hasAuthor "%s" ;
  194. odml:hasDate "%s"^^xsd:date ;
  195. odml:hasSection <https://g-node.org/odml-rdf#2abc6711-34e1-4102-8e3a-297fa4a3d19a> .
  196. <https://g-node.org/odml-rdf#2abc6711-34e1-4102-8e3a-297fa4a3d19a> a odml:Section ;
  197. odml:hasName "%s" ;
  198. odml:hasType "%s" .
  199. """ % (author, date, sec_name, sec_type)
  200. rdoc = self.rdf_reader.from_string(rdf_doc, "turtle")
  201. self.assertEqual(rdoc[0].author, author)
  202. self.assertEqual(str(rdoc[0].date), date)
  203. self.assertEqual(len(rdoc[0].sections), 1)
  204. self.assertEqual(rdoc[0].sections[0].name, sec_name)
  205. with self.assertRaises(ValueError):
  206. self.rdf_reader.from_string(rdf_doc)