"""Unit Testcases for Processor.""" from unittest.mock import call, patch from correction_apply import config from correction_apply.processor import CorrectionApplyProcessor @patch('correction_apply.processor.get_unapplied_worksheet_corrections') def test_get_unapplied_worksheet_corrections( mock_request, mock_event, mock_worksheet_correction ): """Test to get unapplied worksheet corrections.""" mock_request.return_value = { 'items': [mock_worksheet_correction], 'total_count': 1 } processor = CorrectionApplyProcessor(mock_event) result = processor._get_unapplied_worksheet_corrections(0) assert result['items'] == [mock_worksheet_correction] assert result['total_count'] == 1 @patch('correction_apply.processor.app_logger') @patch('correction_apply.processor.get_unapplied_worksheet_corrections') def test_get_unapplied_worksheet_corrections_invalid_event( mock_request, mock_app_logger, mock_event, mock_worksheet_correction ): """Test to get unapplied worksheet corrections with invalid event_name.""" mock_request.return_value = { 'items': [mock_worksheet_correction], 'total_count': 1 } mock_event['event_name'] = 'Test' processor = CorrectionApplyProcessor(mock_event) processor._get_unapplied_worksheet_corrections(0) mock_app_logger.assert_has_calls( [call.info(config.NO_MATCHING_CORRECTION_TYPE.format('Test'))] ) @patch('correction_apply.processor.app_logger') @patch('correction_apply.processor.get_unapplied_worksheet_corrections') def test_get_unapplied_worksheet_corrections_no_result( mock_request, mock_app_logger, mock_event, mock_worksheet_correction ): """Test to get unapplied worksheet corrections. When the endpoint does not return any results. """ mock_request.return_value = { 'items': [mock_worksheet_correction], 'total_count': 0 } processor = CorrectionApplyProcessor(mock_event) processor._get_unapplied_worksheet_corrections(0) mock_app_logger.assert_has_calls([ call.info( config.NO_UNAPPLIED_WORKSHEET_CORRECTION.format(2, 'royalty_reversal') ) ]) @patch('correction_apply.processor.app_logger') @patch('correction_apply.processor.LedgerProcessor') @patch('correction_apply.processor.CorrectionApplyProcessor' '._get_unapplied_worksheet_corrections') def test_processor_process( mock_request, mock_ledger_processor, mock_app_logger, mock_event, mock_worksheet_correction ): """Test Processor.process method.""" mock_request.return_value = { 'items': [mock_worksheet_correction], 'total_count': 1 } processor = CorrectionApplyProcessor(mock_event) processor.process() assert mock_ledger_processor.return_value.process.call_count == 1 mock_request.assert_called_once_with(0) mock_app_logger.assert_has_calls([ call.info(config.CORRECTION_APPLY_FINISHED) ]) @patch('correction_apply.processor.LedgerEntryQueue') @patch('correction_apply.processor.LedgerProcessor.process') @patch('correction_apply.processor.CorrectionApplyProcessor' '._post_ledger_entries') def test_process_and_create_ledger_entries( mock_post_request, mock_ledger_processor_process, mock_ledger_entry_queue, mock_event, mock_worksheet_correction ): """Test _process_and_create_ledger_entries method.""" mock_post_request.return_value = True mock_ledger_entry_queue.return_value.length = 1 processor = CorrectionApplyProcessor(mock_event) processor._process_and_create_ledger_centries([mock_worksheet_correction]) assert mock_ledger_processor_process.call_count == 1 mock_post_request.assert_called_once() @patch('correction_apply.processor.app_logger') @patch('correction_apply.processor.LedgerEntryQueue') def test_post_ledger_entries( mock_ledger_entry_queue, mock_app_logger, mock_event, ): """Test _post_ledger_entries method.""" worksheet_correction_ids = [1, 2] mock_ledger_entry_queue.return_value.flush_entries.return_value = True mock_ledger_correction_entry_queue = mock_ledger_entry_queue processor = CorrectionApplyProcessor(mock_event) processor._post_ledger_entries( mock_ledger_correction_entry_queue, mock_ledger_entry_queue, worksheet_correction_ids ) mock_ledger_entry_queue.flush_entries.call_count == 2 mock_app_logger.assert_has_calls([ call.info(config.LEDGER_BATCH_MSG.format(worksheet_correction_ids)), call.info(config.CREATE_LEDGER_CORRECTION_ENTRIES_MSG), call.info(config.CREATE_LEDGER_ENTRIES_MSG) ])