def _process_input_zip(self, zip_arch, result_folder, remote_prefix, file_name_base, expected_pairs, fix_number=True): """ Read entries in uploaded ZIP. Raise Exception in case pairs HDR/IMG are not matched or number "expected_pairs" is not met. :return string with HDR list (to be passed to DTI pipeline). """ hdr_files = [] for file_name in zip_arch.namelist(): if not file_name.startswith(file_name_base) or file_name.endswith("/"): continue if file_name.endswith(".hdr"): pair_img = file_name.replace(".hdr", ".img") if pair_img not in zip_arch.namelist(): raise ConnectException("Could not find pair for HDR file :" + str(file_name)) new_file_name = os.path.join(result_folder, file_name_base + str(len(hdr_files)) + ".hdr") src = zip_arch.open(file_name, 'rU') FilesHelper.copy_file(src, new_file_name) hdr_files.append(os.path.join(remote_prefix, os.path.split(new_file_name)[1])) new_file_name = new_file_name.replace(".hdr", ".img") src = zip_arch.open(pair_img, 'rU') FilesHelper.copy_file(src, new_file_name) elif not file_name.endswith(".img"): self.logger.warning("Ignored file :" + str(file_name)) if len(hdr_files) < expected_pairs or (fix_number and len(hdr_files) > expected_pairs): raise ConnectException("Invalid number of files "+ str(len(hdr_files)) + " expected "+ str(expected_pairs)) result = "" for hdr_name in hdr_files: result = result + hdr_name + " " return result
class DTITest(TransactionalTestCase): """ Test basic functionality of DTI Import Service. """ ### First dataSet FILE_1 = os.path.join(os.path.dirname(current_pack.__file__), "data", "TVB_ConnectionCapacityMatrix.csv") FILE_2 = os.path.join(os.path.dirname(current_pack.__file__), "data", "TVB_ConnectionDistanceMatrix.csv") ### Second dataSet FILE_3 = os.path.join(os.path.dirname(current_pack.__file__), "data", "TVB_ConnectionCapacityMatrix_3.csv") FILE_4 = os.path.join(os.path.dirname(current_pack.__file__), "data", "TVB_ConnectionDistanceMatrix_3.csv") def setUp(self): """ Reset the database before each test. """ # self.clean_database() self.test_user = TestFactory.create_user() self.test_project = TestFactory.create_project(self.test_user) self.service = DTIPipelineService('127.0.0.1', 'root') self.helper = FilesHelper() def test_process_csv(self): """ Test that a CSV generated on the server is correctly processed. """ folder = self.helper.get_project_folder(self.test_project, "TEMP") for file_name in [self.FILE_1, self.FILE_2, self.FILE_3, self.FILE_4]: intermediate_file = os.path.join(folder, os.path.split(file_name)[1]) self.helper.copy_file(file_name, intermediate_file) result_file = 'weights.txt' if 'Capacity' in file_name else 'tracts.txt' result_file = os.path.join(folder, result_file) self.service._process_csv_file(intermediate_file, result_file) matrix = read_list_data(result_file) self.assertEqual(96, len(matrix)) self.assertEqual(96, len(matrix[0]))