|
| 1 | +import copy |
| 2 | +import json |
1 | 3 | from numbers import Number as Num
|
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +from plotly import session |
| 7 | +from plotly.tools import CREDENTIALS_FILE, CONFIG_FILE, _file_permissions |
| 8 | + |
| 9 | + |
| 10 | +class PlotlyTestCase(TestCase): |
| 11 | + |
| 12 | + # parent test case to assist with clean up of local credentials/config |
| 13 | + |
| 14 | + def __init__(self, **kwargs): |
| 15 | + self._file_credentials = None |
| 16 | + self._file_config = None |
| 17 | + self._session = None |
| 18 | + super(PlotlyTestCase, self).__init__(**kwargs) |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + self.stash_file_credentials_and_config() |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + self.restore_file_credentials_and_config() |
| 25 | + |
| 26 | + def stash_file_credentials_and_config(self): |
| 27 | + if _file_permissions: |
| 28 | + with open(CREDENTIALS_FILE, 'r') as f: |
| 29 | + self._file_credentials = json.load(f) |
| 30 | + with open(CONFIG_FILE, 'r') as f: |
| 31 | + self._file_config = json.load(f) |
| 32 | + |
| 33 | + def restore_file_credentials_and_config(self): |
| 34 | + if _file_permissions: |
| 35 | + if self._file_credentials is not None: |
| 36 | + with open(CREDENTIALS_FILE, 'w') as f: |
| 37 | + json.dump(self._file_credentials, f) |
| 38 | + if self._file_config is not None: |
| 39 | + with open(CONFIG_FILE, 'w') as f: |
| 40 | + json.load(self._file_config, f) |
| 41 | + |
| 42 | + def stash_session(self): |
| 43 | + self._session = copy.deepcopy(session._session) |
| 44 | + |
| 45 | + def restore_session(self): |
| 46 | + session._session.clear() # clear and update to preserve references. |
| 47 | + session._session.update(self._session) |
2 | 48 |
|
3 | 49 |
|
4 | 50 | def compare_dict(dict1, dict2, equivalent=True, msg='', tol=10e-8):
|
|
0 commit comments