test_script_odml_convert.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. import shutil
  3. import unittest
  4. from docopt import DocoptExit
  5. from odml import load as odml_load
  6. from odml.scripts import odml_convert
  7. from . import util
  8. class TestScriptOdmlConvert(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_convert")
  12. self.dir_broken = os.path.join(self.dir_files, "test_broken")
  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_convert.main([])
  20. with self.assertRaises(DocoptExit):
  21. odml_convert.main(["-o", self.tmp_dir])
  22. with self.assertRaises(SystemExit):
  23. odml_convert.main(["-h"])
  24. with self.assertRaises(SystemExit):
  25. odml_convert.main(["--version"])
  26. def test_broken(self):
  27. # make sure temp dir is empty
  28. self.assertListEqual(os.listdir(self.tmp_dir), [])
  29. # run converter on directory with invalid files
  30. odml_convert.main(["-o", self.tmp_dir, self.dir_broken])
  31. # make sure an output directory has been created
  32. out_dir_lst = os.listdir(self.tmp_dir)
  33. self.assertEqual(len(out_dir_lst), 1)
  34. out_dir = os.path.join(self.tmp_dir, out_dir_lst[0])
  35. self.assertTrue(os.path.isdir(out_dir))
  36. # make sure no file has been created
  37. self.assertListEqual(os.listdir(out_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_convert.main(["-o", self.tmp_dir, self.dir_files])
  43. # make sure an output directory has been created
  44. out_dir_lst = os.listdir(self.tmp_dir)
  45. self.assertEqual(len(out_dir_lst), 1)
  46. out_dir = os.path.join(self.tmp_dir, out_dir_lst[0])
  47. self.assertTrue(os.path.isdir(out_dir))
  48. # make sure two files have been created
  49. file_lst = os.listdir(out_dir)
  50. self.assertEqual(len(file_lst), 2)
  51. # make sure the files are valid odml files
  52. _ = odml_load(os.path.join(out_dir, file_lst[0]))
  53. _ = odml_load(os.path.join(out_dir, file_lst[1]))
  54. def test_recursive_conversion(self):
  55. # make sure temp dir is empty
  56. self.assertListEqual(os.listdir(self.tmp_dir), [])
  57. # run converter on root directory containing two files
  58. odml_convert.main(["-r", "-o", self.tmp_dir, self.dir_recursive])
  59. # make sure an output directory has been created
  60. out_dir_lst = os.listdir(self.tmp_dir)
  61. self.assertEqual(len(out_dir_lst), 1)
  62. out_dir = os.path.join(self.tmp_dir, out_dir_lst[0])
  63. self.assertTrue(os.path.isdir(out_dir))
  64. # make sure two files have been created
  65. file_lst = os.listdir(out_dir)
  66. self.assertEqual(len(file_lst), 2)
  67. # make sure the files are valid odml files
  68. _ = odml_load(os.path.join(out_dir, file_lst[0]))
  69. _ = odml_load(os.path.join(out_dir, file_lst[1]))