Skip to content

Commit

Permalink
Merge pull request #4 from dnbasta/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dnbasta authored May 9, 2024
2 parents 1e4851c + 5a861bb commit 19d44c7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![GitHub Release](https://img.shields.io/github/release/dnbasta/ynab-api-import?style=flat)]()
[![Github Release](https://img.shields.io/maintenance/yes/2100)]()
[![Monthly downloads](https://img.shields.io/pypi/dm/ynab-api-import)]()

[!["Buy Me A Coffee"](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://www.buymeacoffee.com/dnbasta)

Expand Down
38 changes: 19 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions tests/test_ynabapiimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@
from ynabapiimport.models.exceptions import BalancesDontMatchError


@patch('ynabapiimport.ynabapiimport.AccountClient')
@patch('ynabapiimport.ynabapiimport.YnabApiImport._account_client')
@patch('ynabapiimport.ynabapiimport.NordigenClient')
@patch('ynabapiimport.ynabapiimport.YnabClient')
def test_compare_balances(mock_ynab_client, mock_nordigen_client, mock_account):
# Arrange
yi = YnabApiImport(secret_id='', secret_key='', token='', resource_id='', reference='reference',
budget_id='', account_id='')
yi._account_client = MagicMock(**{'fetch_balances.return_value': (dict(closingBooked=1000), 500)})
mock_account.fetch_balances.return_value = (dict(closingBooked=1000), 500)
yi._ynab_client = MagicMock(**{'fetch_balance.return_value': 1000})
# Act
yi.compare_balances()

@patch('ynabapiimport.ynabapiimport.AccountClient')

@patch('ynabapiimport.ynabapiimport.YnabApiImport._account_client')
@patch('ynabapiimport.ynabapiimport.NordigenClient')
@patch('ynabapiimport.ynabapiimport.YnabClient')
def test_compare_balances_fail(mock_ynab_client, mock_nordigen_client, mock_account):
# Arrange
yi = YnabApiImport(secret_id='', secret_key='', token='', resource_id='', reference='reference',
budget_id='', account_id='')
yi._account_client = MagicMock(**{'fetch_balances.return_value': (dict(closingBooked=2000), 500)})
mock_account.fetch_balances.return_value = (dict(closingBooked=2000), 500)
yi._ynab_client = MagicMock(**{'fetch_balance.return_value': 1000})
# Act
with pytest.raises(BalancesDontMatchError) as e:
Expand Down
17 changes: 11 additions & 6 deletions ynabapiimport/ynabapiimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ def __init__(self, secret_id: str, secret_key: str, token: str,
:param budget_id: YNAB budget id
:param account_id: YNAB account id
:param resource_id: GoCardless resource id to specify accounts in case multiple are available
:raises NoRequisitionError: if GoCardless connection is not valid
:raises NoAccountError: if no account is available in authorized GoCardless connection
:raises MultipleAccountsError: if multiple accounts are available in authorized GoCardless connection
"""
self.logger = self._set_up_logger()
self.reference = reference
self.resource_id = resource_id
self._ynab_client = YnabClient(token=token, account_id=account_id, budget_id=budget_id)
self._api_client = NordigenClient(secret_id=secret_id, secret_key=secret_key)
self._api_client.generate_token()
self._account_client = AccountClient.from_api_client(client=self._api_client, reference=self.reference,
resource_id=self.resource_id)

@property
def _account_client(self):
return AccountClient.from_api_client(client=self._api_client, reference=self.reference,
resource_id=self.resource_id)

@classmethod
def from_yaml(cls, path: str):
Expand Down Expand Up @@ -72,10 +72,12 @@ def import_transactions(self, startdate: date = None, memo_regex: str = None) ->
:param startdate: date from which to start importing. Will only work for up to max_history_days available for the bank. If not specified will fetch for last 90 days
:param memo_regex: regex pattern to parse memos from bank transactions
:return: count of processed transactions
:raises NoRequisitionError: if GoCardless connection is not valid
:raises NoAccountError: if no account is available in authorized GoCardless connection
:raises MultipleAccountsError: if multiple accounts are available in authorized GoCardless connection
"""
if startdate is None:
startdate = date.today() - timedelta(days=90)

transactions = self._account_client.fetch_transactions(startdate=startdate)

if memo_regex:
Expand All @@ -91,6 +93,9 @@ def compare_balances(self):
Compares balance variants for the account (e.g. expected, closingBooked) from API and from YNAB. The method
compares the plain balance values as well as the balances minus the sum of still pending transactions.
:raises BalancesDontMatchError: if none of the API returned balances for the account match with the one in YNAB
:raises NoRequisitionError: if GoCardless connection is not valid
:raises NoAccountError: if no account is available in authorized GoCardless connection
:raises MultipleAccountsError: if multiple accounts are available in authorized GoCardless connection
"""
ab, ap = self._account_client.fetch_balances()
yb = self._ynab_client.fetch_balance()
Expand Down

0 comments on commit 19d44c7

Please sign in to comment.