"""Test for retry stuck generation script.""" from unittest.mock import call from unittest.mock import patch from scripts import retry_stuck_generation from moneyhub.config import Config from tests.utils.factories import ReportCustomFactory from tests.utils.factories import StatementAttachmentFactory @patch('scripts.retry_stuck_generation.ReportCustom') @patch('scripts.retry_stuck_generation.StatementAttachment') @patch('scripts.retry_stuck_generation.sqs') def test_run( mock_sqs, mock_StatementAttachment, mock_ReportCustom ): """Test running the script.""" stuck_reports = [ ReportCustomFactory(report_custom_id=1), ReportCustomFactory(report_custom_id=2), ReportCustomFactory(report_custom_id=3), ] stuck_attachments = [ StatementAttachmentFactory(statement_attachment_id=123), StatementAttachmentFactory(statement_attachment_id=124), StatementAttachmentFactory(statement_attachment_id=125), ] mock_ReportCustom.get_stuck_in_progress.return_value = stuck_reports mock_StatementAttachment.get_stuck_in_progress.return_value = stuck_attachments retry_stuck_generation._run() assert mock_sqs.send_messages.call_count == 2 assert mock_sqs.send_messages.call_args_list[0] == call( Config.SQS_MH_CUSTOM_REPORTS_QUEUE_NAME, None, [ {'report_custom_id': 1}, {'report_custom_id': 2}, {'report_custom_id': 3}, ], ) (queue_name, _, messages) = mock_sqs.send_messages.call_args_list[1].args assert queue_name == Config.SQS_MH_GENERATE_ATTACHMENTS_QUEUE_NAME assert mock_sqs.send_messages.call_args_list[1] == call( Config.SQS_MH_GENERATE_ATTACHMENTS_QUEUE_NAME, None, [ {'statement_attachment_id': 123}, {'statement_attachment_id': 124}, {'statement_attachment_id': 125}, ], ) @patch('scripts.retry_stuck_generation.ReportCustom') @patch('scripts.retry_stuck_generation.StatementAttachment') @patch('scripts.retry_stuck_generation.sqs') def test_run_no_entries( mock_sqs, mock_StatementAttachment, mock_ReportCustom ): """Test running the script with no stuck entries.""" mock_ReportCustom.get_stuck_in_progress.return_value = [] mock_StatementAttachment.get_stuck_in_progress.return_value = [] retry_stuck_generation._run() mock_sqs.send_messages.assert_not_called()