"""export_processed_dig_sales.py unit tests.""" import os from unittest.mock import MagicMock from unittest.mock import mock_open from unittest.mock import patch import boto3 from moto import mock_s3 from export_processed_dig_sales import CliArgs from export_processed_dig_sales import compress_file from export_processed_dig_sales import get_final_local_file_path from export_processed_dig_sales import main from export_processed_dig_sales import mysql_unload from export_processed_dig_sales import s3_file_exists from export_processed_dig_sales import s3_upload_file @mock_s3 def test_s3_file_exists(): """Test if a file exists on s3.""" bucket_name = 'test_bucket' key_name = 'test_key' s3 = boto3.resource('s3') bucket = s3.create_bucket(Bucket=bucket_name) bucket.put_object(Key=key_name, Body=b'im data') assert s3_file_exists(bucket_name, key_name) @mock_s3 def test_s3_file_exists_no_file(): """Test if a file does not exist on s3.""" bucket_name = 'test_bucket' key_name = 'test_key' s3 = boto3.resource('s3') bucket = s3.create_bucket(Bucket=bucket_name) bucket.put_object(Key=key_name, Body=b'im data') assert not s3_file_exists(bucket_name, 'oops') @patch('export_processed_dig_sales.MySQLdb') def test_mysql_unload(mock_mysql): """Test mysql_unload.""" mock_db = MagicMock() mock_cursor = MagicMock() mock_db.cursor.return_value = mock_cursor mock_mysql.connect.return_value = mock_db outfile_path = '/my/local/path' period_id = 666 with patch('builtins.open', mock_open(read_data='sql')): mysql_unload(period_id, outfile_path) assert mock_cursor.execute.call_args[0] == ('sql', { 'period_id': period_id, 'local_path': outfile_path}) assert mock_db.close.call_count == 1 assert mock_cursor.close.call_count == 1 def test_compress_file(): """Test compress_file.""" source_path = './tests/test.txt' dest_path = './tests/test.txt.gz' assert not os.path.exists(dest_path) compress_file(source_path, dest_path) assert os.path.exists(dest_path) os.remove(dest_path) @mock_s3 def test_s3_upload_file(): """Test S3 file upload.""" bucket_name = 'my_bucket' key_name = 'my_key' local_file = './tests/test.txt' s3 = boto3.resource('s3') s3.create_bucket(Bucket=bucket_name) s3_upload_file(bucket_name, key_name, local_file) assert s3_file_exists(bucket_name, key_name) def test_get_final_local_file_path(): """Test getting compressed file path.""" # Set APP_FILE_DIR so the check for its existence passes. os.environ['APP_FILE_DIR'] = './tests' file_path = get_final_local_file_path() assert file_path[-3:] == '.gz' def test_get_final_local_file_path_no_compress(): """Test getting final file path.""" file_path = get_final_local_file_path(True) assert file_path[-3:] != '.gz' @patch('export_processed_dig_sales.get_final_local_file_path') @patch('export_processed_dig_sales.s3_file_exists') @patch('export_processed_dig_sales.os.path.exists') @patch('export_processed_dig_sales.parse_args') @patch('export_processed_dig_sales.s3_upload_file') @patch('export_processed_dig_sales.mysql_unload') def test_main_skip_unload( mock_unload, mock_write, mock_parse, mock_path_exists, mock_s3_exists, mock_get_path): """Test main logic.""" mock_parse.return_value = CliArgs( overwrite=True, skip_unload=True, skip_compress=False) mock_path_exists.return_value = False mock_s3_exists.return_value = False mock_get_path.return_value = '/tmp/file.gz' main() assert mock_unload.call_count == 0 assert mock_write.call_count == 1 assert mock_get_path.call_count == 1 @patch('export_processed_dig_sales.get_final_local_file_path') @patch('export_processed_dig_sales.compress_file') @patch('export_processed_dig_sales.s3_file_exists') @patch('export_processed_dig_sales.os.path.exists') @patch('export_processed_dig_sales.parse_args') @patch('export_processed_dig_sales.s3_upload_file') @patch('export_processed_dig_sales.mysql_unload') def test_main_unload( mock_unload, mock_write, mock_parse, mock_path_exists, mock_s3_exists, mock_compress_file, mock_get_path): """Test main logic.""" mock_parse.return_value = CliArgs( overwrite=True, skip_unload=False, skip_compress=False) mock_path_exists.return_value = False mock_s3_exists.return_value = False mock_compress_file.return_value = None mock_get_path.return_value = '/tmp/file.gz' main() assert mock_unload.call_count == 1 assert mock_write.call_count == 1 assert mock_compress_file.call_count == 1 assert mock_get_path.call_count == 1 @patch('export_processed_dig_sales.get_final_local_file_path') @patch('export_processed_dig_sales.parse_args') @patch('export_processed_dig_sales.s3_upload_file') @patch('export_processed_dig_sales.s3_file_exists') @patch('export_processed_dig_sales.mysql_unload') def test_main_not_overwrite( mock_unload, mock_exists, mock_write, mock_parse, mock_get_path): """Test main logic.""" mock_parse.return_value = CliArgs( overwrite=False, skip_unload=False, skip_compress=False) mock_exists.return_value = True mock_get_path.return_value = '/tmp/file.gz' main() assert mock_unload.call_count == 0 assert mock_write.call_count == 0 @patch('export_processed_dig_sales.get_final_local_file_path') @patch('export_processed_dig_sales.compress_file') @patch('export_processed_dig_sales.s3_file_exists') @patch('export_processed_dig_sales.os.path.exists') @patch('export_processed_dig_sales.os.remove') @patch('export_processed_dig_sales.parse_args') @patch('export_processed_dig_sales.s3_upload_file') @patch('export_processed_dig_sales.mysql_unload') def test_main_remove( mock_unload, mock_write, mock_parse, mock_remove, mock_path_exists, mock_s3_exists, mock_compress, mock_get_path): """Test main logic.""" mock_parse.return_value = CliArgs( overwrite=True, skip_unload=False, skip_compress=False) mock_path_exists.return_value = True mock_s3_exists.return_value = True mock_compress.return_value = None mock_get_path.return_value = '/tmp/file.gz' main() assert mock_remove.call_count == 1 assert mock_unload.call_count == 1 assert mock_write.call_count == 1 assert mock_compress.call_count == 1 assert mock_get_path.call_count == 1