test_script_odml_to_rdf.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import shutil
  3. import unittest
  4. from docopt import DocoptExit
  5. from rdflib import Graph
  6. from odml.scripts import odml_to_rdf
  7. from . import util
  8. class TestScriptOdmlToRDF(unittest.TestCase):
  9. def setUp(self):
  10. self.tmp_dir = util.create_test_dir(__file__)
  11. self.dir_files = os.path.join(util.TEST_RESOURCES_DIR, "scripts", "odml_to_rdf")
  12. self.dir_invalid = os.path.join(self.dir_files, "test_invalid")
  13. self.dir_recursive = os.path.join(self.dir_files, "test_recursive")
  14. def tearDown(self):
  15. if self.tmp_dir and os.path.exists(self.tmp_dir):
  16. shutil.rmtree(self.tmp_dir)
  17. def test_script_exit(self):
  18. with self.assertRaises(DocoptExit):
  19. odml_to_rdf.main([])
  20. with self.assertRaises(DocoptExit):
  21. odml_to_rdf.main(["-o", self.tmp_dir])
  22. with self.assertRaises(SystemExit):
  23. odml_to_rdf.main(["-h"])
  24. with self.assertRaises(SystemExit):
  25. odml_to_rdf.main(["--version"])
  26. def _check_intermediate_dirs(self):
  27. # make sure an odml version conversion output directory has been created
  28. out_dir_lst = os.listdir(self.tmp_dir)
  29. self.assertEqual(len(out_dir_lst), 1)
  30. out_dir = os.path.join(self.tmp_dir, out_dir_lst[0])
  31. self.assertTrue(os.path.isdir(out_dir))
  32. # make sure an rdf conversion output directory has been created
  33. rdf_dir_lst = os.listdir(out_dir)
  34. self.assertEqual(len(rdf_dir_lst), 1)
  35. rdf_dir = os.path.join(out_dir, rdf_dir_lst[0])
  36. self.assertTrue(os.path.isdir(rdf_dir))
  37. return rdf_dir
  38. def test_valid_conversion(self):
  39. # make sure temp dir is empty
  40. self.assertListEqual(os.listdir(self.tmp_dir), [])
  41. # run converter on root directory containing two files
  42. odml_to_rdf.main(["-o", self.tmp_dir, self.dir_files])
  43. rdf_dir = self._check_intermediate_dirs()
  44. # make sure two files have been created
  45. file_lst = os.listdir(rdf_dir)
  46. self.assertEqual(len(file_lst), 2)
  47. # make sure the files are valid RDF files
  48. curr_graph = Graph()
  49. curr_graph.parse(os.path.join(rdf_dir, file_lst[0]))
  50. curr_graph.parse(os.path.join(rdf_dir, file_lst[1]))
  51. def test_invalid(self):
  52. # make sure temp dir is empty
  53. self.assertListEqual(os.listdir(self.tmp_dir), [])
  54. # run converter on directory with invalid files
  55. odml_to_rdf.main(["-o", self.tmp_dir, self.dir_invalid])
  56. rdf_dir = self._check_intermediate_dirs()
  57. # make sure no file has been created
  58. self.assertListEqual(os.listdir(rdf_dir), [])
  59. def test_recursive_conversion(self):
  60. # make sure temp dir is empty
  61. self.assertListEqual(os.listdir(self.tmp_dir), [])
  62. # run converter on root directory containing two files
  63. odml_to_rdf.main(["-r", "-o", self.tmp_dir, self.dir_recursive])
  64. rdf_dir = self._check_intermediate_dirs()
  65. # make sure two files have been created
  66. file_lst = os.listdir(rdf_dir)
  67. self.assertEqual(len(file_lst), 2)
  68. # make sure the files are valid RDF files
  69. curr_graph = Graph()
  70. curr_graph.parse(os.path.join(rdf_dir, file_lst[0]))
  71. curr_graph.parse(os.path.join(rdf_dir, file_lst[1]))