"""Tests for account model.""" from decimal import Decimal import pytest from abacus_common_logic.connectors.database import db from abacus_account.constants.constants import BULK_PAYOUTS_PAYONEER_PAYMENT_TYPES from abacus_account.models.account import Account from abacus_account.models.account_payee import AccountPayee from abacus_account.tests.utils.factories import ( AccountFactory, AccountPayeeFactory, AccountPaymentTermFactory, ReferencePaymentTypeFactory, ) def test_get_account_by_id(): """Test getting an account by account id.""" account = AccountFactory.create() account_info = Account.get_by_id(account.account_id) assert account_info.account_name == account.account_name assert account_info.account_id == account.account_id assert account_info.created_by == account.created_by assert account_info.sap_created_at == account.sap_created_at def test_get_eligible_for_payment(account_payment_eligible): """Get accounts eligible for payment.""" accounts = Account.get_eligible_for_payment(123) assert len(accounts) == 1 accounts_2 = Account.get_eligible_for_payment(1) assert len(accounts_2) == 0 @pytest.mark.parametrize( 'reference_payment_type_id', BULK_PAYOUTS_PAYONEER_PAYMENT_TYPES ) def test_get_eligible_for_payment_via_closing_balance( account_payment_eligible_via_closing_balance, reference_payment_type_id ): """Get accounts eligible for payment via closing balance.""" [ ReferencePaymentTypeFactory.create(reference_payment_type_id=payment_type_id) for payment_type_id in [8, 11, 12] ] db.session.query(AccountPayee).update( {AccountPayee.reference_payment_type_id: reference_payment_type_id} ) db.session.commit() accounts = Account.get_eligible_for_payment_via_closing_balance(123) assert len(accounts) == 1 def test_get_eligible_for_payment_when_ff_is_enabled(account_payment_eligible): """Get accounts eligible for payment.""" accounts = Account.get_eligible_for_payment(123) assert len(accounts) == 1 accounts_2 = Account.get_eligible_for_payment(1) assert len(accounts_2) == 0 def test_get_eligible_for_payment_multiple_contracts( account_payment_multiple_contract_balances, ): """Get accounts eligible for payment with multiple contract balances.""" accounts = Account.get_eligible_for_payment(123) assert len(accounts) == 1 assert accounts[0]['current_balance'] == Decimal('425.86') assert ( accounts[0]['contracts_payable'] == '[{"contract_id": 111, "currency_code": "GBP", "current_balance": 100.00}, {"contract_id": 333, "currency_code": "GBP", "current_balance": 325.86}]' ) # noqa: E501 def test_get_eligible_for_payment_multiple_contracts_via_closing_balance( account_payment_multiple_contract_balances_via_closing_balance, ): """Get accounts eligible for payment with multiple contract balances.""" accounts = Account.get_eligible_for_payment_via_closing_balance(123) assert len(accounts) == 1 assert accounts[0]['contracts'] == '[111, 222, 333]' def test_get_filtered_query(reference_payment_entity_fixture): """Get accounts by request parameters.""" test_account = AccountFactory.create(account_name='Test Account') AccountFactory.create(account_name='Account Test') accounts = Account.get_filtered_query(account_name='account').all() assert len(accounts) == 2 accounts = Account.get_filtered_query(account_name='test account').all() assert len(accounts) == 1 assert accounts[0].account_name == 'Test Account' # A account name having '\' character AccountFactory.create(account_name='A1 LaFlare\\Amigo Records, LLC') accounts = Account.get_filtered_query(account_name='A1 LaFlare\\Amigo').all() assert len(accounts) == 1 assert accounts[0].account_name == 'A1 LaFlare\\Amigo Records, LLC' # A account name having '%' character AccountFactory.create(account_name='Dylan Bukov 100% t/a Dybbukk') accounts = Account.get_filtered_query(account_name='%').all() assert len(accounts) == 1 assert accounts[0].account_name == 'Dylan Bukov 100% t/a Dybbukk' assert not Account.get_filtered_query(payment_entity_id=5).all() AccountPaymentTermFactory.create(account=test_account, payment_entity_id=5) accounts = Account.get_filtered_query(payment_entity_id=5) assert accounts.count() == 1 assert accounts[0].account_name == test_account.account_name assert not Account.get_filtered_query(reference_payment_type_id=7).all() AccountPayeeFactory.create(account=test_account, reference_payment_type_id=7) accounts = Account.get_filtered_query(reference_payment_type_id=7) assert accounts.count() == 1 assert accounts[0].account_name == test_account.account_name def test_get_filtered_query_with_term_field(): """Get accounts by search term.""" AccountFactory.create(account_name='Test Account', account_id=78697) AccountFactory.create(account_name='2023 Music', account_id=97182) # Get accounts that contain the text "account" accounts = Account.get_filtered_query(search_term='Music').all() assert len(accounts) == 1 assert accounts[0].account_name == '2023 Music' # Get "Test Account" accounts = Account.get_filtered_query(search_term='test account').all() assert len(accounts) == 1 assert accounts[0].account_name == 'Test Account' # Get accounts by account_id accounts = Account.get_filtered_query(search_term='97').all() assert len(accounts) == 2 assert [account.account_id for account in accounts] == [97182, 78697] # Get accounts with a '\' character in the account name AccountFactory.create(account_name='A1 LaFlare\\Amigo Records, LLC') accounts = Account.get_filtered_query(search_term='A1 LaFlare\\Amigo').all() assert len(accounts) == 1 assert accounts[0].account_name == 'A1 LaFlare\\Amigo Records, LLC' # Get accounts with a '%' character in the account name AccountFactory.create(account_name='Dylan Bukov 100% t/a Dybbukk') accounts = Account.get_filtered_query(search_term='%').all() assert len(accounts) == 1 assert accounts[0].account_name == 'Dylan Bukov 100% t/a Dybbukk'