test_parser_odml.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 tempfile
  8. import unittest
  9. from odml.tools import odmlparser
  10. class TestOdmlParser(unittest.TestCase):
  11. def setUp(self):
  12. # Set up test environment
  13. dir_path = os.path.dirname(os.path.realpath(__file__))
  14. self.basefile = os.path.join(dir_path, "resources", "example.odml")
  15. self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
  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(self.basefile)
  29. def tearDown(self):
  30. if 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. self.rdf_writer.write_file(self.odml_doc, self.rdf_file)
  62. rdf_doc = self.rdf_reader.from_file(self.rdf_file, "xml")
  63. self.assertEqual(self.odml_doc, rdf_doc[0])
  64. # RDF does not preserve the order of sections or properties,
  65. # check the attributes by hand to make sure everything
  66. # was correctly imported.
  67. self.assertEqual(len(rdf_doc), 1)
  68. self.assertEqual(rdf_doc[0].author, self.odml_doc.author)
  69. self.assertEqual(rdf_doc[0].version, self.odml_doc.version)
  70. self.assertEqual(rdf_doc[0].date, self.odml_doc.date)
  71. self.assertIn(self.odml_doc.sections[0].name, rdf_doc[0].sections)
  72. self.assertIn(self.odml_doc.sections[1].name, rdf_doc[0].sections)
  73. # Check error on missing document format
  74. with self.assertRaises(ValueError):
  75. self.rdf_reader.from_file(self.rdf_file)
  76. def test_xml_string(self):
  77. # Read from string
  78. author = "HPL"
  79. date = "1890-08-20"
  80. sec_name = "section name"
  81. sec_type = "section type"
  82. doc = """
  83. <odML version="1.1">
  84. <author>%s</author>
  85. <date>%s</date>
  86. <section>
  87. <name>%s</name>
  88. <type>%s</type>
  89. </section>
  90. </odML>
  91. """ % (author, date, sec_name, sec_type)
  92. xml_doc = self.xml_reader.from_string(doc)
  93. self.assertEqual(xml_doc.author, author)
  94. self.assertEqual(str(xml_doc.date), date)
  95. self.assertEqual(len(xml_doc.sections), 1)
  96. self.assertEqual(xml_doc.sections[0].name, sec_name)
  97. def test_json_string(self):
  98. author = "HPL"
  99. date = "1890-08-20"
  100. sec_name = "section name"
  101. sec_type = "section type"
  102. doc = """
  103. {
  104. "odml-version": "1.1",
  105. "Document": {
  106. "author": "%s",
  107. "date": "%s",
  108. "sections": [{
  109. "name": "%s",
  110. "type": "%s"
  111. }]
  112. }
  113. }
  114. """ % (author, date, sec_name, sec_type)
  115. json_doc = self.json_reader.from_string(doc)
  116. self.assertEqual(json_doc.author, author)
  117. self.assertEqual(str(json_doc.date), date)
  118. self.assertEqual(len(json_doc.sections), 1)
  119. self.assertEqual(json_doc.sections[0].name, sec_name)
  120. # Test empty return on broken json document
  121. self.assertIsNone(self.json_reader.from_string("{"))
  122. def test_yaml_string(self):
  123. author = "HPL"
  124. date = "1890-08-20"
  125. sec_name = "section name"
  126. sec_type = "section type"
  127. yaml_doc = """
  128. odml-version: '1.1'
  129. Document:
  130. author: %s
  131. date: %s
  132. sections:
  133. - name: %s
  134. type: %s
  135. """ % (author, date, sec_name, sec_type)
  136. ydoc = self.yaml_reader.from_string(yaml_doc)
  137. self.assertEqual(ydoc.author, author)
  138. self.assertEqual(str(ydoc.date), date)
  139. self.assertEqual(len(ydoc.sections), 1)
  140. self.assertEqual(ydoc.sections[0].name, sec_name)
  141. def test_rdf_string(self):
  142. author = "HPL"
  143. date = "1890-08-20"
  144. sec_name = "section name"
  145. sec_type = "section type"
  146. rdf_doc = u"""
  147. @prefix odml: <https://g-node.org/projects/odml-rdf#> .
  148. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
  149. @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
  150. @prefix xml: <http://www.w3.org/XML/1998/namespace> .
  151. @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
  152. odml:Hub odml:hasDocument <https://g-node.org/projects/odml-rdf#1c9ca24a-1d2c-40b6-a096-5a48efbd77d0> .
  153. <https://g-node.org/projects/odml-rdf#1c9ca24a-1d2c-40b6-a096-5a48efbd77d0> a odml:Document ;
  154. odml:hasAuthor "%s" ;
  155. odml:hasDate "%s"^^xsd:date ;
  156. odml:hasSection <https://g-node.org/projects/odml-rdf#2abc6711-34e1-4102-8e3a-297fa4a3d19a> .
  157. <https://g-node.org/projects/odml-rdf#2abc6711-34e1-4102-8e3a-297fa4a3d19a> a odml:Section ;
  158. odml:hasName "%s" ;
  159. odml:hasType "%s" .
  160. """ % (author, date, sec_name, sec_type)
  161. rdoc = self.rdf_reader.from_string(rdf_doc, "turtle")
  162. self.assertEqual(rdoc[0].author, author)
  163. self.assertEqual(str(rdoc[0].date), date)
  164. self.assertEqual(len(rdoc[0].sections), 1)
  165. self.assertEqual(rdoc[0].sections[0].name, sec_name)
  166. with self.assertRaises(ValueError):
  167. self.rdf_reader.from_string(rdf_doc)