"""Test for regenerate custom reports script.""" from unittest.mock import patch from scripts import regenerate_custom_reports from moneyhub.config import Config from moneyhub.constants.constants import ReportCustomStatus from tests.utils.factories import ReportCustomFactory @patch('scripts.regenerate_custom_reports.ReportCustom') @patch('scripts.regenerate_custom_reports.sqs') def test_run(mock_sqs, mock_ReportCustom): """Test running the script.""" statement_period_id = 123 report_custom_ids = [1, 2, 3] reports = [ ReportCustomFactory(report_custom_id=1, report_custom_status=ReportCustomStatus.COMPLETE), ReportCustomFactory(report_custom_id=2, report_custom_status=ReportCustomStatus.COMPLETE), ReportCustomFactory(report_custom_id=3, report_custom_status=ReportCustomStatus.ERROR), ] mock_ReportCustom.get_by_statement_period.return_value = reports regenerate_custom_reports._run(statement_period_id, report_custom_ids) assert all( report.report_custom_status == ReportCustomStatus.IN_PROGRESS for report in reports ) mock_ReportCustom.get_by_statement_period.assert_called_once_with( statement_period_id, report_custom_ids) mock_ReportCustom.commit_changes.assert_called_once() mock_sqs.send_messages.assert_called_once_with( Config.SQS_MH_CUSTOM_REPORTS_QUEUE_NAME, None, [ {'report_custom_id': 1}, {'report_custom_id': 2}, {'report_custom_id': 3}, ], ) @patch('scripts.regenerate_custom_reports.ReportCustom') @patch('scripts.regenerate_custom_reports.sqs') def test_run_no_entries(mock_sqs, mock_ReportCustom): """Test running the script when no reports are found.""" mock_ReportCustom.get_by_statement_period.return_value = [] regenerate_custom_reports._run(123, []) mock_ReportCustom.commit_changes.assert_not_called() mock_sqs.send_messages.assert_not_called()