"""Ledger Correction logic tests.""" from unittest.mock import patch import pytest from ledger.constants.error import ERROR_LEDGER_CORRECTION_RECORD_ALREADY_EXISTS from ledger.logic import ledger_correction as logic from ledger.schemas.ledger_correction import ( LedgerCorrectionDetailSchema, LedgerCorrectionSchema, ) from tests.utils.factories import LedgerCorrectionFactory @patch('ledger.logic.ledger_correction.LedgerCorrection') def test_create_bulk(mock_model, mock_event_fixtures, mock_worksheet_correction): """Test bulk_create function.""" ledger_correction = LedgerCorrectionFactory.create() mock_model.build.return_value = ledger_correction post_data = LedgerCorrectionSchema(many=True).dump([ledger_correction]) res = logic.bulk_create(post_data) assert res.status == 201 assert res.message == LedgerCorrectionDetailSchema(many=True).dump( [ledger_correction] ) mock_model.build.assert_called_once_with(**post_data[0]) @patch('ledger.logic.ledger_correction.LedgerCorrection') def test_has_existing_ledger_records_error( mock_model, mock_event_fixtures, mock_worksheet_correction, mock_bulk_ledger_correction_request_body, ): """Test _has_existing_ledger_records function if record exist.""" mock_post_records = mock_bulk_ledger_correction_request_body ledger_correction = LedgerCorrectionFactory.create() mock_model.get_by_worksheet_correction_ids.return_value = [ledger_correction] with pytest.raises(Exception) as excinfo: logic._has_existing_ledger_records(mock_post_records) (msg,) = excinfo.value.args assert msg == ERROR_LEDGER_CORRECTION_RECORD_ALREADY_EXISTS.format([1]) mock_model.get_by_worksheet_correction_ids.assert_called_once_with([1, 2]) @patch('ledger.logic.ledger_correction.LedgerCorrection') def test_has_existing_ledger_records_no_error( mock_model, mock_bulk_ledger_correction_request_body ): """Test _has_existing_ledger_records function if there are no existing ledger records.""" mock_post_records = mock_bulk_ledger_correction_request_body mock_model.get_by_worksheet_correction_ids.return_value = [] res = logic._has_existing_ledger_records(mock_post_records) assert res is True mock_model.get_by_worksheet_correction_ids.assert_called_once_with([1, 2])