"""Tests for XLSX to Pandas module.""" # import test_track_list from types import SimpleNamespace from unittest.mock import patch, MagicMock from integration_scripts.xlsx_to_pandas_s3 import convert_all_xlsx_to_csv_s3, \ convert_all_csv_to_xlsx_s3, get_files_from_s3_bucket, \ convert_dataframe_to_csv_file, convert_dataframe_to_csv_s3, \ convert_dataframe_to_xlsx_s3 @patch('integration_scripts.xlsx_to_pandas_s3.s3_utils.filter_file_keys') @patch('integration_scripts.xlsx_to_pandas_s3.read_xlsx_to_dataframe_s3') @patch('integration_scripts.xlsx_to_pandas_s3.s3_resource') def test_convert_all_xlsx_to_csv( mock_resource, mock_df, mock_filter, test_track_list_xlsx): source_folder_key = 'source_key' output_folder_key = 'output_key' mock_filter.return_value = test_track_list_xlsx mock_df.head = MagicMock mock_df.to_csv = MagicMock mock_resource.Object = MagicMock mock_resource.Object.put = MagicMock convert_all_xlsx_to_csv_s3(source_folder_key, output_folder_key) # TODO: Test sorting is performed assert mock_df.call_count == 3 @patch('pandas.core.frame.DataFrame') @patch('builtins.open') def test_convert_dataframe_to_csv_file(mock_open, mock_df): """Test converting all CSV files in an s3 bucket to XLSX. """ # if not output_folder_key: # output_folder_key = source_folder_key # # converted_list = list() # mock_filter.return_value = test_s3_track_list_clean # mock_read_csv.return_value = mock_df mock_dropna = MagicMock() mock_df.dropna.return_value = mock_dropna mock_dropna.to_csv.return_value = MagicMock() filename = 'bucket/path/test_filename.csv' # open_name = 'builtins.open' # with patch('{}.open'.format(__name__), create=True) as mock_open: with patch('__main__.open', mock_open()): convert_dataframe_to_csv_file(mock_df, filename) # open_ = mock_open() # with patch.object('builtins', 'open', open_): assert mock_df.dropna.call_count == 1 assert mock_dropna.to_csv.call_count == 1 @patch('integration_scripts.xlsx_to_pandas_s3.s3_utils.filter_backoff') @patch('integration_scripts.xlsx_to_pandas_s3.s3_utils.get_object_backoff') @patch('pandas.core.frame.DataFrame') @patch('integration_scripts.xlsx_to_pandas_s3.pd.read_csv') @patch('integration_scripts.xlsx_to_pandas_s3.s3_resource') @patch('integration_scripts.xlsx_to_pandas_s3.convert_dataframe_to_xlsx_s3') def test_convert_all_csv_to_xlsx_s3( mock_to_excel, mock_resource, mock_read_csv, mock_df, mock_get_object, mock_filter, test_s3_track_list_clean, test_to_excel_return_values, test_to_excel_results): output_folder_key = 'output_key' mock_filter.return_value = test_s3_track_list_clean mock_read_csv.return_value = mock_df mock_to_excel.side_effect = test_to_excel_return_values mock_resource.Object = MagicMock mock_resource.Object.put = MagicMock check_value = convert_all_csv_to_xlsx_s3(output_folder_key) assert mock_to_excel.call_count == 3 assert check_value == test_to_excel_results assert mock_read_csv.call_count == 3 assert mock_get_object.call_count == 0 @patch('integration_scripts.xlsx_to_pandas_s3.s3_utils.filter_backoff') def test_get_files_from_s3_bucket(mock_filter, test_s3_track_list_clean, test_s3_track_list_mixed): """Test files are parsed from S3 key.""" # Case 1 - all good mock_filter.return_value = test_s3_track_list_clean filter_response_1 = { 'path1': [ SimpleNamespace(key='bucket/path1/file_1.csv'), SimpleNamespace(key='bucket/path1/file_2.csv') ], 'path2': [ SimpleNamespace(key='bucket/path2/file_3.csv') ] } response = get_files_from_s3_bucket('folder_key') assert response == filter_response_1 # Case 2 - mixed mock_filter.return_value = test_s3_track_list_mixed filter_response_2 = { 'path2': [ SimpleNamespace(key='bucket/path2/file_2.csv') ], 'path3': [ SimpleNamespace(key='bucket/path2/path3/file_4.csv') ] } response = get_files_from_s3_bucket('folder_key') assert response == filter_response_2 # Case 3 - empty mock_filter.return_value = [] filter_response_3 = {} response = get_files_from_s3_bucket('folder_key') assert response == filter_response_3 @patch('pandas.core.frame.DataFrame') @patch('integration_scripts.xlsx_to_pandas_s3.s3_resource') def test_convert_dataframe_to_xlsx_s3(mock_s3_resource, mock_dframe): """Test converting dataframe to excel doc.""" # Check default case df_result = MagicMock() mock_dframe.dropna.return_value = df_result mock_s3_resource.Object = MagicMock mock_s3_resource.Object.put = MagicMock output_path_and_file = 'test_file_name' source_key, target_key = convert_dataframe_to_xlsx_s3( mock_dframe, output_path_and_file) assert mock_dframe.dropna.call_count == 1 assert source_key == 'test_file_name.csv' assert target_key == 'test_file_name.xlsx' mock_dframe.dropna.assert_called_with(axis='rows', how='all') df_result.to_excel.assert_called_once() # Check added drop_by case drop_by = 'test' convert_dataframe_to_xlsx_s3(mock_dframe, output_path_and_file, drop_by) mock_dframe.dropna.assert_called_with( axis='rows', how='all', subset=[drop_by]) @patch('pandas.core.frame.DataFrame') @patch('integration_scripts.xlsx_to_pandas_s3.s3_resource') def test_convert_dataframe_to_csv_s3(mock_s3_resource, mock_dframe): """Convert a DataFrame to an CSV file and save it to an S3 object.""" df_result = MagicMock() mock_dframe.dropna.return_value = df_result mock_s3_resource.Object = MagicMock mock_s3_resource.Object.put = MagicMock output_path_and_file = 'test_file_name' target_key = convert_dataframe_to_csv_s3(mock_dframe, output_path_and_file) assert mock_dframe.dropna.call_count == 1 assert target_key == 'test_file_name.csv' mock_dframe.dropna.assert_called_with(axis='index', how='all') df_result.to_csv.assert_called_once() # TODO: More tests.