"""Tests for account_tax_info serialization.""" from datetime import date from decimal import Decimal import pytest from marshmallow import ValidationError from abacus_account.constants.constants import ( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, TAX_EMPLOYMENT_TYPES, ) from abacus_account.schemas.account_tax_info import ( AccountTaxInfoDetailSchema, AccountTaxInfoListInputSchema, AccountTaxInfoPutSchema, ) from abacus_account.tests.utils.factories import AccountTaxInfoFactory @pytest.mark.parametrize('country_of_tax_residence', ('USA', None)) def test_account_tax_info_detail_schema(country_of_tax_residence): """Test account_tax_info detail schema serialization.""" account_tax_info = AccountTaxInfoFactory.build( account_id=1, country_of_tax_residence=country_of_tax_residence ) result = AccountTaxInfoDetailSchema().dump(account_tax_info) assert result == { 'account_tax_info_id': account_tax_info.account_tax_info_id, 'account_id': account_tax_info.account_id, 'country_of_tax_residence': account_tax_info.country_of_tax_residence, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, 'tax_employment_type': account_tax_info.tax_employment_type, 'certificate_of_residence_expiration_date': account_tax_info.certificate_of_residence_expiration_date, 'is_wht_applicable': account_tax_info.is_wht_applicable, 'is_resident_of_spanish_islands': account_tax_info.is_resident_of_spanish_islands, 'wht_rate_override': None, } def test_account_tax_info_put_schema(): """Test account_tax_info put schema validation.""" account_tax_info = AccountTaxInfoFactory.build(account_id=1) test_data = { 'country_of_tax_residence': account_tax_info.country_of_tax_residence, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, } result = AccountTaxInfoPutSchema().load(test_data) assert result == test_data def test_account_tax_info_put_schema_no_cotr(): """Test account_tax_info put schema validation.""" account_tax_info = AccountTaxInfoFactory.build(account_id=1) test_data = { 'country_of_tax_residence': None, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, } result = AccountTaxInfoPutSchema().load(test_data) assert result == test_data @pytest.mark.parametrize('tax_employment_type', (*TAX_EMPLOYMENT_TYPES, None)) def test_account_tax_info_put_schema_spanish_taxes(tax_employment_type): """Test account_tax_info put schema validation.""" account_tax_info = AccountTaxInfoFactory.build( account_id=1, tax_employment_type=tax_employment_type ) test_data = { 'country_of_tax_residence': account_tax_info.country_of_tax_residence, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, 'tax_employment_type': account_tax_info.tax_employment_type, 'certificate_of_residence_expiration_date': account_tax_info.certificate_of_residence_expiration_date, 'is_wht_applicable': account_tax_info.is_wht_applicable, 'is_resident_of_spanish_islands': account_tax_info.is_resident_of_spanish_islands, } result = AccountTaxInfoPutSchema().load(test_data) assert result == test_data @pytest.mark.parametrize('tax_employment_type', (*TAX_EMPLOYMENT_TYPES, None)) def test_account_tax_info_put_schema_german_taxes(tax_employment_type): """Test account_tax_info put schema validation.""" account_tax_info = AccountTaxInfoFactory.build( account_id=1, tax_employment_type=tax_employment_type, wht_rate_override=Decimal('10.01'), ) test_data = { 'country_of_tax_residence': account_tax_info.country_of_tax_residence, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, 'tax_employment_type': account_tax_info.tax_employment_type, 'is_wht_applicable': account_tax_info.is_wht_applicable, 'wht_rate_override': account_tax_info.wht_rate_override, } result = AccountTaxInfoPutSchema().load(test_data) assert result == test_data @pytest.mark.parametrize( 'data,expected', [ ( { 'certificate_of_residence_expiration_date_start': '2024-01-01', 'certificate_of_residence_expiration_date_end': '2024-12-31', 'limit': 10, 'offset': 5, 'account_ids': [1, 2, 3], }, { 'certificate_of_residence_expiration_date_start': date(2024, 1, 1), # noqa: E501 'certificate_of_residence_expiration_date_end': date(2024, 12, 31), # noqa: E501 'limit': 10, 'offset': 5, 'account_ids': [1, 2, 3], }, ), ( { 'certificate_of_residence_expiration_date_start': None, 'certificate_of_residence_expiration_date_end': None, 'account_ids': None, }, { 'certificate_of_residence_expiration_date_start': None, 'certificate_of_residence_expiration_date_end': None, 'limit': DEFAULT_PAGE_LIMIT, 'offset': DEFAULT_PAGE_OFFSET, 'account_ids': None, }, ), ], ) def test_account_tax_info_list_input_schema_success(data, expected): """Test AccountTaxInfoListInputSchema.""" schema = AccountTaxInfoListInputSchema() result = schema.load(data) assert result == expected @pytest.mark.parametrize( 'data,field', [ ( { 'certificate_of_residence_expiration_date_start': 'invalid-date', 'certificate_of_residence_expiration_date_end': '2024-12-31', 'account_ids': [1, 2], }, 'certificate_of_residence_expiration_date_start', ), ({'limit': 0, 'offset': 5, 'account_ids': [1]}, 'limit'), ({'limit': 10, 'offset': -1, 'account_ids': [1]}, 'offset'), ({'limit': 10, 'offset': 5, 'account_ids': ['not-an-int']}, 'account_ids'), ( { 'certificate_of_residence_expiration_date_start': '2024-12-31', 'certificate_of_residence_expiration_date_end': '2024-01-01', 'limit': 10, 'offset': 5, 'account_ids': [1, 2], }, '_schema', ), ], ) def test_account_tax_info_list_input_schema_failure(data, field): """Test AccountTaxInfoListInputSchema.""" schema = AccountTaxInfoListInputSchema() with pytest.raises(ValidationError) as exc_info: schema.load(data) assert field in exc_info.value.messages