mirror of
https://github.com/hydrosquall/tiingo-python.git
synced 2025-12-17 20:04:19 +01:00
Test base REST Client
This commit is contained in:
67
tests/test_restclient.py
Normal file
67
tests/test_restclient.py
Normal 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)
|
||||
Reference in New Issue
Block a user