Skip to content

Commit 35240cd

Browse files
authored
Merge pull request #13 from PJUllrich/develop
Load and Save an ApiContext from and to JSON Data
2 parents 356d5ca + 7e62a69 commit 35240cd

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed

bunq/sdk/context.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,17 @@ def save(self, path=None):
264264
if path is None:
265265
path = self._PATH_API_CONTEXT_DEFAULT
266266

267-
with open(path, self._FILE_MODE_WRITE) as file:
268-
file.write(converter.class_to_json(self))
267+
with open(path, self._FILE_MODE_WRITE) as file_:
268+
file_.write(self.to_json())
269+
270+
def to_json(self):
271+
"""
272+
Serializes an ApiContext to JSON string.
273+
274+
:rtype: str
275+
"""
276+
277+
return converter.class_to_json(self)
269278

270279
@classmethod
271280
def restore(cls, path=None):
@@ -278,8 +287,25 @@ def restore(cls, path=None):
278287
if path is None:
279288
path = cls._PATH_API_CONTEXT_DEFAULT
280289

281-
with open(path, cls._FILE_MODE_READ) as file:
282-
return converter.json_to_class(ApiContext, file.read())
290+
with open(path, cls._FILE_MODE_READ) as file_:
291+
return cls.from_json(file_.read())
292+
293+
@classmethod
294+
def from_json(cls, json_str):
295+
"""
296+
Creates an ApiContext instance from JSON string.
297+
298+
:type json_str: str
299+
300+
:rtype: ApiContext
301+
"""
302+
303+
return converter.json_to_class(ApiContext, json_str)
304+
305+
def __eq__(self, other):
306+
return (self.token == other.token and
307+
self.api_key == other.api_key and
308+
self.environment_type == other.environment_type)
283309

284310

285311
class InstallationContext(object):

tests/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ of a configuration file is located at [`tests/assets/config.example.json`](./ass
3737
In order to make use of the configuration file, please copy the example to the
3838
same directory, fill in your sandbox user data and rename the copy to config.json.
3939

40+
Note:
41+
* `MONETARY_ACCOUNT_ID` and `MONETARY_ACCOUNT_ID2` must be of same user
42+
* `CounterPartyOther` must be of another Sandbox user
43+
* You can create a `CASH_REGISTER_ID` on doc.bunq.com
44+
1. Add your **Developer Key** to `settings`
45+
2. Upload an image to the `Attachment Public` endpoint
46+
3. Create an `Avatar` with the returned UUID
47+
4. Use the Avatar's UUID to create a `Cash Register`
48+
5. Copy the Cash Register's ID to the `config.json`
49+
4050
## Execution
4151

4252
You can run the tests via command line:
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.to_json()
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.to_json()
83+
api_context_restored = self._API_CONTEXT.from_json(context_json)
84+
85+
self.assertEqual(api_context_restored, self._API_CONTEXT)

0 commit comments

Comments
 (0)