mirror of
https://github.com/hydrosquall/tiingo-python.git
synced 2025-12-18 20:24:19 +01:00
Add coverage to Tiingo Client for Tickers Endpoint
This commit is contained in:
@@ -2,9 +2,12 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Tests for `tiingo` package."""
|
"""Tests for `tiingo` package."""
|
||||||
|
|
||||||
|
import csv
|
||||||
import pytest
|
import pytest
|
||||||
|
from unittest import TestCase
|
||||||
from tiingo import TiingoClient
|
from tiingo import TiingoClient
|
||||||
|
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
# Add tests for
|
# Add tests for
|
||||||
# Invalid API key
|
# Invalid API key
|
||||||
@@ -16,21 +19,6 @@ from tiingo import TiingoClient
|
|||||||
# Expand test coverage
|
# Expand test coverage
|
||||||
|
|
||||||
|
|
||||||
# Refactor fixtures into separate file
|
|
||||||
@pytest.fixture
|
|
||||||
def ticker_price_response():
|
|
||||||
"""Test /tiingo/<ticker>/prices endpoint"""
|
|
||||||
t = TiingoClient()
|
|
||||||
return t.get_ticker_price("GOOGL")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def ticker_metadata_response():
|
|
||||||
"""Test /tiingo/<ticker> endpoint"""
|
|
||||||
t = TiingoClient()
|
|
||||||
return t.get_ticker_metadata("GOOGL")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def fund_metadata_response():
|
def fund_metadata_response():
|
||||||
"""Test /tiingo/<ticker> endpoint"""
|
"""Test /tiingo/<ticker> endpoint"""
|
||||||
@@ -45,22 +33,62 @@ def fund_metrics_response():
|
|||||||
return t.get_fund_metrics("VFINX")
|
return t.get_fund_metrics("VFINX")
|
||||||
|
|
||||||
|
|
||||||
# PRICES ENDPOINTS
|
def test_client_repr():
|
||||||
class TestTickerPrices(object):
|
"""Test representation of client when logged to console"""
|
||||||
def test_ticker_price(self, ticker_price_response):
|
client = TiingoClient()
|
||||||
"""Test the EOD Prices Endpoint"""
|
base_url = "https://api.tiingo.com"
|
||||||
assert len(ticker_price_response) == 1
|
assert repr(client) == "<TiingoClient(url=\"{}\")>".format(base_url)
|
||||||
assert ticker_price_response[0].get('adjClose')
|
|
||||||
|
|
||||||
def test_ticker_metadata(self, ticker_metadata_response):
|
|
||||||
|
# PRICES ENDPOINTS
|
||||||
|
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")
|
||||||
|
|
||||||
|
def test_ticker_price(self):
|
||||||
|
"""Test the EOD Prices Endpoint"""
|
||||||
|
assert len(self._ticker_price_response) == 1
|
||||||
|
assert self._ticker_price_response[0].get('adjClose')
|
||||||
|
|
||||||
|
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")
|
||||||
|
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')
|
||||||
|
reader = csv.reader(prices_csv.splitlines(), delimiter=",")
|
||||||
|
rows = list(reader)
|
||||||
|
assert len(rows) > 2 # more than 1 day of data
|
||||||
|
|
||||||
|
def test_ticker_metadata(self):
|
||||||
"""Refactor this with python data schemavalidation"""
|
"""Refactor this with python data schemavalidation"""
|
||||||
assert ticker_metadata_response.get('ticker') == "GOOGL"
|
assert self._ticker_metadata_response.get('ticker') == "GOOGL"
|
||||||
assert ticker_metadata_response.get("name")
|
assert self._ticker_metadata_response.get("name")
|
||||||
|
|
||||||
|
def test_list_tickers(self):
|
||||||
|
"""Update this test when the method is added."""
|
||||||
|
with self.assertRaises(NotImplementedError):
|
||||||
|
response = self._client.list_tickers()
|
||||||
|
assert not response
|
||||||
|
|
||||||
|
|
||||||
# FUND ENDPOINTS
|
# FUND ENDPOINTS
|
||||||
|
# tiingo/funds
|
||||||
|
# Try to get a working API key from Tiingo development for testing purposes
|
||||||
@pytest.mark.skip(reason="My API key doesn't have access to mutual funds API")
|
@pytest.mark.skip(reason="My API key doesn't have access to mutual funds API")
|
||||||
class TestMutualFunds(object):
|
class TestMutualFunds(TestCase):
|
||||||
|
|
||||||
def test_fund_metadata(self, fund_metadata_response):
|
def test_fund_metadata(self, fund_metadata_response):
|
||||||
"""Refactor this with python data schemavalidation"""
|
"""Refactor this with python data schemavalidation"""
|
||||||
@@ -72,3 +100,18 @@ class TestMutualFunds(object):
|
|||||||
"""Test Fund Level Metrics"""
|
"""Test Fund Level Metrics"""
|
||||||
assert len(fund_metrics_response) > 0
|
assert len(fund_metrics_response) > 0
|
||||||
assert fund_metrics_response[0].get('managementFee')
|
assert fund_metrics_response[0].get('managementFee')
|
||||||
|
|
||||||
|
|
||||||
|
# News Feed
|
||||||
|
# tiingo/news
|
||||||
|
class TestNews(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self._client = TiingoClient()
|
||||||
|
|
||||||
|
def test_get_news_articles(self):
|
||||||
|
"""Rewrite when method is implemented
|
||||||
|
"""
|
||||||
|
with self.assertRaises(NotImplementedError) as context:
|
||||||
|
self.get_news()
|
||||||
|
print(context)
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class TiingoClient(RestClient):
|
|||||||
if fmt == "json":
|
if fmt == "json":
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
return response.content
|
return response.content.decode("utf-8")
|
||||||
|
|
||||||
# FUND DATA (From over 26,000 mutual funds)
|
# FUND DATA (From over 26,000 mutual funds)
|
||||||
# https://api.tiingo.com/docs/tiingo/funds
|
# https://api.tiingo.com/docs/tiingo/funds
|
||||||
|
|||||||
Reference in New Issue
Block a user