예제 #1
0
    def test_class_balance_method(self, mocker, mocked_monzo,
                                  balance_api_response, accounts_api_response):
        """Test class `balance` method"""
        mocked_get_response = mocker.patch(
            'pymonzo.monzo_api.MonzoAPI._get_response',
        )
        mocked_get_response.return_value.json.return_value = balance_api_response

        accounts_json = accounts_api_response['accounts']
        mocked_monzo._cached_accounts = [
            MonzoAccount(data=account) for account in accounts_json
        ]

        result = mocked_monzo.balance()

        mocked_get_response.assert_called_once_with(
            method='get',
            endpoint='/balance',
            params={
                'account_id': mocked_monzo._cached_accounts[0].id,
            },
        )

        expected_result = MonzoBalance(balance_api_response, context=mocked_monzo)

        assert result == expected_result

        # It should raise an 'ValueError' if there more (or less) then 1 account
        mocked_monzo._cached_accounts = mocked_monzo._cached_accounts * 2

        with pytest.raises(ValueError):
            mocked_monzo.balance()
예제 #2
0
    def balance(self, account_id=None):
        """
        Returns balance information for a specific account.

        Official docs:
            https://monzo.com/docs/#read-balance

        :param account_id: Monzo account ID
        :type account_id: str
        :raises: ValueError
        :returns: Monzo balance instance
        :rtype: MonzoBalance
        """
        if not account_id:
            if len(self.accounts()) == 1:
                account_id = self.accounts()[0].id
            else:
                raise ValueError("You need to pass account ID")

        endpoint = '/balance'
        response = self._get_response(
            method='get',
            endpoint=endpoint,
            params={
                'account_id': account_id,
            },
        )

        return MonzoBalance(data=response.json())