def test_api_init_cache(self, set_env, sample_oid, sample_uri): a = api(consumer_key=sample_oid, callback_url=sample_uri, store_tokens=False) assert isinstance(a.cache, MemCache) assert isinstance(a.cache.refresh_token, EmptyToken) assert isinstance(a.cache.access_token, EmptyToken)
def test_get_raises_json(self, bad_json, valid_cache, sample_oid, sample_uri): a = api(consumer_key=sample_oid, callback_url=sample_uri, cache=valid_cache) a.request = lambda s, *a, **k: bad_json resource = Get(api=a) with pytest.raises(TDQueryError): resource.get()
def test_non_global_api(self, sample_oid, sample_uri): a = api(consumer_key=sample_oid, callback_url=sample_uri) assert a.consumer_key == sample_oid assert a.callback_url == sample_uri assert a.refresh_valid is False assert a.access_valid is False assert a.auth_valid is False
def test_quote_arg_api(self, valid_cache, sample_oid, sample_uri): a = api(consumer_key=sample_oid, callback_url=sample_uri, cache=valid_cache) r = MockResponse('{"symbol":"AAPL","quote":155.34}', 200) a.request = lambda s, *a, **k: r q = get_quotes("AAPL", api=a, output_format='json') assert isinstance(q, dict) assert q["symbol"] == "AAPL"
def test_instrument_arg_api(self, valid_cache, sample_oid, sample_uri): a = api(consumer_key=sample_oid, callback_url=sample_uri, cache=valid_cache) r = MockResponse('{"symbol":"ORCL"}', 200) a.request = lambda s, *a, **k: r i = get_instrument("68389X105", api=a, output_format='json') assert isinstance(i, dict) assert i["symbol"] == "ORCL"
def test_invalid_access_quote(self, valid_cache, sample_oid, sample_uri, monkeypatch): a = api(consumer_key=sample_oid, callback_url=sample_uri, cache=valid_cache) r = MockResponse('{"error":"Not Authrorized"}', 401) def _mock_handler(self, *args, **kwargs): return a.handle_response(r) a.request = _mock_handler with pytest.raises(AuthorizationError): get_quotes("AAPL", api=a)
def test_api_passed_dict(self, sample_oid, sample_uri, valid_cache): params = { "consumer_key": sample_oid, "callback_url": sample_uri, "cache": valid_cache } a = api(params) assert a.consumer_key == sample_oid assert a.callback_url == sample_uri assert a.refresh_valid is True assert a.access_valid is True assert a.auth_valid is True
def test_api_pass_cache(self, set_env, sample_oid, sample_uri, valid_access_token, valid_refresh_token): c = MemCache() c.refresh_token = valid_refresh_token c.access_token = valid_access_token assert valid_access_token.valid is True a = api(consumer_key=sample_oid, callback_url=sample_uri, store_tokens=False, cache=c) assert isinstance(a.cache, MemCache) assert a.cache.refresh_token == c.refresh_token assert a.cache.access_token == c.access_token assert a.cache.access_token.valid is True