Skip to content

Commit f732248

Browse files
committed
Added unittests for the ApiContext.save() and ApiContext.restore() functions in order to test whether modifying them in the last commit broke them or not.
1 parent d4994de commit f732248

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

bunq/sdk/context.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def save(self, path=None, to_json=False):
259259
:type path: str
260260
:type to_json: bool
261261
262-
:rtype: None
262+
:rtype: Union[None, str]
263263
"""
264264

265265
if path is None:
@@ -289,6 +289,11 @@ def restore(cls, path=None, json_data=None):
289289
with open(path, cls._FILE_MODE_READ) as file:
290290
return converter.json_to_class(ApiContext, file.read())
291291

292+
def __eq__(self, other):
293+
return self.token == other.token \
294+
and self.api_key == other.api_key \
295+
and self.environment_type == other.environment_type
296+
292297

293298
class InstallationContext(object):
294299
"""
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os
2+
3+
from bunq.sdk.context import ApiContext
4+
from bunq.sdk.json import converter
5+
from tests.bunq_test import BunqSdkTestCase
6+
7+
8+
class ApiContextTest(BunqSdkTestCase):
9+
"""
10+
Tests:
11+
ApiContext
12+
"""
13+
14+
_TMP_FILE_PATH = '/context-save-test.conf'
15+
16+
@classmethod
17+
def setUpClass(cls):
18+
cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ
19+
cls._API_CONTEXT = cls._get_api_context()
20+
cls._TMP_FILE_PATH_FULL = (cls._get_directory_test_root() +
21+
cls._TMP_FILE_PATH)
22+
23+
def test_api_context_save(self):
24+
"""
25+
Converts an ApiContext to JSON data, saves the same ApiContext to a
26+
temporary file, and compares whether the JSON data is equal to the
27+
data in the file.
28+
29+
Removes the temporary file before assertion.
30+
"""
31+
32+
context_json = converter.class_to_json(self._API_CONTEXT)
33+
34+
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL)
35+
36+
with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file:
37+
context_retrieved = file.read()
38+
39+
os.remove(self._TMP_FILE_PATH_FULL)
40+
41+
self.assertEqual(context_retrieved, context_json)
42+
43+
def test_api_context_restore(self):
44+
"""
45+
Saves an ApiContext to a temporary file, restores an ApiContext from
46+
that file, and compares whether the api_keys, tokens, and environment
47+
types are equal in the ApiContext and the restored ApiContext.
48+
49+
Removes the temporary file before assertion.
50+
"""
51+
52+
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL)
53+
api_context_restored = ApiContext.restore(self._TMP_FILE_PATH_FULL)
54+
55+
os.remove(self._TMP_FILE_PATH_FULL)
56+
57+
self.assertEqual(api_context_restored, self._API_CONTEXT)
58+
59+
def test_api_context_save_json(self):
60+
"""
61+
Converts an ApiContext to JSON data, saves the ApiContext using the
62+
ApiContext.save() function with the to_JSON flag set to True, and
63+
compares whether the JSON data equals the returned JSON data from the
64+
ApiContext.save() function.
65+
"""
66+
67+
context_json = converter.class_to_json(self._API_CONTEXT)
68+
context_saved = self._API_CONTEXT.save(to_json=True)
69+
70+
self.assertEqual(context_saved, context_json)
71+
72+
def test_api_context_restore_json(self):
73+
"""
74+
Saves an ApiContext with the ApiContext.save() function with the
75+
to_JSON flag set to True, restores an ApiContext from the JSON data
76+
returned from the ApiContext.save() function, and checks that the
77+
api_key, token, and environment type variables of the restored
78+
ApiContext are equal to the respective variables in the original
79+
ApiContext.
80+
"""
81+
82+
context_json = self._API_CONTEXT.save(to_json=True)
83+
api_context_restored = self._API_CONTEXT.restore(json_data=context_json)
84+
85+
self.assertEqual(api_context_restored, self._API_CONTEXT)

0 commit comments

Comments
 (0)