|
| 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