-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load and Save an ApiContext from and to JSON Data #13
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
67dfb41
Added functionality to the ApiContext to save and restore a context t…
PJUllrich d4994de
Add a clarification of the example config.json to the Test Readme
PJUllrich f732248
Added unittests for the ApiContext.save() and ApiContext.restore() fu…
PJUllrich eb969c0
Extracted a to_json and from_json function from ApiContext.save() and…
PJUllrich 60a4579
Formatted the docstrings
PJUllrich 592b44c
Deleted str from docstring in save()
PJUllrich 60440e0
Making PRs is fun!
PJUllrich 7e62a69
Little cleanup
dnl-blkv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,85 @@ | ||
import os | ||
|
||
from bunq.sdk.context import ApiContext | ||
from bunq.sdk.json import converter | ||
from tests.bunq_test import BunqSdkTestCase | ||
|
||
|
||
class ApiContextTest(BunqSdkTestCase): | ||
""" | ||
Tests: | ||
ApiContext | ||
""" | ||
|
||
_TMP_FILE_PATH = '/context-save-test.conf' | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ | ||
cls._API_CONTEXT = cls._get_api_context() | ||
cls._TMP_FILE_PATH_FULL = (cls._get_directory_test_root() + | ||
cls._TMP_FILE_PATH) | ||
|
||
def test_api_context_save(self): | ||
""" | ||
Converts an ApiContext to JSON data, saves the same ApiContext to a | ||
temporary file, and compares whether the JSON data is equal to the | ||
data in the file. | ||
|
||
Removes the temporary file before assertion. | ||
""" | ||
|
||
context_json = converter.class_to_json(self._API_CONTEXT) | ||
|
||
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) | ||
|
||
with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file_: | ||
context_retrieved = file_.read() | ||
|
||
os.remove(self._TMP_FILE_PATH_FULL) | ||
|
||
self.assertEqual(context_retrieved, context_json) | ||
|
||
def test_api_context_restore(self): | ||
""" | ||
Saves an ApiContext to a temporary file, restores an ApiContext from | ||
that file, and compares whether the api_keys, tokens, and environment | ||
types are equal in the ApiContext and the restored ApiContext. | ||
|
||
Removes the temporary file before assertion. | ||
""" | ||
|
||
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) | ||
api_context_restored = ApiContext.restore(self._TMP_FILE_PATH_FULL) | ||
|
||
os.remove(self._TMP_FILE_PATH_FULL) | ||
|
||
self.assertEqual(api_context_restored, self._API_CONTEXT) | ||
|
||
def test_api_context_save_json(self): | ||
""" | ||
Converts an ApiContext to JSON data, saves the ApiContext using the | ||
ApiContext.save() function with the to_JSON flag set to True, and | ||
compares whether the JSON data equals the returned JSON data from the | ||
ApiContext.save() function. | ||
""" | ||
|
||
context_json = converter.class_to_json(self._API_CONTEXT) | ||
context_saved = self._API_CONTEXT.to_json() | ||
|
||
self.assertEqual(context_saved, context_json) | ||
|
||
def test_api_context_restore_json(self): | ||
""" | ||
Saves an ApiContext with the ApiContext.save() function with the | ||
to_JSON flag set to True, restores an ApiContext from the JSON data | ||
returned from the ApiContext.save() function, and checks that the | ||
api_key, token, and environment type variables of the restored | ||
ApiContext are equal to the respective variables in the original | ||
ApiContext. | ||
""" | ||
|
||
context_json = self._API_CONTEXT.to_json() | ||
api_context_restored = self._API_CONTEXT.from_json(context_json) | ||
|
||
self.assertEqual(api_context_restored, self._API_CONTEXT) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, shouldn't this class be named
TestApiContext
? @dnl-blkvThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rip already merged 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OGKevin Please create a PR :P
Nice catch!