"""Ledger Accounting Run VAT logic tests.""" from unittest.mock import patch import pytest from ledger.constants.constants import ERROR_INVALID_LIMIT_OFFSET, VAT_CATEGORIES_OLD from ledger.constants.error import ERROR_COUNTRY_CODE_REQUIRED, INVALID_VAT_CATEGORY from ledger.logic import ledger_accounting_run_vat as logic from tests.utils.factories import LedgerAccountingRunVatFactory @patch('ledger.logic.ledger_accounting_run_vat.LedgerAccountingRunVat') def test_get_contracts_count_by_vat_category(mock_model): """Test get_ledger_accounting_run_vat_overview method.""" mock_model.get_ledger_accounting_run_vat_overview.return_value = [] res = logic.get_ledger_accounting_run_vat_overview(1) mock_model.get_ledger_accounting_run_vat_overview.assert_called_once_with(1) assert res.status == 200 @patch('ledger.logic.ledger_accounting_run_vat._validate_request_params') @patch('ledger.logic.ledger_accounting_run_vat.LedgerAccountingRunVat') def test_get_ledger_vat_list_success(mock_model, mock_validation): """Test get_ledger_vat_list method.""" entry = LedgerAccountingRunVatFactory.build() mock_model_response = [entry] vat_category = VAT_CATEGORIES_OLD.VAT_APPLIED country_code = 'GBR' request_params = {'country_code': country_code, 'limit': 1, 'offset': 0} validated_params = { 'vat_category': vat_category, 'country_code': country_code, 'limit': 1, 'offset': 0, } mock_model.get_ledger_vat_list.return_value = mock_model_response mock_model.get_ledger_vat_list_count.return_value = 1 mock_validation.return_value = validated_params res = logic.get_ledger_vat_list(1, vat_category, request_params) assert res.status == 200 mock_model.get_ledger_vat_list.assert_called_once_with(1, **validated_params) mock_model.get_ledger_vat_list_count.assert_called_once_with( 1, VAT_CATEGORIES_OLD.VAT_APPLIED, country_code ) @patch('ledger.logic.ledger_accounting_run_vat.LedgerAccountingRunVat') def test_get_ledger_vat_list_validation_error(mock_model): """Test get_ledger_vat_list method for an invalid request params.""" vat_category = VAT_CATEGORIES_OLD.VAT_APPLIED request_params = {'limit': 1, 'offset': 0} res = logic.get_ledger_vat_list(1, vat_category, request_params) assert res.status == 400 assert res.errors['message'] == ERROR_COUNTRY_CODE_REQUIRED mock_model.get_ledger_vat_list.assert_not_called() mock_model.get_ledger_vat_list_count.assert_not_called() def test_validate_request_params_success(): """Test _validate_request_params when all parameters are valid.""" vat_category = VAT_CATEGORIES_OLD.VAT_APPLIED country_code = 'GBR' request_params = {'country_code': country_code, 'limit': 1, 'offset': 0} expected_response = { 'vat_category': vat_category, 'country_code': country_code, 'limit': 1, 'offset': 0, } res = logic._validate_request_params(vat_category, request_params) assert res == expected_response def test_validate_request_params_invalid(): """Test _validate_request_params for an invalid params.""" with pytest.raises( Exception, match=INVALID_VAT_CATEGORY.format( VAT_CATEGORIES_OLD=(', '.join(VAT_CATEGORIES_OLD)) ), ): vat_category = 'test' request_params = {'country_code': 'GBR', 'limit': 1, 'offset': 0} logic._validate_request_params(vat_category, request_params) with pytest.raises(Exception, match=ERROR_INVALID_LIMIT_OFFSET): vat_category = 'vat_applied' request_params = {'country_code': 'GBR', 'limit': 'test', 'offset': 0} logic._validate_request_params(vat_category, request_params) with pytest.raises(Exception, match=ERROR_COUNTRY_CODE_REQUIRED): vat_category = 'vat_applied' request_params = {'limit': 1, 'offset': 0} logic._validate_request_params(vat_category, request_params) @patch('ledger.logic.ledger_accounting_run_vat.db') @patch('ledger.logic.ledger_accounting_run_vat.validate_record') @patch('ledger.logic.ledger_accounting_run_vat.LedgerAccountingRunVat') def test_bulk_create(mock_model, mock_validation, mock_db): """Test bulk_create method.""" post_body = [ { 'accounting_run_id': 1, 'abacus_event_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'country_of_tax_residence': 'USA', 'exempt_reason': 'US citizenship', } ] mock_validation.return_value = None mock_model.build.return_value = post_body[0] mock_db.session.return_value.commit.return_value = None res = logic.bulk_create(post_body) assert res.status == 201 assert res.message['items'][0]['accounting_run_id'] == 1 assert res.message['total_count'] == 1 mock_validation.assert_called_once_with(post_body[0]) mock_model.build.assert_called_once_with(**post_body[0]) mock_db.session.commit.assert_called_once()