"""Statement Reserves logic tests.""" from decimal import Decimal from unittest.mock import patch from moneyhub.logic import statement_reserves as logic from moneyhub.schemas.account_statements import AccountStatementsReservesSchema from tests.utils.factories import LedgerAccountContractFactory @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves(mock_models): """Test get_statement_reserves method with period id params.""" account_id = 123 contract_id = 34784 statement_period_id = 284 visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods released1 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('100.00')) released2 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('200.00')) taken1 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('-50.00')) taken2 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('-20.00')) mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [released1, released2] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [taken1, taken2] res = logic.get_statement_reserves( account_id, contract_id, statement_period_id) assert res == { 'ledger_reserve_release_total': Decimal('300.00'), 'ledger_reserve_taken_total': Decimal('-70.00'), 'ledger_reserve_total': Decimal('230.00') } mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.\ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_only_taken(mock_models): """Test get_statement_reserves method with period id when only taken present.""" account_id = 123 contract_id = 34784 statement_period_id = 284 visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods taken1 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('-50.00')) taken2 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('-20.00')) mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [taken1, taken2] res = logic.get_statement_reserves( account_id, contract_id, statement_period_id) assert res == { 'ledger_reserve_taken_total': Decimal('-70.00'), 'ledger_reserve_total': Decimal('-70.00') } mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_only_released(mock_models): """Test get_statement_reserves method with period id when only released present.""" account_id = 123 contract_id = 34784 statement_period_id = 284 visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods released1 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('100.00')) released2 = LedgerAccountContractFactory.build( account_id=account_id, contract_id=contract_id, currency_amount=Decimal('200.00')) mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [released1, released2] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [] res = logic.get_statement_reserves( account_id, contract_id, statement_period_id) assert res == { 'ledger_reserve_release_total': Decimal('300.00'), 'ledger_reserve_total': Decimal('300.00') } mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_empty(mock_models): """Test get_statement_reserves method with period id params when none present.""" account_id = 123 contract_id = 34784 statement_period_id = 284 visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [] res = logic.get_statement_reserves( account_id, contract_id, statement_period_id) assert res == {} mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, [statement_period_id]) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_statement_period_not_visible(mock_models): """Test get_statement_reserves method when statement periods is not visible.""" account_id = 123 contract_id = 34784 statement_period_id = 284 visible_statement_periods = [285, 286] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods res = logic.get_statement_reserves( account_id, contract_id, statement_period_id) assert res is None mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_by_statement_periods(mock_models): """Test get_statement_reserves_by_statement_periods method with period id params.""" account_id = 123 contract_id = 34784 statement_period_ids = [284] visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods released1 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('100.00'), 'statement_period_id': statement_period_ids[0]} released2 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('200.00'), 'statement_period_id': statement_period_ids[0]} taken1 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('-50.00'), 'statement_period_id': statement_period_ids[0]} taken2 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('-20.00'), 'statement_period_id': statement_period_ids[0]} mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [released1, released2] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [taken1, taken2] res = logic.get_statement_reserves_by_statement_periods( account_id, contract_id, statement_period_ids) assert res == [AccountStatementsReservesSchema( ledger_reserve_release_total=Decimal('300.00'), ledger_reserve_taken_total=Decimal('-70.00'), ledger_reserve_total=Decimal('230.00'), account_id=account_id, contract_id=contract_id, statement_period_id=statement_period_ids[0] )] mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.\ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_by_statement_periods_only_taken( mock_models ): """Test get_statement_reserves_by_statement_periods method when only taken present.""" account_id = 123 contract_id = 34784 statement_period_ids = [284] visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods taken1 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('-50.00'), 'statement_period_id': statement_period_ids[0]} taken2 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('-20.00'), 'statement_period_id': statement_period_ids[0]} mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [taken1, taken2] res = logic.get_statement_reserves_by_statement_periods( account_id, contract_id, statement_period_ids) assert res == [AccountStatementsReservesSchema( ledger_reserve_taken_total=Decimal('-70.00'), ledger_reserve_total=Decimal('-70.00'), account_id=account_id, contract_id=contract_id, statement_period_id=statement_period_ids[0] )] mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_by_statement_periods_only_released( mock_models ): """Test get_statement_reserves_by_statement_periods method when only released present.""" account_id = 123 contract_id = 34784 statement_period_ids = [284] visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods released1 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('100.00'), 'statement_period_id': statement_period_ids[0]} released2 = { 'account_id': account_id, 'contract_id': contract_id, 'currency_amount': Decimal('200.00'), 'statement_period_id': statement_period_ids[0]} mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [released1, released2] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [] res = logic.get_statement_reserves_by_statement_periods( account_id, contract_id, statement_period_ids) assert res == [AccountStatementsReservesSchema( ledger_reserve_release_total=Decimal('300.00'), ledger_reserve_total=Decimal('300.00'), account_id=account_id, contract_id=contract_id, statement_period_id=statement_period_ids[0] )] mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_by_statement_periods_empty(mock_models): """Test get_statement_reserves_by_statement_periods method when none present.""" account_id = 123 contract_id = 34784 statement_period_ids = [284] visible_statement_periods = [284, 285] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods mock_models.LedgerAccountContract.\ get_reserves_released_by_account_and_statement_periods.return_value = [] mock_models.LedgerAccountContract.\ get_reserves_taken_by_account_and_statement_periods.return_value = [] res = logic.get_statement_reserves_by_statement_periods( account_id, contract_id, statement_period_ids) assert res == [] mock_models.LedgerAccountContract.get_reserves_released_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.LedgerAccountContract.get_reserves_taken_by_account_and_statement_periods.\ assert_called_once_with(account_id, contract_id, statement_period_ids) mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id) @patch('moneyhub.logic.statement_reserves.models') def test_get_statement_reserves_by_statement_periods_statement_period_not_visible( mock_models): """Test get_statement_reserves_by_statement_periods method when periods is not visible.""" account_id = 123 contract_id = 34784 statement_period_ids = [284] visible_statement_periods = [285, 286] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids.return_value = \ visible_statement_periods res = logic.get_statement_reserves_by_statement_periods( account_id, contract_id, statement_period_ids) assert res == [] mock_models.StatementPeriodPaymentEntity.get_visible_statement_period_ids. \ assert_called_with(account_id)