"""Tests for account serialization.""" import datetime from abacus_common_logic.utils.dates import safe_format_datetime from abacus_account.constants.constants import ( SKIP_PAYMENT_TERM_TEMPLATE_CREATION_SOURCES, ) from abacus_account.schemas.account import ( AccountDetailSchema, AccountPostSchema, AccountPutSchema, EligibleAccountListSchema, SAPFormattedAccountDetailSchema, ) from abacus_account.tests.utils.factories import ( AccountFactory, AccountPayeeFactory, AccountPaymentTermFactory, ) def test_account_detail_schema(reference_payment_entity_fixture): """Test account detail serialization.""" account = AccountFactory.create() AccountPayeeFactory.create(account=account) AccountPaymentTermFactory.create(account=account) result = AccountDetailSchema().dump(account) assert result == { 'account_name': account.account_name, 'account_id': account.account_id, 'created_by': account.created_by, 'account_payee_id': account.account_payee.account_payee_id, 'account_payment_term_id': account.account_payment_term.account_payment_term_id, 'sap_created_at': None, } def test_account_post_schema_required_only(): """Test account post serialization with required fields only.""" post_body = {'account_id': 123, 'account_name': 'A Count'} result = AccountPostSchema().dump(post_body) assert result == post_body def test_account_post_schema_optional(): """Test account post serialization with optional fields.""" post_body = { 'account_id': 123, 'account_name': 'A Count', 'currency_code': 'USD', 'country_of_tax_residence': 'USA', 'creation_source': SKIP_PAYMENT_TERM_TEMPLATE_CREATION_SOURCES[0], } result = AccountPostSchema().dump(post_body) assert result == post_body def test_account_put_schema(): """Test account PUT serialization.""" sap_created_at_datetime = datetime.date(2022, 2, 1) put_body = { 'account_name': 'Test Account', 'sap_created_at': sap_created_at_datetime, } result = AccountPutSchema().dump(put_body) assert result == { 'account_name': 'Test Account', 'sap_created_at': safe_format_datetime(sap_created_at_datetime), } def test_eligible_account_list_schema(): """Test eligible accounts list serialization.""" eligible_accounts = [ { 'account_id': 1, 'account_name': 'Account 1', 'country_of_tax_residence': 'USA', 'currency_code': 'GBP', 'contracts_payable': '[{"contract_id": 535575, "currency_code": "USD", "current_balance": 7.07}, {"contract_id": 535988, "currency_code": "USD", "current_balance": 141.4}]', # noqa: E501 'current_balance': '5000.00', 'eligibility_status': 'active', 'payment_minimum': '35.00', 'payment_schedule': '30_days_after_month_end', 'payment_entity_id': 2, } ] result = EligibleAccountListSchema(many=True).dump(eligible_accounts) assert len(result) == 1 assert result[0]['account_id'] == eligible_accounts[0]['account_id'] assert result[0]['account_name'] == eligible_accounts[0]['account_name'] assert ( result[0]['country_of_tax_residence'] == eligible_accounts[0]['country_of_tax_residence'] ) assert result[0]['currency_code'] == eligible_accounts[0]['currency_code'] assert result[0]['current_balance'] == eligible_accounts[0]['current_balance'] assert result[0]['eligibility_status'] == eligible_accounts[0]['eligibility_status'] assert result[0]['payment_minimum'] == eligible_accounts[0]['payment_minimum'] assert result[0]['payment_schedule'] == eligible_accounts[0]['payment_schedule'] assert result[0]['payment_entity_id'] == eligible_accounts[0]['payment_entity_id'] assert { 'contract_id': 535575, 'currency_code': 'USD', 'current_balance': '7.07', } in result[0]['contracts_payable'] # noqa: E501 assert { 'contract_id': 535988, 'currency_code': 'USD', 'current_balance': '141.4', } in result[0]['contracts_payable'] # noqa: E501 def test_eligible_account_list_schema_via_closing_balance(): """Test eligible accounts list serialization.""" eligible_accounts = [ { 'account_id': 1, 'account_name': 'Account 1', 'country_of_tax_residence': 'USA', 'currency_code': 'GBP', 'contracts': '[1, 2, 3]', 'payment_minimum': '35.00', 'payment_schedule': '30_days_after_month_end', 'payment_entity_id': 2, } ] result = EligibleAccountListSchema(many=True).dump(eligible_accounts) assert len(result) == 1 assert result[0]['account_id'] == eligible_accounts[0]['account_id'] assert result[0]['account_name'] == eligible_accounts[0]['account_name'] assert ( result[0]['country_of_tax_residence'] == eligible_accounts[0]['country_of_tax_residence'] ) assert result[0]['currency_code'] == eligible_accounts[0]['currency_code'] assert result[0]['payment_minimum'] == eligible_accounts[0]['payment_minimum'] assert result[0]['payment_schedule'] == eligible_accounts[0]['payment_schedule'] assert result[0]['payment_entity_id'] == eligible_accounts[0]['payment_entity_id'] assert result[0]['contracts'] == [1, 2, 3] assert 'eligibility_status' not in result[0] assert 'contracts_payable' not in result[0] assert 'current_balance' not in result[0] def test_sap_formatted_account_detail_schema(): """Test sap formatted account detail serialization.""" account = AccountFactory.create() result = SAPFormattedAccountDetailSchema().dump(account) assert result == { 'AcctName': account.account_name, 'AccountId': str(account.account_id), 'Kunnr': None, 'Lifnr': None, 'Zzfield1': None, 'Zzfield2': None, } def test_contract_name_sap_formatted_schema(): """Test contract schema for SAP api with long account_name.""" account = AccountFactory.create() account.account_name = 'Oliver Bock t/a rights controlled by Art Bleek & Mercy Collazo; Administrated by our company: Aescom plus a few more chacters' # noqa: E501 result = SAPFormattedAccountDetailSchema().dump(account) assert result == { 'AcctName': account.account_name[:100], 'AccountId': str(account.account_id), 'Kunnr': None, 'Lifnr': None, 'Zzfield1': None, 'Zzfield2': None, }