Skip to content

Commit ac3779f

Browse files
felipao-mxmatin
andauthored
how to get the current balance (#183)
* example * clarification * fix * fix * tests * test * balance * bump version * comments * using mock for empty balance entry test and clarification * using mocks * lint * change order in __all__ Co-authored-by: Matin Tamizi <matin@users.noreply.github.com>
1 parent 9604e39 commit ac3779f

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ transfers = cuenca.Transfer.all(
9898
# the total number of succeeded transfers
9999
count = cuenca.Transfer.count(status=Status.succeeded)
100100
```
101+
## Balance
102+
103+
### Current balance
104+
```python
105+
import cuenca
106+
107+
# balance is the amount in cents
108+
balance = cuenca.get_balance()
109+
110+
```
111+
101112

102113
## Api Keys
103114

cuenca/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
'UserLogin',
2020
'WhatsappTransfer',
2121
'configure',
22+
'get_balance',
2223
]
2324

25+
from typing import cast
2426

25-
from .http import session
27+
from .http import Session, session
2628
from .resources import (
2729
Account,
2830
ApiKey,
@@ -46,3 +48,8 @@
4648
from .version import __version__
4749

4850
configure = session.configure
51+
52+
53+
def get_balance(session: Session = session) -> int:
54+
balance_entry = cast('BalanceEntry', BalanceEntry.first(session=session))
55+
return balance_entry.rolling_balance if balance_entry else 0

cuenca/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = '0.7.10'
1+
__version__ = '0.7.11'
22
CLIENT_VERSION = __version__
33
API_VERSION = '2020-03-19'

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ mypy==0.910
66
pytest==6.2.*
77
pytest-cov==2.12.*
88
pytest-vcr==1.0.*
9+
requests-mock==1.8.*
910
types-freezegun
1011
types-requests

tests/test_cuenca.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import requests_mock # type: ignore
2+
3+
import cuenca
4+
5+
6+
def test_get_balance():
7+
# It is the case when the user has transactions in the account
8+
response_json = {
9+
'items': [
10+
{
11+
'id': 'LE6xvD1ocHmSIu7MgzlC9woj',
12+
'created_at': '2021-08-17T21:45:23.061000',
13+
'user_id': 'US3zGWi1n852bTyqD1wCZ1ft',
14+
'name': 'RESTAURANT BRASIL MEXICO DF CMXMX',
15+
'amount': 1212,
16+
'descriptor': '',
17+
'rolling_balance': 7578889,
18+
'type': 'debit',
19+
'related_transaction_uri': '/card_transactions/CT3TA',
20+
'funding_instrument_uri': '/cards/CA243qFpGLDAaPamCMYHhS0p',
21+
'wallet_id': 'default',
22+
}
23+
],
24+
'next_page_uri': None,
25+
}
26+
with requests_mock.mock() as m:
27+
m.get(
28+
'https://sandbox.cuenca.com/balance_entries?limit=1',
29+
status_code=200,
30+
json=response_json,
31+
)
32+
33+
balance = cuenca.get_balance()
34+
assert balance == response_json['items'][0]['rolling_balance']
35+
36+
37+
def test_get_balance_before_first_transaction():
38+
# When the user have no transactions at all
39+
# balance_entries endpoint returns `items` as empty list.
40+
# It means that its balance is Mx$0.00
41+
response_json = {'items': [], 'next_page_uri': None}
42+
43+
with requests_mock.mock() as m:
44+
m.get(
45+
'https://sandbox.cuenca.com/balance_entries?limit=1',
46+
status_code=200,
47+
json=response_json,
48+
)
49+
50+
balance = cuenca.get_balance()
51+
assert balance == 0

0 commit comments

Comments
 (0)