-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separated requisition handling and improved startdate feature
- Loading branch information
dnbasta
committed
Mar 3, 2024
1 parent
3234ca8
commit c66a7e0
Showing
7 changed files
with
173 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from ynabapiimport.models.exceptions import NoRequisitionError, ReferenceNotUnique | ||
from ynabapiimport.requisitionhandler import RequisitionHandler | ||
|
||
|
||
@pytest.mark.parametrize('test_input', [[], [{'reference': 'xxx::xxx', 'status': 'LN'}]]) | ||
def test_fetch_requisition_fail(test_input): | ||
# Arrange | ||
client = MagicMock() | ||
client.requisition.get_requisitions.return_value = {'results': test_input} | ||
|
||
# Act & Assert | ||
rh = RequisitionHandler(client=client, reference='reference') | ||
with pytest.raises(NoRequisitionError): | ||
r = rh.fetch_requisition() | ||
|
||
|
||
def test_fetch_requisition_success(): | ||
# Arrange | ||
client = MagicMock() | ||
mock_req = {'reference': 'reference::xxx', 'status': 'LN'} | ||
client.requisition.get_requisitions.return_value = {'results': [mock_req]} | ||
|
||
# Act | ||
rh = RequisitionHandler(client=client, reference='reference') | ||
r = rh.fetch_requisition() | ||
|
||
# Assert | ||
assert r == mock_req | ||
|
||
|
||
def test_reference_is_unique_fail(): | ||
# Arrange | ||
req_list = [{'reference': 'xxx::xxx'}, {'reference': 'ref::xxx'}] | ||
|
||
# Act & Assert | ||
rh = RequisitionHandler(client=MagicMock(), reference='ref') | ||
with pytest.raises(ReferenceNotUnique): | ||
rh.reference_is_unique(req_list) | ||
|
||
|
||
@pytest.mark.parametrize('test_input', [[], [{'reference': 'xxx:xxx'}]]) | ||
def test_reference_is_unique_success(test_input): | ||
# Act | ||
rh = RequisitionHandler(client=MagicMock(), reference='ref') | ||
rh.reference_is_unique(test_input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import List | ||
from urllib.error import HTTPError | ||
from uuid import uuid4 | ||
|
||
from nordigen import NordigenClient | ||
|
||
from ynabapiimport.models.exceptions import NoRequisitionError, ReferenceNotUnique, ReferenceNotValid | ||
|
||
|
||
class RequisitionHandler: | ||
|
||
def __init__(self, client: NordigenClient, reference: str): | ||
self._client = client | ||
self._reference = reference | ||
|
||
def fetch_requisition(self) -> dict: | ||
try: | ||
results = self._client.requisition.get_requisitions()['results'] | ||
return next(r for r in results if r['status'] == 'LN' and r['reference'].split('::')[0] == self._reference) | ||
except (HTTPError, StopIteration): | ||
raise NoRequisitionError() | ||
|
||
def create_requisition_auth_link(self, institution_id: str) -> str: | ||
req_list = self._client.requisition.get_requisitions()['results'] | ||
|
||
self.delete_inactive_requisitions(req_list=req_list) | ||
self.reference_is_unique(req_list=req_list) | ||
self.reference_is_valid() | ||
|
||
init_session = self._client.initialize_session(institution_id=institution_id, | ||
redirect_uri='http://localhost:', | ||
reference_id=f"{self._reference}::{uuid4()}", | ||
max_historical_days=730) | ||
return init_session.link | ||
|
||
def delete_inactive_requisitions(self, req_list: List[dict]): | ||
inactive_requisitions = [r['id'] for r in req_list | ||
if r['status'] != 'LN' and r['reference'].split('::')[0] == self._reference] | ||
[self._client.requisition.delete_requisition(ir) for ir in inactive_requisitions] | ||
|
||
def delete_current_requisition(self): | ||
req = self.fetch_requisition() | ||
self._client.requisition.delete_requisition(requisition_id=req['id']) | ||
|
||
def reference_is_unique(self, req_list: List[dict]): | ||
try: | ||
existing_reference = next(r for r in req_list if r['reference'].split('::')[0] == self._reference) | ||
raise ReferenceNotUnique(f"{self._reference} already used for: {existing_reference}") | ||
except StopIteration: | ||
pass | ||
|
||
def reference_is_valid(self): | ||
if '::' in self._reference: | ||
raise ReferenceNotValid(f"'{self._reference}' is not valid: '::' is not allowed") | ||
|
||
def get_institutions(self, countrycode: str) -> List[dict]: | ||
institutions = self._client.institution.get_institutions(countrycode) | ||
return [{'institution_id': i['id'], 'name': i['name']} for i in institutions] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from datetime import date | ||
from typing import List | ||
|
||
from nordigen import NordigenClient | ||
|
||
from ynabapiimport.accountfetcher import AccountFetcher | ||
from ynabapiimport.models.transaction import Transaction | ||
|
||
|
||
class TransactionFetcher: | ||
def __init__(self, client: NordigenClient, reference: str, resource_id: str): | ||
self._client = client | ||
self._reference = reference | ||
self.resource_id = resource_id | ||
|
||
def fetch(self, startdate: date) -> List[Transaction]: | ||
af = AccountFetcher(client=self._client, reference=self._reference) | ||
|
||
if self.resource_id: | ||
account_id = af.fetch(resource_id=self.resource_id) | ||
else: | ||
account_id = af.fetch() | ||
|
||
account = self._client.account_api(id=account_id) | ||
|
||
transaction_dicts = account.get_transactions(date_from=date.strftime(startdate, '%Y-%m-%d')) | ||
transactions = [Transaction.from_dict(t) for t in transaction_dicts['transactions']['booked']] | ||
return transactions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters