"""Ledger Account logic tests.""" from datetime import datetime from unittest.mock import patch from ledger.logic import ledger_account as logic from ledger.schemas.ledger_account import LedgerAccountListSchema, LedgerAccountSchema from tests.utils.factories import LedgerAccountFactory @patch('ledger.logic.ledger_account.LedgerAccount.get_ledger_account_info') def test_get_ledger_account_info(mock_model): """Test get_ledger_account_info method.""" mock_model_response = [ { 'ledger_account_id': 1, 'account_id': 123, 'accounting_period_id': 1, 'amount': '320260.99', 'contract_id': 44, 'currency_code': 'USD', 'date': datetime(2020, 5, 17), 'ending_balance': '320260.99', 'opening_balance': '0.00', 'statement_period_id': 265, 'transaction_type': 'accounting_run', } ] mock_response = LedgerAccountListSchema().dump(mock_model_response, many=True) mock_model.return_value = mock_model_response, 1 res = logic.get_ledger_account_info(1) assert res.status == 200 assert res.message == mock_response @patch('ledger.logic.ledger_account.LedgerAccount.get_ledger_account_balance') def test_get_current_balance(mock_model, mock_event_fixtures): """Test get current balance of an account.""" ledger_account_1 = LedgerAccountFactory.create(account_id=50) LedgerAccountFactory.create(account_id=1) mock_response = { 'currency_code': ledger_account_1.currency_code, 'current_balance': ledger_account_1.current_balance, } mock_model.return_value.first.return_value = mock_response res = logic.get_current_balance(1) result = res.message assert result['currency_code'] == ledger_account_1.currency_code assert result['current_balance'] == str(ledger_account_1.current_balance) @patch('ledger.logic.ledger_account.LedgerAccount.get_accounts_by_acc_period_id') @patch( 'ledger.logic.ledger_account.LedgerAccount.get_total_accounts_count_by_acc_period_id' ) def test_get_ledger_accounts_by_acc_period_id( mock_model_accounts_count, mock_model_accounts ): """Test get_ledger_accounts_by_acc_period_id method.""" mock_accounts_response = [ { 'abacus_event_id': 1, 'currency_amount': '1881.35', 'currency_code': 'USD', 'ledger_account_id': 1, 'account_id': '318379', } ] mock_accounts_count_response = 1 mock_model_accounts.return_value = mock_accounts_response mock_model_accounts_count.return_value = mock_accounts_count_response mock_response = LedgerAccountSchema().dump(mock_accounts_response, many=True) request_params = {'limit': 2, 'offset': 0} result = logic.get_ledger_accounts_by_acc_period_id(1, request_params) assert result.status == 200 assert result.message['items'] == mock_response