"""Unit testcases for ledger_contract_flowthrough model.""" from ledger.models.ledger_contract_flowthrough import LedgerContractFlowthrough from tests.conftest import mock_account from tests.utils.factories import LedgerContractFlowthroughFactory def test_create_ledger_contract_flowthrough(mock_event_fixtures): """Create a ledger_contract_flowthrough_current_balance record on ledger_contract_flowthrough insertion.""" account_id = 2 mock_account(account_id) contract_id = 3 contract_flowthrough = LedgerContractFlowthrough.create( account_id=account_id, contract_id=contract_id, abacus_event_id=1, currency_code='EUR', currency_amount=78.0, previous_balance=0, current_balance=78.0, note='opening balance', ) assert contract_flowthrough.account_id == account_id assert contract_flowthrough.contract_id == contract_id assert contract_flowthrough.abacus_event_id == 1 assert contract_flowthrough.currency_code == 'EUR' def test_get_ledger_contract_flowthrough_balances_by_contracts(mock_event_fixtures): """Test to get most recent current balance by contract_ids.""" account_id = 2 mock_account(account_id) contract_id = 3 LedgerContractFlowthroughFactory.create( account_id=account_id, contract_id=1, abacus_event_id=1, currency_code='EUR', currency_amount=78.0, previous_balance=0, current_balance=78.0, note='opening balance', ) LedgerContractFlowthroughFactory.create( account_id=account_id, contract_id=contract_id, abacus_event_id=1, currency_code='USD', currency_amount=100.0, previous_balance=0, current_balance=100.0, note='opening balance', ) contract_2_balance = LedgerContractFlowthroughFactory.create( account_id=account_id, contract_id=2, abacus_event_id=2, currency_code='USD', currency_amount=63.4, previous_balance=78.0, current_balance=141.4, note='credit ledger item', ) contract_3_balance = LedgerContractFlowthroughFactory.create( account_id=account_id, contract_id=contract_id, abacus_event_id=2, currency_code='USD', currency_amount=3230, previous_balance=100.0, current_balance=3330.0, note='credit ledger item', ) contract_flowthrough_leders = ( LedgerContractFlowthrough.get_ledger_contract_flowthrough_balance_by_contracts( [contract_id, contract_2_balance.contract_id] ).all() ) assert ( contract_flowthrough_leders[0].ledger_contract_flowthrough_id == contract_2_balance.ledger_contract_flowthrough_id ) assert ( contract_flowthrough_leders[0].current_balance == contract_2_balance.current_balance ) assert ( contract_flowthrough_leders[1].ledger_contract_flowthrough_id == contract_3_balance.ledger_contract_flowthrough_id ) assert ( contract_flowthrough_leders[1].current_balance == contract_3_balance.current_balance ) assert len(contract_flowthrough_leders) == 2