test_format_converter.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import os
  2. import shutil
  3. import tempfile
  4. import unittest
  5. from contextlib import contextmanager
  6. from odml.tools.converters import FormatConverter
  7. from .util import create_test_dir
  8. FC = FormatConverter
  9. # TODO The used NamedTemporaryFile does not play nice with Windows;
  10. # deactivating all affected tests for Windows until this has been fixed.
  11. class TestFormatConverter(unittest.TestCase):
  12. def setUp(self):
  13. self.doc = """<odML version="1.1">
  14. <section>
  15. <type>some</type>
  16. <name>S</name>
  17. </section>
  18. </odML>
  19. """
  20. self.tmp_dir = None
  21. def tearDown(self):
  22. if self.tmp_dir and os.path.exists(self.tmp_dir):
  23. shutil.rmtree(self.tmp_dir)
  24. @contextmanager
  25. def assertNotRaises(self, exc_type):
  26. try:
  27. yield None
  28. except exc_type:
  29. raise self.failureException('{} raised'.format(exc_type.__name__))
  30. def _create_open_file(self, in_dir):
  31. in_file = tempfile.NamedTemporaryFile(mode='a+', suffix=".xml", dir=in_dir)
  32. in_file.write(self.doc)
  33. in_file.seek(0)
  34. return in_file
  35. def test_convert(self):
  36. if os.name == 'nt':
  37. raise unittest.SkipTest("Skipping test on Windows")
  38. self.test_convert_dir_no_output_dir(False, FC.convert)
  39. self.test_convert_dir_no_output_dir(True, FC.convert)
  40. self.test_convert_dir_with_output_dir_specified(FC.convert)
  41. def test_convert_dir(self):
  42. if os.name == 'nt':
  43. raise unittest.SkipTest("Skipping test on Windows")
  44. with self.assertRaises(ValueError):
  45. FC.convert_dir(None, None, False, "not valid format")
  46. # Testing recursive part of the converter for not nested input dir
  47. self.test_convert_dir_no_output_dir(True)
  48. def test_convert_dir_no_output_dir(self, recursive=False, func=None):
  49. if os.name == 'nt':
  50. raise unittest.SkipTest("Skipping test on Windows")
  51. self.tmp_dir = create_test_dir(__file__)
  52. in_dir = tempfile.mkdtemp(dir=self.tmp_dir)
  53. in_file = self._create_open_file(in_dir)
  54. in_file2 = self._create_open_file(in_dir)
  55. if not func:
  56. FC.convert_dir(in_dir, None, recursive, "odml")
  57. else:
  58. if recursive:
  59. func([in_dir, "odml", "-r"])
  60. else:
  61. func([in_dir, "odml"])
  62. files = []
  63. for dir_path, dir_names, file_names in os.walk(self.tmp_dir):
  64. for file_name in file_names:
  65. files.append(os.path.join(dir_path, file_name))
  66. # check if the input file in the correct repo
  67. self.assertIn(os.path.join(in_dir, in_file.name), files)
  68. self.assertIn(os.path.join(in_dir, in_file2.name), files)
  69. # check if the output file in correct repo and has intended name
  70. in_files = [in_file.name, in_file2.name]
  71. for file in in_files:
  72. odml_file_name = os.path.basename(file)
  73. odml_file_name = odml_file_name.replace(".xml", ".odml")
  74. root, odml_dir_name = os.path.split(in_dir)
  75. odml_dir_name = odml_dir_name + "_odml"
  76. self.assertIn(os.path.join(root, odml_dir_name, odml_file_name), files)
  77. in_file.close()
  78. in_file2.close()
  79. def test_convert_dir_with_output_dir_specified(self, func=None):
  80. if os.name == 'nt':
  81. raise unittest.SkipTest("Skipping test on Windows")
  82. # Testing FC.convert_dir(in_dir, out_dir, False, "odml")
  83. self.tmp_dir = create_test_dir(__file__)
  84. in_dir = tempfile.mkdtemp(dir=self.tmp_dir)
  85. out_dir = tempfile.mkdtemp(dir=self.tmp_dir)
  86. in_file = self._create_open_file(in_dir)
  87. in_file2 = self._create_open_file(in_dir)
  88. if not func:
  89. FC.convert_dir(in_dir, out_dir, False, "odml")
  90. else:
  91. func([in_dir, "odml", "-out", out_dir])
  92. in_files = []
  93. out_files = []
  94. for dir_path, dir_names, file_names in os.walk(in_dir):
  95. for file_name in file_names:
  96. in_files.append(os.path.join(dir_path, file_name))
  97. for dir_path, dir_names, file_names in os.walk(out_dir):
  98. for file_name in file_names:
  99. out_files.append(os.path.join(dir_path, file_name))
  100. # check if the input file in the correct repo
  101. self.assertIn(os.path.join(in_dir, in_file.name), in_files)
  102. self.assertIn(os.path.join(in_dir, in_file2.name), in_files)
  103. # check if the output file in correct repo and has intended name
  104. check_in_files = [in_file.name, in_file2.name]
  105. for file in check_in_files:
  106. _, out_file_name = os.path.split(file)
  107. pre, ext = os.path.splitext(out_file_name)
  108. out_file_name = out_file_name.replace(out_file_name, pre + ".odml")
  109. self.assertIn(os.path.join(out_dir, out_file_name), out_files)
  110. in_file.close()
  111. in_file2.close()
  112. def test_check_io_directory(self):
  113. self.tmp_dir = create_test_dir(__file__)
  114. out_dir = tempfile.mkdtemp(dir=self.tmp_dir)
  115. in_dir = tempfile.mkdtemp(dir=self.tmp_dir)
  116. with self.assertRaises(ValueError):
  117. FC._check_input_output_directory(None, None)
  118. with self.assertRaises(ValueError):
  119. FC._check_input_output_directory("/not_valid_path", None)
  120. with self.assertRaises(ValueError):
  121. FC._check_input_output_directory(in_dir, "/not_valid_path")
  122. self.assertNotRaises(FC._check_input_output_directory(in_dir, None))
  123. self.assertNotRaises(FC._check_input_output_directory(in_dir, out_dir))