"""PostSpanishTaxDetailsProcessor tests""" from datetime import date from unittest.mock import _Call, call, MagicMock, patch from src.models import ( Address, NewTaxDetails, PostSpanishTaxDetailsInput, UpdateAccountTaxInfo, ) from src.processors.tax_details import PostSpanishTaxDetailsProcessor from tests.utils import create_sample_csv_buffer class TestPostSpanishTaxDetailsProcessor: """PostSpanishTaxDetailsProcessor test suite.""" _headers = ( 'vendor_id', 'business_name', 'first_name', 'last_name', 'is_vat_registered', 'local_tax_id', 'tax_employment_type', 'is_resident_of_spanish_islands', 'address1', 'address2', 'province', 'city', 'zip', 'country_code', 'country_of_tax_residence_code', 'expiration_date', ) def test_load_data_success(self) -> None: """Test loading data from CSV file success.""" buffer = create_sample_csv_buffer( ( tuple(list(self._headers) + ['override']), ( '5', 'business name', 'first name', 'last name', 'TRUE', 'X12345678', 'Employed Individual', 'FALSE', '123 Baldwin Ave.', 'apt. 321', 'NY', 'New york', '10001', 'USA', 'ESP', '12/11/2025', '', ), ( '6', '', 'first name', 'last name', 'FALSE', 'X12345678', '', '', '', '', '', '', '', 'USA', 'ESP', '12/11/2025', 'TRUE', ), ) ) processor = PostSpanishTaxDetailsProcessor('', '') processor._input_file_buffer = buffer processor._load_data() assert processor._data == self._get_mock_data() @patch('src.processors.tax_details.base_post_tax_details.ows_payee') @patch('src.processors.tax_details.base_post_tax_details.ows_abacus_account') def test_post_data_success( self, mock_ows_abacus_account: MagicMock, mock_ows_payee: MagicMock, ) -> None: """Test _post_data""" data = self._get_mock_data(validated=True) processor = PostSpanishTaxDetailsProcessor('', '') processor._data = data # type: ignore processor._post_data() assert ( mock_ows_payee.save_tax_details.call_args_list == self._get_tax_details_calls(data) ) assert ( mock_ows_abacus_account.update_account_tax_info.call_args_list == self._get_tax_info_calls(data) ) assert not processor._logs @staticmethod def _get_mock_data( validated: bool = False, key: int | None = None ) -> dict[int, PostSpanishTaxDetailsInput]: data = { 5: PostSpanishTaxDetailsInput( vendor_id=5, business_name='business name', first_name='first name', last_name='last name', is_vat_registered=True, local_tax_id='X12345678', tax_employment_type='Employed Individual', is_resident_of_spanish_islands=False, address1='123 Baldwin Ave.', address2='apt. 321', province='NY', city='New york', zip='10001', country_code='USA', country_of_tax_residence_code='ESP', expiration_date=date(2025, 12, 11), **( { 'account_payee_id': 1, 'account_tax_info_id': 2, } if validated else {} ), ), 6: PostSpanishTaxDetailsInput( vendor_id=6, first_name='first name', last_name='last name', is_vat_registered=False, local_tax_id='X12345678', country_code='USA', country_of_tax_residence_code='ESP', expiration_date=date(2025, 12, 11), override=True, **( { 'account_payee_id': 15, 'account_tax_info_id': 16, } if validated else {} ), ), } if key: data = {key: data[key]} return data @staticmethod def _get_tax_details_calls( data: dict[int, PostSpanishTaxDetailsInput], ) -> list[_Call]: return [ call( data_item.account_payee_id, NewTaxDetails( local_tax_id=data_item.local_tax_id, is_vat_registered=data_item.is_vat_registered, business_name=data_item.business_name, country_of_tax_residency_code=data_item.country_of_tax_residence_code, address=Address( country=data_item.country_code, address1=data_item.address1, address2=data_item.address2, city=data_item.city, postal_code=data_item.zip, state=data_item.province, ) if data_item.is_vat_registered else None, ), ) for data_item in data.values() ] @staticmethod def _get_tax_info_calls(data: dict[int, PostSpanishTaxDetailsInput]) -> list[_Call]: return [ call( data_item.account_tax_info_id, UpdateAccountTaxInfo( country_of_tax_residence=data_item.country_of_tax_residence_code, is_resident_of_spanish_islands=data_item.is_resident_of_spanish_islands, tax_employment_type=data_item.tax_employment_type, certificate_of_residence_expiration_date=data_item.expiration_date, is_sba_signed=data_item.is_vat_registered, ), ) for data_item in data.values() ]