Test base REST Client

This commit is contained in:
Cameron Yick
2017-08-28 22:15:53 -04:00
parent 91900fac0d
commit 1127113ef4
3 changed files with 74 additions and 0 deletions

67
tests/test_restclient.py Normal file
View File

@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# @Author: Cameron Yick
from unittest import TestCase
from tiingo.restclient import (RestClient, RestClientError)
# Tests of basic REST API functionality
BASE_URL = "http://www.google.com"
# Test valid url
class TestRestClient(TestCase):
def setUp(self):
client = RestClient()
client._base_url = BASE_URL
client._headers = {}
self._client = client
# Test Requests
def test_client_representation(self):
self.assertEqual(repr(self._client),
"<RestClient(url=\"{}\")>".format(BASE_URL))
# Test valid page
def test_valid_url(self):
response = self._client._request('GET', "")
self.assertEqual(response.status_code, 200)
# Test 404 error
def test_invalid_url(self):
with self.assertRaisesRegexp(RestClientError, "404") as context:
# Should return 404 error
self._client._request('GET', "bing_is_great")
print(context)
# Todo: try using an invalid HTTP method (i.e. SNAG) and catch the error
# Check if everything still works when a session is reused
# TODO: Figure how how to refactor so that we can run these same 3 tests,
# except with 2 different types of client.
class TestRestClientWithSession(TestCase):
def setUp(self):
config = {'session': True}
client = RestClient(config)
client._base_url = BASE_URL
client._headers = {}
self._client = client
# Test Requests
def test_client_representation(self):
self.assertEqual(repr(self._client),
"<RestClient(url=\"{}\")>".format(BASE_URL))
# Test valid page
def test_valid_url(self):
response = self._client._request('GET', "")
self.assertEqual(response.status_code, 200)
# Test 404 error
def test_invalid_url(self):
with self.assertRaisesRegexp(RestClientError, "404") as context:
# Should return 404 error
self._client._request('GET', "bing_is_great")
print(context)

View File

@@ -13,6 +13,7 @@ from tiingo import TiingoClient
# Wrap server errors with client side descriptive errors
# Coerce startDate/endDate to string if they are passed in as datetime
# Use VCR.py to enable offline testing
# Expand test coverage
# Refactor fixtures into separate file

View File

@@ -20,6 +20,12 @@ class RestClient(object):
"""
self._config = config
# The child class should override these properties or else the
# restclient won't work. Reevalute whether to do this as an abstract
# base class so it doesn't get used by itself.
self._headers = {}
self._base_url = ""
if config.get('session'):
self._session = requests.Session()
else: