Skip to content

Commit f8adcac

Browse files
committed
Cleaned up tests. Added type hints, improved formatting and fixed imports.
1 parent c293bf1 commit f8adcac

18 files changed

+184
-439
lines changed

tests/bunq_test.py

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import os
22
import time
33
import unittest
4+
from typing import AnyStr
45

6+
from bunq.sdk.context.api_context import ApiContext
57
from bunq.sdk.context.bunq_context import BunqContext
68
from bunq.sdk.exception.bunq_exception import BunqException
79
from bunq.sdk.http.api_client import ApiClient
10+
from bunq.sdk.model.generated.endpoint import MonetaryAccountBank, RequestInquiry, AttachmentPublic, Avatar, \
11+
CashRegister
12+
from bunq.sdk.model.generated.object_ import Amount, Pointer
813
from bunq.sdk.util import util
9-
from bunq.sdk.model.generated import endpoint
10-
from bunq.sdk.model.generated import object_
1114

1215

1316
class BunqSdkTestCase(unittest.TestCase):
1417
"""
15-
:type _second_monetary_account: endpoint.MonetaryAccountBank
16-
:type _cash_register: endpoint.CashRegister
18+
:type _second_monetary_account: MonetaryAccountBank
19+
:type _cash_register: CashRegister
1720
"""
1821

1922
# Error constants.
@@ -61,31 +64,25 @@ def setUp(self):
6164
BunqContext.user_context().refresh_user_context()
6265

6366
def __set_second_monetary_account(self):
64-
response = endpoint.MonetaryAccountBank.create(
67+
response = MonetaryAccountBank.create(
6568
self.__CURRENCY_EUR,
6669
self.__SECOND_MONETARY_ACCOUNT_DESCRIPTION
6770
)
6871

69-
self._second_monetary_account = endpoint.MonetaryAccountBank.get(
72+
self._second_monetary_account = MonetaryAccountBank.get(
7073
response.value
7174
).value
7275

7376
def __request_spending_money(self):
74-
endpoint.RequestInquiry.create(
75-
object_.Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
76-
object_.Pointer(
77-
self._POINTER_EMAIL,
78-
self.__SPENDING_MONEY_RECIPIENT
79-
),
77+
RequestInquiry.create(
78+
Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
79+
Pointer(self._POINTER_EMAIL, self.__SPENDING_MONEY_RECIPIENT),
8080
self.__REQUEST_SPENDING_DESCRIPTION,
8181
False
8282
)
83-
endpoint.RequestInquiry.create(
84-
object_.Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
85-
object_.Pointer(
86-
self._POINTER_EMAIL,
87-
self.__SPENDING_MONEY_RECIPIENT
88-
),
83+
RequestInquiry.create(
84+
Amount(self.__SPENDING_MONEY_AMOUNT, self.__CURRENCY_EUR),
85+
Pointer(self._POINTER_EMAIL, self.__SPENDING_MONEY_RECIPIENT),
8986
self.__REQUEST_SPENDING_DESCRIPTION,
9087
False,
9188
self._second_monetary_account.id_
@@ -98,55 +95,38 @@ def _get_cash_register_id(self):
9895
return self._cash_register.id_
9996

10097
@classmethod
101-
def _get_api_context(cls):
102-
"""
103-
:rtype: ApiContext
104-
"""
105-
98+
def _get_api_context(cls) -> ApiContext:
10699
return util.automatic_sandbox_install()
107100

108-
def _get_pointer_bravo(self):
109-
"""
110-
:rtype: object_.Pointer
111-
"""
112-
113-
return object_.Pointer(self._POINTER_EMAIL, self._EMAIL_BRAVO)
114-
115-
def _get_alias_second_account(self):
116-
"""
117-
:rtype: object_.Pointer
118-
"""
101+
def _get_pointer_bravo(self) -> Pointer:
102+
return Pointer(self._POINTER_EMAIL, self._EMAIL_BRAVO)
119103

104+
def _get_alias_second_account(self) -> Pointer:
120105
return self._second_monetary_account.alias[self._FIRST_INDEX]
121106

122107
@staticmethod
123108
def _get_directory_test_root():
124109
return os.path.dirname(os.path.abspath(__file__))
125110

126111
def _set_cash_register(self):
127-
attachment_uuid = endpoint.AttachmentPublic.create(
112+
attachment_uuid = AttachmentPublic.create(
128113
self._attachment_contents,
129114
{
130115
ApiClient.HEADER_CONTENT_TYPE: self._CONTENT_TYPE,
131-
ApiClient.HEADER_ATTACHMENT_DESCRIPTION:
132-
self._ATTACHMENT_DESCRIPTION,
116+
ApiClient.HEADER_ATTACHMENT_DESCRIPTION: self._ATTACHMENT_DESCRIPTION,
133117
}
134118
)
135-
avatar_uuid = endpoint.Avatar.create(attachment_uuid.value)
136-
cash_register_id = endpoint.CashRegister.create(
119+
avatar_uuid = Avatar.create(attachment_uuid.value)
120+
cash_register_id = CashRegister.create(
137121
self.__CASH_REGISTER_DESCRIPTION,
138122
self.__CASH_REGISTER_STATUS,
139123
avatar_uuid.value
140124
)
141125

142-
self._cash_register = endpoint.CashRegister.get(cash_register_id.value)
126+
self._cash_register = CashRegister.get(cash_register_id.value)
143127

144128
@property
145-
def _attachment_contents(self):
146-
"""
147-
:rtype: bytes
148-
"""
149-
129+
def _attachment_contents(self) -> AnyStr:
150130
with open(
151131
self._get_directory_test_root() +
152132
self._PATH_ATTACHMENT +
@@ -156,17 +136,11 @@ def _attachment_contents(self):
156136
return file.read()
157137

158138
@property
159-
def alias_first(self):
160-
"""
161-
:rtype: Pointer
162-
"""
163-
139+
def alias_first(self) -> Pointer:
164140
if BunqContext.user_context().is_only_user_company_set():
165-
return BunqContext.user_context().user_company.alias[
166-
self._FIRST_INDEX]
141+
return BunqContext.user_context().user_company.alias[self._FIRST_INDEX]
167142

168143
if BunqContext.user_context().is_only_user_person_set():
169-
return BunqContext.user_context().user_person.alias[
170-
self._FIRST_INDEX]
144+
return BunqContext.user_context().user_person.alias[self._FIRST_INDEX]
171145

172146
raise BunqException(self.__ERROR_COULD_NOT_DETERMINE_USER)

tests/config.py

Lines changed: 25 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
22
import os
33

4-
from bunq.sdk.model.generated import object_
4+
from typing import List, Any
5+
6+
from bunq.sdk.model.generated.object_ import Pointer
57

68

79
class Config:
@@ -26,105 +28,53 @@ class Config:
2628
_FIELD_ATTACHMENT_CONTENT_TYPE = "CONTENT_TYPE"
2729

2830
@classmethod
29-
def get_attachment_content_type(cls):
30-
"""
31-
:rtype: str
32-
"""
33-
34-
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
35-
cls._FIELD_ATTACHMENT_CONTENT_TYPE]
31+
def get_attachment_content_type(cls) -> str:
32+
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][cls._FIELD_ATTACHMENT_CONTENT_TYPE]
3633

3734
@classmethod
38-
def get_attachment_description(cls):
39-
"""
40-
:rtype: str
41-
"""
42-
43-
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
44-
cls._FIELD_ATTACHMENT_DESCRIPTION]
35+
def get_attachment_description(cls) -> str:
36+
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][cls._FIELD_ATTACHMENT_DESCRIPTION]
4537

4638
@classmethod
47-
def get_attachment_path_in(cls):
48-
"""
49-
:rtype: str
50-
"""
51-
52-
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][
53-
cls._FIELD_ATTACHMENT_PATH_IN]
39+
def get_attachment_path_in(cls) -> str:
40+
return cls._get_config_file()[cls._FIELD_ATTACHMENT_PUBLIC][cls._FIELD_ATTACHMENT_PATH_IN]
5441

5542
@classmethod
56-
def get_api_key(cls):
57-
"""
58-
:rtype: str
59-
"""
60-
43+
def get_api_key(cls) -> str:
6144
return cls._get_config_file()[cls._FIELD_API_KEY]
6245

6346
@classmethod
64-
def get_user_id(cls):
65-
"""
66-
:rtype: int
67-
"""
68-
47+
def get_user_id(cls) -> int:
6948
return int(cls._get_config_file()[cls._FIELD_USER_ID])
7049

7150
@classmethod
72-
def get_monetary_account_id_2(cls):
73-
"""
74-
:rtype: int
75-
"""
76-
51+
def get_monetary_account_id_2(cls) -> int:
7752
return int(cls._get_config_file()[cls._FIELD_MONETARY_ACCOUNT_ID_2])
7853

7954
@classmethod
80-
def get_monetary_account_id_1(cls):
81-
"""
82-
:rtype: int
83-
"""
84-
55+
def get_monetary_account_id_1(cls) -> int:
8556
return int(cls._get_config_file()[cls._FIELD_MONETARY_ACCOUNT_ID_1])
8657

8758
@classmethod
88-
def get_cash_register_id(cls):
89-
"""
90-
:rtype str
91-
"""
92-
93-
return cls._get_config_file()[cls._FIELD_TAB_USAGE][
94-
cls._FIELD_CASH_REGISTER_ID]
59+
def get_cash_register_id(cls) -> str:
60+
return cls._get_config_file()[cls._FIELD_TAB_USAGE][cls._FIELD_CASH_REGISTER_ID]
9561

9662
@classmethod
97-
def get_pointer_counter_party_self(cls):
98-
"""
99-
:rtype: Pointer
100-
"""
101-
102-
type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][
103-
cls._FIELD_TYPE]
104-
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][
105-
cls._FIELD_ALIAS]
63+
def get_pointer_counter_party_self(cls) -> Pointer:
64+
type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][cls._FIELD_TYPE]
65+
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_SELF][cls._FIELD_ALIAS]
10666

