"""Unit tests for notifications tasks.""" from unittest import mock from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.tasks import notification_tasks @pytest.fixture def mock_boto3(): """Mock boto3.""" boto3_path = ( 'feed_ingestion.tasks.notification_tasks.boto3') with patch(boto3_path) as boto3: mock_client = MagicMock() mock_client.send_email = MagicMock() boto3.client.return_value = mock_client yield boto3 def test_notify_new_ms_files(mock_boto3): """Test notify_new_ms_files activity.""" sub = 'New files for google_play_marketshare' body = ( 'There are new files available ' 'for google_play_marketshare 2018-08-01.') notification_tasks.notify_new_ms_files( MagicMock(), 'google_play_marketshare', '2018-08-01') assert (mock_boto3.client.return_value. send_email.call_args_list) == [ mock.call().send_email(Destination={ 'ToAddresses': [ 'data-squad@theorchard.com', 'DataAnalyticsDept@theorchard.com']}, Message={ 'Subject': {'Data': sub, 'Charset': 'UTF-8'}, 'Body': { 'Text': {'Data': body, 'Charset': 'UTF-8'}}}, Source='datarequests@theorchard.com') ] @pytest.fixture def mock_task_status(): """Yield task status.""" task_status_path = 'feed_ingestion.tasks.task_status' with patch(task_status_path) as task_status: task_status.is_completed_task.return_value = False task_status.mark_completed_task = MagicMock() yield task_status def test_sns_publish_message(mock_boto3, mock_task_status): """Test sns_publish_message activity.""" notification_tasks.sns_publish_message( MagicMock(), 'feed_name', '2018-08-01', 'topic', 'message', 'subject') assert mock_boto3.client.return_value.publish.call_args_list == [ mock.call().publish( Subject='subject', Message='message', TopicArn='topic')]