Files
tiingo-python/tests/test_restclient.py
2017-08-28 22:20:33 -04:00

68 lines
2.0 KiB
Python

# -*- 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)