mirror of
https://github.com/hydrosquall/tiingo-python.git
synced 2025-12-18 04:14:20 +01:00
Use VCR.py to Mock API Responses
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
import csv
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
import vcr
|
||||
|
||||
from tiingo import TiingoClient
|
||||
from tiingo.restclient import RestClientError
|
||||
|
||||
@@ -33,9 +36,12 @@ class TestTickerPrices(TestCase):
|
||||
def setUp(self):
|
||||
self._client = TiingoClient()
|
||||
# Stub all endpoints that get reused
|
||||
self._ticker_price_response = self._client.get_ticker_price("GOOGL")
|
||||
self._ticker_metadata_response = \
|
||||
self._client.get_ticker_metadata("GOOGL")
|
||||
with vcr.use_cassette('tests/fixtures/ticker_price.yaml'):
|
||||
self._ticker_price_response = \
|
||||
self._client.get_ticker_price("GOOGL")
|
||||
with vcr.use_cassette('tests/fixtures/ticker_metadata.yaml'):
|
||||
self._ticker_metadata_response = \
|
||||
self._client.get_ticker_metadata("GOOGL")
|
||||
|
||||
def test_ticker_price(self):
|
||||
"""Test the EOD Prices Endpoint"""
|
||||
@@ -44,17 +50,19 @@ class TestTickerPrices(TestCase):
|
||||
|
||||
def test_ticker_price_with_date(self):
|
||||
"""Test the EOD Prices Endpoint with data param"""
|
||||
prices = self._client.get_ticker_price("GOOGL",
|
||||
startDate="2015-01-01",
|
||||
endDate="2015-01-05")
|
||||
with vcr.use_cassette('tests/fixtures/ticker_price_with_date.yaml'):
|
||||
prices = self._client.get_ticker_price("GOOGL",
|
||||
startDate="2015-01-01",
|
||||
endDate="2015-01-05")
|
||||
self.assertGreater(len(prices), 1)
|
||||
|
||||
def test_ticker_price_with_csv(self):
|
||||
"""Confirm that CSV endpoint works"""
|
||||
prices_csv = self._client.get_ticker_price("GOOGL",
|
||||
startDate="2015-01-01",
|
||||
endDate="2015-01-05",
|
||||
fmt='csv')
|
||||
with vcr.use_cassette('tests/fixtures/ticker_price_with_date_csv.yaml'):
|
||||
prices_csv = self._client.get_ticker_price("GOOGL",
|
||||
startDate="2015-01-01",
|
||||
endDate="2015-01-05",
|
||||
fmt='csv')
|
||||
reader = csv.reader(prices_csv.splitlines(), delimiter=",")
|
||||
rows = list(reader)
|
||||
assert len(rows) > 2 # more than 1 day of data
|
||||
@@ -66,7 +74,8 @@ class TestTickerPrices(TestCase):
|
||||
|
||||
def test_list_stock_tickers(self):
|
||||
"""Update this test when the method is added."""
|
||||
tickers = self._client.list_stock_tickers()
|
||||
with vcr.use_cassette('tests/fixtures/list_stock_tickers.yaml'):
|
||||
tickers = self._client.list_stock_tickers()
|
||||
assert len(tickers) > 1
|
||||
assert all(ticker['assetType'] == 'Stock' for ticker in tickers)
|
||||
|
||||
@@ -90,7 +99,7 @@ class TestNews(TestCase):
|
||||
|
||||
def test_get_news_articles(self):
|
||||
"""Confirm that news article work"""
|
||||
NUM_ARTICLES = 10
|
||||
NUM_ARTICLES = 1
|
||||
|
||||
search_params = {
|
||||
"tickers": ["aapl", "googl"],
|
||||
@@ -101,7 +110,8 @@ class TestNews(TestCase):
|
||||
"limit": NUM_ARTICLES
|
||||
}
|
||||
|
||||
articles = self._client.get_news(**search_params)
|
||||
with vcr.use_cassette('tests/fixtures/news.yaml'):
|
||||
articles = self._client.get_news(**search_params)
|
||||
assert len(articles) == NUM_ARTICLES
|
||||
for article in articles:
|
||||
assert all(key in article for key in self.article_keys)
|
||||
@@ -109,13 +119,15 @@ class TestNews(TestCase):
|
||||
def test_get_news_bulk(self):
|
||||
"""Fails because this API key lacks institutional license"""
|
||||
|
||||
with self.assertRaises(RestClientError):
|
||||
with self.assertRaises(RestClientError),\
|
||||
vcr.use_cassette('tests/fixtures/news_bulk.yaml'):
|
||||
value = self._client.get_bulk_news(file_id="1")
|
||||
assert value
|
||||
|
||||
def test_get_news_bulk_ids(self):
|
||||
"""Fails because this API key lacks institutional license"""
|
||||
|
||||
with self.assertRaises(RestClientError):
|
||||
with self.assertRaises(RestClientError),\
|
||||
vcr.use_cassette('tests/fixtures/news_bulk_file_ids.yaml'):
|
||||
value = self._client.get_bulk_news()
|
||||
assert value
|
||||
|
||||
Reference in New Issue
Block a user