"""Test cases for worksheet_account_contract_closing_balance model.""" from abacus_common_logic.connectors.database import db from payment.models.worksheet_account_contract_closing_balance import ( WorksheetAccountContractClosingBalance, ) from tests.utils.factories import ( WorksheetAccountContractClosingBalanceFactory, ) def test_create( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Create a closing balance worksheet instance.""" contract_id = 1 account_id = 1 abacus_event_id = 1 statement_period_id = 1 ledger_account_contract_id = 1 reference_payment_entity_id = 1 currency_code = 'USD' amount = 100 created_item = WorksheetAccountContractClosingBalance.create( contract_id=contract_id, account_id=account_id, abacus_event_id=abacus_event_id, statement_period_id=statement_period_id, ledger_account_contract_id=ledger_account_contract_id, reference_payment_entity_id=reference_payment_entity_id, currency_code=currency_code, amount=amount, ) found_item = WorksheetAccountContractClosingBalance.get_by_id( created_item.worksheet_account_contract_closing_balance_id ) assert found_item == created_item assert found_item.contract_id == contract_id assert found_item.account_id == account_id assert found_item.abacus_event_id == abacus_event_id assert found_item.statement_period_id == statement_period_id assert found_item.ledger_account_contract_id == ledger_account_contract_id assert found_item.reference_payment_entity_id == reference_payment_entity_id assert not found_item.deleted_at assert not found_item.deleted_by assert found_item.created_at assert found_item.last_modified assert found_item.last_modified_by def test_filter_active( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Filter active worksheet instances.""" contract_id = 1 account_id = 1 abacus_event_id = 1 statement_period_id = 1 ledger_account_contract_id = 1 reference_payment_entity_id = 1 currency_code = 'USD' amount = 100 created_item = WorksheetAccountContractClosingBalance.create( contract_id=contract_id, account_id=account_id, abacus_event_id=abacus_event_id, statement_period_id=statement_period_id, ledger_account_contract_id=ledger_account_contract_id, reference_payment_entity_id=reference_payment_entity_id, currency_code=currency_code, amount=amount, ) found_item = ( db.session.execute(WorksheetAccountContractClosingBalance.filter_active()) .scalars() .first() ) assert found_item == created_item assert found_item.contract_id == contract_id assert found_item.account_id == account_id assert found_item.abacus_event_id == abacus_event_id assert found_item.statement_period_id == statement_period_id assert found_item.ledger_account_contract_id == ledger_account_contract_id assert found_item.reference_payment_entity_id == reference_payment_entity_id assert not found_item.deleted_at assert not found_item.deleted_by assert found_item.created_at assert found_item.created_by assert found_item.last_modified assert found_item.last_modified_by def test_soft_delete_by_event_id( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Soft delete worksheet instances by event id.""" contract_id = 1 account_id = 1 abacus_event_id = 1 statement_period_id = 1 ledger_account_contract_id = 1 reference_payment_entity_id = 1 currency_code = 'USD' amount = 100 created_item = WorksheetAccountContractClosingBalance.create( contract_id=contract_id, account_id=account_id, abacus_event_id=abacus_event_id, statement_period_id=statement_period_id, ledger_account_contract_id=ledger_account_contract_id, reference_payment_entity_id=reference_payment_entity_id, currency_code=currency_code, amount=amount, ) WorksheetAccountContractClosingBalance.soft_delete_by_event_id(abacus_event_id) found_item = WorksheetAccountContractClosingBalance.get_by_id( created_item.worksheet_account_contract_closing_balance_id ) assert found_item.deleted_at assert found_item.deleted_by assert found_item.last_modified == found_item.deleted_at assert found_item.last_modified_by == found_item.deleted_by assert found_item.deleted_at >= found_item.created_at assert found_item.deleted_by == 'default_user_id' def test_get_by_statement_period_id( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Get contracts by statement period id.""" statement_period_id = 1 created_item = WorksheetAccountContractClosingBalance.create( contract_id=1, account_id=1, abacus_event_id=3, statement_period_id=statement_period_id, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=100, ) created_item2 = WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=2, abacus_event_id=2, statement_period_id=statement_period_id, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='EUR', amount=200, ) created_item3 = WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=3, abacus_event_id=3, statement_period_id=3, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=300, ) found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=1, ) ) assert len(found_items) == 2 assert total_count == 2 assert created_item in found_items assert created_item2 in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id assert found_items[1].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa limit=1, offset=1, statement_period_id=1, ) ) assert len(found_items) == 1 assert total_count == 2 assert created_item2 in found_items assert created_item not in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa limit=1, offset=2, statement_period_id=1, ) ) assert len(found_items) == 0 assert total_count == 2 found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa limit=10, statement_period_id=3, ) ) assert len(found_items) == 1 assert total_count == 1 assert created_item3 in found_items assert created_item not in found_items assert created_item2 not in found_items assert found_items[0].statement_period_id == 3 found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=statement_period_id, account_ids=[1], ) ) assert len(found_items) == 1 assert total_count == 1 assert created_item in found_items assert created_item2 not in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=statement_period_id, account_ids=[1, 2], ) ) assert len(found_items) == 2 assert total_count == 2 assert created_item in found_items assert created_item2 in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id assert found_items[1].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=statement_period_id, account_ids=[1, 2], limit=1, offset=1, ) ) assert len(found_items) == 1 assert total_count == 2 assert created_item2 in found_items assert created_item not in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=statement_period_id, contract_ids=[1, 2], ) ) assert len(found_items) == 2 assert total_count == 2 assert created_item in found_items assert created_item2 in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id assert found_items[1].statement_period_id == statement_period_id found_items, total_count = ( WorksheetAccountContractClosingBalance.get_by_statement_period_id( # noqa statement_period_id=statement_period_id, contract_ids=[1, 2], limit=1, offset=1, ) ) assert len(found_items) == 1 assert total_count == 2 assert created_item2 in found_items assert created_item not in found_items assert created_item3 not in found_items assert found_items[0].statement_period_id == statement_period_id def test_get_by_ids( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Get closing balances by list of primary key IDs.""" item1 = WorksheetAccountContractClosingBalance.create( contract_id=1, account_id=1, abacus_event_id=3, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=100, ) item2 = WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=2, abacus_event_id=3, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='EUR', amount=200, ) item3_deleted = WorksheetAccountContractClosingBalance.create( contract_id=3, account_id=3, abacus_event_id=3, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='GBP', amount=300, deleted_at='2021-10-01', ) found, total_count = WorksheetAccountContractClosingBalance.get_by_ids( worksheet_closing_balance_ids=[ item1.worksheet_account_contract_closing_balance_id, item2.worksheet_account_contract_closing_balance_id, item3_deleted.worksheet_account_contract_closing_balance_id, 999, ] ) assert total_count == 2 assert len(found) == 2 assert item1 in found assert item2 in found assert item3_deleted not in found def test_get_by_ids_empty( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Returns empty list when no IDs match.""" found, total_count = WorksheetAccountContractClosingBalance.get_by_ids( worksheet_closing_balance_ids=[999, 1000] ) assert found == [] assert total_count == 0 def test_get_by_ids_no_filter_returns_all_active( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Returns all active records when worksheet_closing_balance_ids is not provided.""" WorksheetAccountContractClosingBalance.create( contract_id=1, account_id=1, abacus_event_id=3, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=100, ) WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=2, abacus_event_id=3, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='EUR', amount=200, deleted_at='2021-10-01', ) found, total_count = WorksheetAccountContractClosingBalance.get_by_ids() assert total_count == 1 assert len(found) == 1 def test_get_by_abacus_event_id( mock_contracts, mock_accounts, mock_abacus_event, mock_statement_periods, mock_ledger_account_contracts, ): """Get contracts by abacus event id.""" abacus_event_id = 3 created_item = WorksheetAccountContractClosingBalance.create( contract_id=1, account_id=1, abacus_event_id=abacus_event_id, statement_period_id=1, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=100, ) WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=2, abacus_event_id=2, statement_period_id=2, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='EUR', amount=200, ) WorksheetAccountContractClosingBalance.create( contract_id=2, account_id=3, abacus_event_id=abacus_event_id, statement_period_id=3, ledger_account_contract_id=1, reference_payment_entity_id=1, currency_code='USD', amount=300, deleted_at='2021-10-01', ) found_items = WorksheetAccountContractClosingBalance.get_by_abacus_event_id( # noqa abacus_event_id=abacus_event_id, ) assert len(found_items) == 1 assert created_item in found_items assert found_items[0].abacus_event_id == abacus_event_id