107-
return object_.Pointer(type_, alias)
67+
return Pointer(type_, alias)
10868

10969
@classmethod
110-
def get_pointer_counter_party_other(cls):
111-
"""
112-
:rtype: Pointer
113-
"""
70+
def get_pointer_counter_party_other(cls) -> Pointer:
71+
type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][cls._FIELD_TYPE]
72+
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][cls._FIELD_ALIAS]
11473

115-
type_ = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][
116-
cls._FIELD_TYPE]
117-
alias = cls._get_config_file()[cls._FIELD_COUNTER_PARTY_OTHER][
118-
cls._FIELD_ALIAS]
119-
120-
return object_.Pointer(type_, alias)
74+
return Pointer(type_, alias)
12175

12276
@classmethod
123-
def get_permitted_ips(cls):
124-
"""
125-
:rtype: list[str]
126-
"""
127-
77+
def get_permitted_ips(cls) -> List[str]:
12878
permitted_ips_str = cls._get_config_file()[cls._FIELD_PERMITTED_IPS]
12979

13080
if not permitted_ips_str:
@@ -133,11 +83,7 @@ def get_permitted_ips(cls):
13383
return permitted_ips_str.split(cls._DELIMITER_IPS)
13484

13585
@classmethod
136-
def _get_config_file(cls):
137-
"""
138-
:rtype: json.load
139-
"""
140-
86+
def _get_config_file(cls) -> Any:
14187
file_path = os.path.dirname(os.path.realpath(__file__))
14288
with open(file_path + "/assets/config.json", "r") as f:
14389
return json.load(f)

tests/context/test_api_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TestApiContext(BunqSdkTestCase):
2020
__TIME_STAMP_IN_PAST = '2000-04-07 19:50:43.839717'
2121

2222
@classmethod
23-
def setUpClass(cls):
23+
def setUpClass(cls) -> None:
2424
super().setUpClass()
2525
cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ
2626
cls._API_CONTEXT = cls._get_api_context()
@@ -96,8 +96,8 @@ def test_auto_bunq_context_update(self):
9696
Tests the auto update of BunqContext.
9797
"""
9898

99-
api_context: ApiContext = BunqContext.api_context()
100-
api_context_json: object = json.loads(api_context.to_json())
99+
api_context = BunqContext.api_context()
100+
api_context_json = json.loads(api_context.to_json())
101101

102102
api_context_json[self.__FIELD_SESSION_CONTEXT][self.__FIELD_EXPIRE_TIME] = self.__TIME_STAMP_IN_PAST
103103

tests/context/test_psd2_context.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ class TestPsd2Context(unittest.TestCase):
2828
def setUpClass(cls) -> None:
2929
cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ
3030
cls._FILE_TEST_CONFIGURATION_PATH_FULL = (
31-
BunqSdkTestCase._get_directory_test_root() +
32-
cls._FILE_TEST_CONFIGURATION)
31+
BunqSdkTestCase._get_directory_test_root() + cls._FILE_TEST_CONFIGURATION
32+
)
3333
cls._FILE_TEST_OAUTH_PATH_FULL = (
34-
BunqSdkTestCase._get_directory_test_root() +
35-
cls._FILE_TEST_OAUTH)
34+
BunqSdkTestCase._get_directory_test_root() + cls._FILE_TEST_OAUTH
35+
)
3636
cls._FILE_TEST_CERTIFICATE_PATH_FULL = (
37-
BunqSdkTestCase._get_directory_test_root() +
38-
cls._FILE_TEST_CERTIFICATE)
37+
BunqSdkTestCase._get_directory_test_root() + cls._FILE_TEST_CERTIFICATE
38+
)
3939
cls._FILE_TEST_CERTIFICATE_CHAIN_PATH_FULL = (
40-
BunqSdkTestCase._get_directory_test_root() +
41-
cls._FILE_TEST_CERTIFICATE_CHAIN)
40+
BunqSdkTestCase._get_directory_test_root() + cls._FILE_TEST_CERTIFICATE_CHAIN
41+
)
4242
cls._FILE_TEST_PRIVATE_KEY_PATH_FULL = (
43-
BunqSdkTestCase._get_directory_test_root() +
44-
cls._FILE_TEST_PRIVATE_KEY)
43+
BunqSdkTestCase._get_directory_test_root() + cls._FILE_TEST_PRIVATE_KEY
44+
)
4545
cls.setup_test_data()
4646

4747
@classmethod

tests/http/test_bad_request_with_response_id.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class TestPagination(BunqSdkTestCase):
1313
_INVALID_MONETARY_ACCOUNT_ID = 0
1414

1515
def test_bad_request_with_response_id(self):
16-
"""
17-
"""
1816
BunqContext.load_api_context(self._get_api_context())
1917

2018
with self.assertRaises(ApiException) as caught_exception:

0 commit comments

Comments
 (0)