Skip to content
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 8 commits into from
Aug 15, 2017
32 changes: 28 additions & 4 deletions bunq/sdk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ def session_context(self):

return self._session_context

def to_json(self):
"""
Serializes an ApiInstance to JSON data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serializes an ApiContext to JSON string.


:rtype: str
"""
return converter.class_to_json(self)
Copy link
Contributor

@dnl-blkv dnl-blkv Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after docstring please


@classmethod
def from_json(cls, data):
"""
Creates an ApiContext instance from JSON data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creates an ApiContext instance from JSON string.


:type data: str

:rtype: ApiContext
"""
return converter.json_to_class(ApiContext, data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline after the docstring please


def save(self, path=None):
"""
:type path: str
Expand All @@ -264,8 +283,8 @@ def save(self, path=None):
if path is None:
path = self._PATH_API_CONTEXT_DEFAULT

with open(path, self._FILE_MODE_WRITE) as file:
file.write(converter.class_to_json(self))
with open(path, self._FILE_MODE_WRITE) as file_:
file_.write(self.to_json())

@classmethod
def restore(cls, path=None):
Expand All @@ -278,8 +297,13 @@ def restore(cls, path=None):
if path is None:
path = cls._PATH_API_CONTEXT_DEFAULT

with open(path, cls._FILE_MODE_READ) as file:
return converter.json_to_class(ApiContext, file.read())
with open(path, cls._FILE_MODE_READ) as file_:
return cls.from_json(file_.read())

def __eq__(self, other):
return (self.token == other.token and
self.api_key == other.api_key and
self.environment_type == other.environment_type)


class InstallationContext(object):
Expand Down
10 changes: 10 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ of a configuration file is located at [`tests/assets/config.example.json`](./ass
In order to make use of the configuration file, please copy the example to the
same directory, fill in your sandbox user data and rename the copy to config.json.

Note:
* `MONETARY_ACCOUNT_ID` and `MONETARY_ACCOUNT_ID2` must be of same user
* `CounterPartyOther` must be of another Sandbox user
* You can create a `CASH_REGISTER_ID` on doc.bunq.com
1. Add your **Developer Key** to `settings`
2. Upload an image to the `Attachment Public` endpoint
3. Create an `Avatar` with the returned UUID
4. Use the Avatar's UUID to create a `Cash Register`
5. Copy the Cash Register's ID to the `config.json`

## Execution

You can run the tests via command line:
Expand Down
85 changes: 85 additions & 0 deletions tests/model/generated/test_api_context.py
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):
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rip already merged 🙈

Copy link
Contributor

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!

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