|
| 1 | +import unittest |
| 2 | +from nasdaqdatalink.model.authorized_session import AuthorizedSession |
| 3 | +from nasdaqdatalink.api_config import ApiConfig |
| 4 | +from requests.sessions import Session |
| 5 | +from requests.adapters import HTTPAdapter |
| 6 | +from mock import patch |
| 7 | + |
| 8 | + |
| 9 | +class AuthorizedSessionTest(unittest.TestCase): |
| 10 | + def test_authorized_session_assign_correct_internal_config(self): |
| 11 | + authed_session = AuthorizedSession() |
| 12 | + self.assertTrue(issubclass(authed_session._api_config, ApiConfig)) |
| 13 | + authed_session = AuthorizedSession(None) |
| 14 | + self.assertTrue(issubclass(authed_session._api_config, ApiConfig)) |
| 15 | + api_config = ApiConfig() |
| 16 | + authed_session = AuthorizedSession(api_config) |
| 17 | + self.assertTrue(isinstance(authed_session._api_config, ApiConfig)) |
| 18 | + |
| 19 | + def test_authorized_session_pass_created_session(self): |
| 20 | + ApiConfig.use_retries = True |
| 21 | + ApiConfig.number_of_retries = 130 |
| 22 | + authed_session = AuthorizedSession() |
| 23 | + self.assertTrue(isinstance(authed_session._auth_session, Session)) |
| 24 | + adapter = authed_session._auth_session.get_adapter(ApiConfig.api_protocol) |
| 25 | + self.assertTrue(isinstance(adapter, HTTPAdapter)) |
| 26 | + self.assertEqual(adapter.max_retries.connect, 130) |
| 27 | + |
| 28 | + @patch("nasdaqdatalink.get") |
| 29 | + def test_call_get_with_session_and_api_config(self, mock): |
| 30 | + api_config = ApiConfig() |
| 31 | + authed_session = AuthorizedSession(api_config) |
| 32 | + authed_session.get('WIKI/AAPL') |
| 33 | + mock.assert_called_with('WIKI/AAPL', api_config=api_config, |
| 34 | + session=authed_session._auth_session) |
| 35 | + |
| 36 | + @patch("nasdaqdatalink.bulkdownload") |
| 37 | + def test_call_bulkdownload_with_session_and_api_config(self, mock): |
| 38 | + api_config = ApiConfig() |
| 39 | + authed_session = AuthorizedSession(api_config) |
| 40 | + authed_session.bulkdownload('NSE') |
| 41 | + mock.assert_called_with('NSE', api_config=api_config, |
| 42 | + session=authed_session._auth_session) |
| 43 | + |
| 44 | + @patch("nasdaqdatalink.export_table") |
| 45 | + def test_call_export_table_with_session_and_api_config(self, mock): |
| 46 | + authed_session = AuthorizedSession() |
| 47 | + authed_session.export_table('WIKI/AAPL') |
| 48 | + mock.assert_called_with('WIKI/AAPL', api_config=ApiConfig, |
| 49 | + session=authed_session._auth_session) |
| 50 | + |
| 51 | + @patch("nasdaqdatalink.get_table") |
| 52 | + def test_call_get_table_with_session_and_api_config(self, mock): |
| 53 | + authed_session = AuthorizedSession() |
| 54 | + authed_session.get_table('WIKI/AAPL') |
| 55 | + mock.assert_called_with('WIKI/AAPL', api_config=ApiConfig, |
| 56 | + session=authed_session._auth_session) |
| 57 | + |
| 58 | + @patch("nasdaqdatalink.get_point_in_time") |
| 59 | + def test_call_get_point_in_time_with_session_and_api_config(self, mock): |
| 60 | + authed_session = AuthorizedSession() |
| 61 | + authed_session.get_point_in_time('DATABASE/CODE', interval='asofdate', date='2020-01-01') |
| 62 | + mock.assert_called_with('DATABASE/CODE', interval='asofdate', |
| 63 | + date='2020-01-01', api_config=ApiConfig, |
| 64 | + session=authed_session._auth_session) |
0 commit comments