def run(metadata_path, file_path): """application main method in charge to convert file in csv""" metadata_data = open(metadata_path, mode='r', encoding=f_type) metadata_list = DataParser.build_metadata_list(metadata_data) fixed_file_data = open(file_path, mode='r', encoding=f_type) result_content_list = DataParser.build_content_list( fixed_file_data, metadata_list) with open( build_file_path(PUBLIC_CSV_PATH, '/converted_file', CONVERTED_FILE_EXT), 'w', encoding=f_type, newline='\n', ) as csvfile: filewriter = writer(csvfile, delimiter=DELIMITER_FILE_OUTPUT) # create header on first line filewriter.writerow(list(i['m_label'] for i in metadata_list)) # create content for data in result_content_list: filewriter.writerow(data) csvfile.close()
def test_data_exception(self): """Test exception raised for invalid content""" file_path = f"{RESOURCES_PATH}/bad_fixed_file.2.txt" bad_fixed_file_data = open(file_path, mode='r', encoding=f_type) # wrong content length column with raises(Exception): assert DataParser.build_content_list(bad_fixed_file_data, self.default_metadata_list) # wrong nb column with raises(Exception): assert DataParser.build_content_list([ '1970-01-01John Smith\n', '1975-01-31Jane Doe 61.1\n', '1988-11-28Bob Big 102.4' ], self.default_metadata_list)
def test_data_parser_build_metadata_exception(self): """Test exception raised for invalid file type column""" with raises(Exception): assert DataParser.build_metadata_list( [ 'Birth date,10,date\n', 'Other value,15,new_type\n', ] )
def test_data_parser_format_data(self): assert DataParser.format_data("text ", "bad_type") == 'text ' assert DataParser.format_data("text") == 'text' assert DataParser.format_data() == ''
def _run_loop_on_tests_data(self, data_dict_list): for parameters_hash, expected_result in data_dict_list: assert DataParser.format_data( parameters_hash[0], parameters_hash[1] ) == expected_result
def _run_loop_on_tests_data(self, data_dict_list): for file_path, expected_result in data_dict_list: metadata_data = open(file_path, mode='r', encoding=f_type) assert DataParser.build_metadata_list( metadata_data ) == expected_result