"""Unit tests for account_contract_vat_detail.""" from unittest.mock import call, patch import pytest from abacus_account import models from abacus_account.logic import account_contract_vat_detail as logic @patch('abacus_account.models.AccountContractVatDetail.' 'build') @patch('abacus_account.models.AccountContractVatDetail.' 'update_attributes') @patch('abacus_account.models.AccountContractVatDetail.' 'get_by_account_id_contract_id') def test_create_or_update_account_contract_vat_detail_success( mock_model, mock_update, mock_build, reference_signing_entity_fixture, account_contract_vat_detail_fixtures): """Test to create or update account contract vat details.""" post_body = [ { 'account_id': 1, 'contract_id': 1, 'is_vat_registered': True, 'is_vat_eligible': True, 'vat_country': 'Germany', 'company_code': 'company_code', }, { 'account_id': 2, 'contract_id': 2, 'is_vat_registered': False, 'is_vat_eligible': False, 'vat_country': 'Slovenia', 'company_code': 'company_code2', } ] mock_model.side_effect = [ models.AccountContractVatDetail(**post_body[0]), None ] mock_build.side_effect = [models.AccountContractVatDetail(**post_body[1])] res = logic.create_or_update_account_contract_vat_detail(post_body) assert res.status == 201 assert len(res.message) == 2 del res.message[0]['account_contract_vat_detail_id'] del res.message[1]['account_contract_vat_detail_id'] assert res.message == post_body mock_model.call_count = 2 mock_model.assert_has_calls([call(1, 1), call(2, 2)]) mock_update.assert_called_once() mock_build.assert_called_once() @patch('abacus_account.models.AccountContractVatDetail.' 'get_by_account_id_contract_id') def test_create_or_update_account_contract_vat_detail_failed( mock_model, reference_signing_entity_fixture, account_contract_vat_detail_fixtures ): """Test to create or update account contract vat details.""" post_body = [ { 'account_id': 1, 'contract_id': 1, 'is_vat_registered': True, 'is_vat_eligible': True, 'vat_country': 'Germany', 'company_code': 'company_code', }, { 'account_id': 2, 'contract_id': 2, 'is_vat_registered': False, 'is_vat_eligible': False, 'vat_country': 'Slovenia', 'company_code': 'company_code2', } ] mock_model.side_effect = [ models.AccountContractVatDetail(**post_body[0]), Exception('Something went wrong!') ] with pytest.raises(Exception) as excinfo: logic.create_or_update_account_contract_vat_detail(post_body) assert 'Something went wrong!' == str(excinfo.value) mock_model.call_count = 2 mock_model.assert_has_calls([call(1, 1), call(2, 2)])