"""Worksheet tax correction logic tests.""" from decimal import Decimal from unittest.mock import call, patch import pytest from sqlalchemy import exc from payment.constants import constants, error from payment.logic import worksheet_tax_correction as logic from payment.logic.exceptions import LogicError from tests.utils.factories import WorksheetTaxCorrectionFactory @pytest.mark.parametrize( 'correction_type, correction_status', ( (constants.CORRECTION_TYPES.WHT, constants.CORRECTION_STATUSES.PENDING), (constants.CORRECTION_TYPES.VAT, constants.CORRECTION_STATUSES.PENDING), (constants.CORRECTION_TYPES.WHT, constants.CORRECTION_STATUSES.ACTIVE), (constants.CORRECTION_TYPES.VAT, constants.CORRECTION_STATUSES.ACTIVE), ), ) @patch('payment.logic.worksheet_tax_correction.repository') def test_get_tax_corrections( mock_repository, correction_type, correction_status, mock_accounts, mock_contracts, mock_abacus_event, mock_ledger_account_contracts, faker, ): """Test get_tax_corrections method.""" tax_correction = WorksheetTaxCorrectionFactory.create() correction_statement_period_id = faker.pyint() contract_ids = faker.pylist(value_types=[int]) test_limit = faker.pyint(20, 300) test_offset = faker.pyint(1, 10) mock_repository.get_filtered_worksheet_tax_corrections.return_value = ( [tax_correction], 1, ) result = logic.get_tax_corrections( correction_type, correction_status, correction_statement_period_id, contract_ids, test_limit, test_offset, ) assert result == {'items': [tax_correction], 'total_count': 1} assert mock_repository.get_filtered_worksheet_tax_corrections.call_args_list == [ call( correction_type, correction_status, correction_statement_period_id, contract_ids, test_limit, test_offset, ) ] @patch('payment.logic.worksheet_tax_correction' '.WorksheetTaxCorrection') def test_bulk_create_success( mock_model, mock_statement_periods, mock_accounts, mock_contracts ): """Test bulk_create_method success.""" test_create_params = [ { 'contract_id': 1, 'account_id': 1, 'correction_statement_period_id': 1, 'payable_detail_type_id': 1, 'amount': Decimal('10.00'), 'currency_code': 'USD', 'note': 'test note 1', }, { 'contract_id': 2, 'account_id': 2, 'correction_statement_period_id': 2, 'payable_detail_type_id': 2, 'amount': Decimal('20.00'), 'currency_code': 'GBP', 'note': 'test note 2', }, ] worksheet1 = WorksheetTaxCorrectionFactory.build(**test_create_params[0]) worksheet2 = WorksheetTaxCorrectionFactory.build(**test_create_params[1]) mock_model.side_effect = [worksheet1, worksheet2] instances = logic.bulk_create(test_create_params) assert instances == [worksheet1, worksheet2] assert mock_model.call_args_list == [call(**param) for param in test_create_params] assert mock_model.bulk_create.call_args_list == [call([worksheet1, worksheet2])] @patch('payment.logic.worksheet_tax_correction' '.WorksheetTaxCorrection') def test_bulk_create_failure( mock_model, mock_statement_periods, mock_accounts, mock_contracts ): """Test bulk_create_method failure.""" test_create_params = [ { 'contract_id': 1, 'account_id': 1, 'correction_statement_period_id': 1, 'payable_detail_type_id': 1, 'amount': Decimal('10.00'), 'currency_code': 'USD', 'note': 'test note 1', }, { 'contract_id': 2, 'account_id': 2, 'correction_statement_period_id': 2, 'payable_detail_type_id': 2, 'amount': Decimal('20.00'), 'currency_code': 'GBP', 'note': 'test note 2', }, ] worksheet1 = WorksheetTaxCorrectionFactory.build(**test_create_params[0]) worksheet2 = WorksheetTaxCorrectionFactory.build(**test_create_params[1]) mock_model.side_effect = [worksheet1, worksheet2] mock_model.bulk_create.side_effect = exc.IntegrityError(None, None, None) with pytest.raises(LogicError, match=error.ERROR_INTEGRITY): logic.bulk_create(test_create_params) assert mock_model.call_args_list == [call(**param) for param in test_create_params] assert mock_model.bulk_create.call_args_list == [call([worksheet1, worksheet2])] @patch('payment.logic.worksheet_tax_correction.' 'WorksheetTaxCorrection') def test_bulk_delete(mock_model, faker): """Def test bulk_delete_method.""" ids = faker.pylist(value_types=[int]) logic.delete_worksheet_tax_corrections(ids) assert mock_model.soft_delete_by_ids(ids)