def transaction(self, transaction_id, expand_merchant=False): """ Returns an individual transaction, fetched by its id. Official docs: https://monzo.com/docs/#retrieve-transaction :param transaction_id: Monzo transaction ID :type transaction_id: str :param expand_merchant: whether merchant data should be included :type expand_merchant: bool :returns: Monzo transaction details :rtype: MonzoTransaction """ endpoint = '/transactions/{}'.format(transaction_id) data = dict() if expand_merchant: data['expand[]'] = 'merchant' response = self._get_response( method='get', endpoint=endpoint, params=data, ) return MonzoTransaction(data=response.json()['transaction'])
def test_class_transaction_method(self, mocker, mocked_monzo, transaction_api_response): """Test class `transaction` method""" mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = transaction_api_response result = mocked_monzo.transaction('foobar') mocked_get_response.assert_called_once_with( method='get', endpoint='/transactions/foobar', params={}, ) expected_result = MonzoTransaction(transaction_api_response['transaction'], context=mocked_monzo) assert result == expected_result # With expanded merchant info mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = transaction_api_response result = mocked_monzo.transaction('foobar', expand_merchant=True) mocked_get_response.assert_called_once_with( method='get', endpoint='/transactions/foobar', params={ 'expand[]': 'merchant', }, ) expected_result = MonzoTransaction(transaction_api_response['transaction'], context=mocked_monzo) assert result == expected_result
def transaction(): data = { 'account_balance': 12343, 'amount': -150, 'created': str(arrow.utcnow()), 'currency': 'GBP', 'description': 'Test', 'id': '1234', 'merchant': 'Boots', 'metadata': '', 'notes': '', 'is_load': False } t = MonzoTransaction(data=data) return t
def transactions(self, account_id=None, reverse=True, limit=None): """ Returns a list of transactions on the user's account. Official docs: https://monzo.com/docs/#list-transactions :param account_id: Monzo account ID :type account_id: str :param reverse: whether transactions should be in in descending order :type reverse: bool :param limit: how many transactions should be returned; None for all :type limit: int :returns: list of Monzo transactions :rtype: list of MonzoTransaction """ transactions = self._raw_transactions(account_id=account_id, reverse=reverse, limit=limit) return [MonzoTransaction(data=t) for t in transactions]
def transactions(self, account_id=None, reverse=True, limit=None): """ Returns a list of transactions on the user's account. Official docs: https://monzo.com/docs/#list-transactions :param account_id: Monzo account ID :type account_id: str :param reverse: whether transactions should be in in descending order :type reverse: bool :param limit: how many transactions should be returned; None for all :type limit: int :returns: list of Monzo transactions :rtype: list of MonzoTransaction """ 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 = '/transactions' response = self._get_response( method='get', endpoint=endpoint, params={ 'account_id': account_id, }, ) # The API does not allow reversing the list or limiting it, so to do # the basic query of 'get the latest transaction' we need to always get # all transactions and do the reversing and slicing in Python # I send Monzo an email, we'll se how they'll respond transactions = response.json()['transactions'] if reverse: transactions.reverse() if limit: transactions = transactions[:limit] return [MonzoTransaction(data=t) for t in transactions]
def test_class_transactions_method(self, mocker, mocked_monzo, transactions_api_response, accounts_api_response): """Test class `transactions` method""" mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = transactions_api_response accounts_json = accounts_api_response['accounts'] mocked_monzo._cached_accounts = [ MonzoAccount(data=account) for account in accounts_json ] result = mocked_monzo.transactions() mocked_get_response.assert_called_once_with( method='get', endpoint='/transactions', params={ 'account_id': mocked_monzo._cached_accounts[0].id, }, ) transactions_json = transactions_api_response['transactions'] expected_result = [ MonzoTransaction(data=transaction, context=mocked_monzo) for transaction in transactions_json ] 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.transactions()