"""Lambda test module.""" import datetime from unittest.mock import call, patch import pytest from constants import errors, slack_messages import index @pytest.fixture def mock_s3_event(): """S3 event mock.""" def _inner(xls_key, bucket): return { 'Records': [ { 's3': { 'bucket': {'name': bucket}, 'object': {'key': xls_key}, } } ] } return _inner @patch('snowflake_connector.etl_connector.SnowflakeSQLExecutor') @patch('models.reports.import_reconfirmation_report') @patch('util.convert_xsl_to_csv') @patch('util.notify_slack') def test_handler( notify_slack, convert_xsl_to_csv, import_reconfirmation_report, SnowflakeSQLExecutor, mock_s3_event): """Test handler function.""" date = datetime.date(2018, 1, 29) date_str = date.strftime('%Y_%m_%d') bucket = 'test-bucket' xls_key = ( 'soundexchange_reports/reconfirmation/input' '/2018-01-29/O-6145-THE_ORCHARD_ENTERPRISES_INC-062017.xlsx') csv_key = ( f'soundexchange/reconfirmation/converted/{date_str}/' f'O-6145-THE_ORCHARD_ENTERPRISES_INC-062017.csv') event = mock_s3_event(xls_key, bucket) result = index.handler(event, None) convert_xsl_to_csv.assert_called_with(bucket, xls_key, csv_key) import_reconfirmation_report.assert_called_with( SnowflakeSQLExecutor(), date.strftime('%Y-%m-%d'), csv_key, xls_key) notify_slack.assert_has_calls([ call(slack_messages.IMPORT_STARTED.format(xls_key)), call(slack_messages.IMPORT_SUCCESSFUL.format(xls_key)), ]) assert result == {'status': 'OK'} @patch('models.reports.import_reconfirmation_report') @patch('util.convert_xsl_to_csv') @patch('util.notify_slack') def test_handler_invalid_xls_path( notify_slack, convert_xsl_to_csv, import_reconfirmation_report, mock_s3_event): """Test handler for a case when the xls_path does not contain date.""" bucket = 'test-bucket' xls_key = 'reconfirmation/input/report.xlsx' event = mock_s3_event(xls_key, bucket) expected_error = errors.INVALID_XLS_KEY.format(xls_key) with pytest.raises(ValueError, match=expected_error): index.handler(event, None) convert_xsl_to_csv.assert_not_called() import_reconfirmation_report.assert_not_called() notify_slack.assert_has_calls([ call(slack_messages.IMPORT_STARTED.format(xls_key)), call(slack_messages.IMPORT_FAILED.format(expected_error)), ]) @patch('common_config.logger') @patch('models.reports.import_reconfirmation_report') @patch('util.convert_xsl_to_csv') @patch('util.notify_slack') def test_handler_not_an_xls_file( notify_slack, convert_xsl_to_csv, import_reconfirmation_report, logger, mock_s3_event): """Test handler for a case when the file has incorrect extension.""" bucket = 'test-bucket' xls_key = 'reconfirmation/input/report.csv' event = mock_s3_event(xls_key, bucket) index.handler(event, None) convert_xsl_to_csv.assert_not_called() import_reconfirmation_report.assert_not_called() logger.error.assert_called_with(errors.INCORRECT_FILE_ERROR) notify_slack.assert_has_calls([ call(slack_messages.IMPORT_STARTED.format(xls_key)), call(slack_messages.INCORRECT_EXTENSION), ]) @patch('snowflake_connector.etl_connector.SnowflakeSQLExecutor') @patch('common_config.logger') @patch('models.reports.import_reconfirmation_report') @patch('util.convert_xsl_to_csv', return_value=False) @patch('util.notify_slack') def test_handler_xls_file_does_not_exist( notify_slack, convert_xsl_to_csv, import_reconfirmation_report, logger, SnowflakeSQLExecutor, mock_s3_event): """Test handler for a case when the xls file does not exist in S3.""" bucket = 'test-bucket' xls_key = 'soundexchange/reconfirmation/input/2018-01-29/report.xlsx' event = mock_s3_event(xls_key, bucket) index.handler(event, None) import_reconfirmation_report.assert_not_called() logger.info.assert_called_with(errors.FILE_DOES_NOT_EXIST.format(xls_key)) notify_slack.assert_has_calls([ call(slack_messages.IMPORT_STARTED.format(xls_key)), call(slack_messages.FILE_DOES_NOT_EXIST.format(xls_key)), ]) @patch('index.sentry') @patch('snowflake_connector.etl_connector.SnowflakeSQLExecutor') @patch('common_config.logger') @patch('models.reports.import_reconfirmation_report', side_effect=Exception) @patch('util.convert_xsl_to_csv') @patch('util.notify_slack') def test_handler_xls_import_exception( notify_slack, convert_xsl_to_csv, import_reconfirmation_report, logger, SnowflakeSQLExecutor, sentry, mock_s3_event): """Test handler for a case when there is an exception during import.""" bucket = 'test-bucket' xls_key = 'soundexchange/reconfirmation/input/2018-01-29/report.xlsx' event = mock_s3_event(xls_key, bucket) with pytest.raises(Exception): index.handler(event, None) logger.exception.assert_called() sentry.capture_exception.assert_called() notify_slack.assert_has_calls([ call(slack_messages.IMPORT_STARTED.format(xls_key)), call(slack_messages.IMPORT_FAILED.format('')), ])