"""Tests for account_payment_term_template logic.""" from unittest.mock import patch from marshmallow import ValidationError import pytest from abacus_account.constants.error import ERROR_PAYMENT_TERM_TEMPLATE_DOES_NOT_EXISTS from abacus_account.constants.error import ERROR_UNKNOWN_CURRENCY from abacus_account.logic import account_payment_term_template as logic from tests.utils.factories import AccountFactory from tests.utils.factories import AccountPaymentTermTemplateFactory @patch('abacus_account.logic.account_payment_term_template.Account') @patch('abacus_account.logic.account_payment_term_template.AccountPaymentTermTemplate') @patch('abacus_account.logic.account_payment_term_template.create_account_payment_term') def test_create_payment_terms_from_template_success( create_payment_terms_mock, mock_template_model, mock_account_model ): """Test create_payment_terms_from_template method.""" account = AccountFactory.create() template = AccountPaymentTermTemplateFactory.create() account_id = account.account_id template_id = template.account_payment_term_template_id expected_result = { 'currency_code': 'USD', 'payment_minimum': '555.55' } mock_account_model.get_by_id_or_error.return_value = account mock_template_model.get_by_id_or_error.return_value = template create_payment_terms_mock.return_value = expected_result result = logic.create_payment_terms_from_template(template_id, account_id) assert result == expected_result mock_account_model.get_by_id_or_error.assert_called_once_with(account_id) mock_template_model.get_by_id_or_error.assert_called_once_with(template_id) payment_term_params = { 'account_id': account_id, 'currency_code': template.currency_code } payment_term_params.update(template.payment_terms) create_payment_terms_mock.assert_called_once_with(**payment_term_params) @patch('abacus_account.logic.account_payment_term_template.AccountPaymentTermTemplate') def test_get_payment_term_template_for_valid_currency_code(mock_template_model): """Test to get account_payment_term_template for valid currency code.""" currency_code = 'AUD' template = AccountPaymentTermTemplateFactory.create(currency_code=currency_code) mock_template_model.get_by_currency_code.return_value = template result = logic.get_payment_term_template_by_currency_code(currency_code) assert result == template @patch('abacus_account.logic.account_payment_term_template.AccountPaymentTermTemplate') def test_get_payment_term_template_for_invalid_currency_code(mock_template_model): """Test to get account_payment_term_template for invalid currency code.""" currency_code = 'test' mock_template_model.get_by_currency_code.return_value = None with pytest.raises( ValidationError, match=ERROR_UNKNOWN_CURRENCY.format(code=currency_code) ): logic.get_payment_term_template_by_currency_code(currency_code) @patch('abacus_account.logic.account_payment_term_template.AccountPaymentTermTemplate') def test_get_payment_term_template_for_invalid_template(mock_template_model): """Test to get account_payment_term_template that doesn't present in table.""" currency_code = 'AOA' mock_template_model.get_by_currency_code.return_value = None with pytest.raises( ValidationError, match=ERROR_PAYMENT_TERM_TEMPLATE_DOES_NOT_EXISTS.format( currency_code=currency_code ) ): logic.get_payment_term_template_by_currency_code(currency_code) @patch('abacus_account.logic.account_payment_term_template.Account') @patch('abacus_account.logic.account_payment_term_template.AccountPaymentTermTemplate') @patch('abacus_account.logic.account_payment_term_template.create_account_payment_term') def test_create_payment_term_by_currency_code_success( create_payment_terms_mock, mock_template_model, mock_account_model ): """Test create_payment_term_from_template_by_currency_code method.""" account = AccountFactory.create() template = AccountPaymentTermTemplateFactory.create() account_id = account.account_id currency_code = template.currency_code expected_result = { 'currency_code': 'USD', 'payment_minimum': '555.55' } mock_account_model.get_by_id_or_error.return_value = account mock_template_model.get_by_currency_code.return_value = template create_payment_terms_mock.return_value = expected_result result = logic.create_payment_term_from_template_by_currency_code( currency_code, account_id) assert result == expected_result mock_account_model.get_by_id_or_error.assert_called_once_with(account_id) mock_template_model.get_by_currency_code.assert_called_once_with(currency_code) payment_term_params = { 'account_id': account_id, 'currency_code': template.currency_code } payment_term_params.update(template.payment_terms) create_payment_terms_mock.assert_called_once_with(**payment_term_params)