/
test_alphavantage.py
217 lines (197 loc) · 9.86 KB
/
test_alphavantage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from ..alpha_vantage.alphavantage import AlphaVantage
from ..alpha_vantage.timeseries import TimeSeries
from ..alpha_vantage.techindicators import TechIndicators
from ..alpha_vantage.sectorperformance import SectorPerformances
from ..alpha_vantage.cryptocurrencies import CryptoCurrencies
from ..alpha_vantage.foreignexchange import ForeignExchange
from pandas import DataFrame as df, Timestamp
import unittest
import sys
from os import path
import requests
import requests_mock
class TestAlphaVantage(unittest.TestCase):
_API_KEY_TEST = "test"
_API_EQ_NAME_TEST = 'MSFT'
@staticmethod
def get_file_from_url(url):
"""
Return the file name used for testing, found in the test data folder
formed using the original url
"""
tmp = url
for ch in [':', '/', '.', '?', '=', '&', ',']:
if ch in tmp:
tmp = tmp.replace(ch, '_')
path_dir = path.join(path.dirname(
path.abspath(__file__)), 'test_data/')
return path.join(path.join(path_dir, tmp))
def test_key_none(self):
"""Raise an error when a key has not been given
"""
try:
AlphaVantage()
self.fail(msg='A None api key must raise an error')
except ValueError:
self.assertTrue(True)
@requests_mock.Mocker()
def test_handle_api_call(self, mock_request):
""" Test that api call returns a json file as requested
"""
av = AlphaVantage(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=test"
path_file = self.get_file_from_url("mock_time_series")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data = av._handle_api_call(url)
self.assertIsInstance(
data, dict, 'Result Data must be a dictionary')
@requests_mock.Mocker()
def test_time_series_intraday(self, mock_request):
""" Test that api call returns a json file as requested
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&outputsize=full&apikey=test&datatype=json"
path_file = self.get_file_from_url("mock_time_series")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_intraday(
"MSFT", interval='1min', outputsize='full')
self.assertIsInstance(
data, dict, 'Result Data must be a dictionary')
@requests_mock.Mocker()
def test_time_series_intraday_pandas(self, mock_request):
""" Test that api call returns a json file as requested
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&outputsize=full&apikey=test&datatype=json"
path_file = self.get_file_from_url("mock_time_series")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_intraday(
"MSFT", interval='1min', outputsize='full')
self.assertIsInstance(
data, df, 'Result Data must be a pandas data frame')
@requests_mock.Mocker()
def test_time_series_intraday_date_indexing(self, mock_request):
""" Test that api call returns a pandas data frame with a date as index
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas', indexing_type='date')
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&outputsize=full&apikey=test&datatype=json"
path_file = self.get_file_from_url("mock_time_series")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_intraday(
"MSFT", interval='1min', outputsize='full')
if ts.indexing_type == 'date':
assert isinstance(data.index[0], Timestamp)
else:
if sys.version_info[0] == 3:
assert isinstance(data.index[0], str)
else:
assert isinstance(data.index[0], basestring)
@requests_mock.Mocker()
def test_time_series_intraday_date_integer(self, mock_request):
""" Test that api call returns a pandas data frame with an integer as index
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas', indexing_type='integer')
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&outputsize=full&apikey=test&datatype=json"
path_file = self.get_file_from_url("mock_time_series")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_intraday(
"MSFT", interval='1min', outputsize='full')
assert type(data.index[0]) == int
@requests_mock.Mocker()
def test_technical_indicator_sma_python3(self, mock_request):
""" Test that api call returns a json file as requested
"""
ti = TechIndicators(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=15min&time_period=10&series_type=close&apikey=test"
path_file = self.get_file_from_url("mock_technical_indicator")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ti.get_sma("MSFT", interval='15min',
time_period=10, series_type='close')
self.assertIsInstance(
data, dict, 'Result Data must be a dictionary')
@requests_mock.Mocker()
def test_technical_indicator_sma_pandas(self, mock_request):
""" Test that api call returns a json file as requested
"""
ti = TechIndicators(
key=TestAlphaVantage._API_KEY_TEST, output_format='pandas')
url = "https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=15min&time_period=10&series_type=close&apikey=test"
path_file = self.get_file_from_url("mock_technical_indicator")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ti.get_sma("MSFT", interval='15min',
time_period=10, series_type='close')
self.assertIsInstance(
data, df, 'Result Data must be a pandas data frame')
@requests_mock.Mocker()
def test_sector_perfomance_python3(self, mock_request):
""" Test that api call returns a json file as requested
"""
sp = SectorPerformances(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=SECTOR&apikey=test"
path_file = self.get_file_from_url("mock_sector")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = sp.get_sector()
self.assertIsInstance(
data, dict, 'Result Data must be a dictionary')
@requests_mock.Mocker()
def test_sector_perfomance_pandas(self, mock_request):
""" Test that api call returns a json file as requested
"""
sp = SectorPerformances(
key=TestAlphaVantage._API_KEY_TEST, output_format='pandas')
url = "https://www.alphavantage.co/query?function=SECTOR&apikey=test"
path_file = self.get_file_from_url("mock_sector")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = sp.get_sector()
self.assertIsInstance(
data, df, 'Result Data must be a pandas data frame')
@requests_mock.Mocker()
def test_foreign_exchange(self, mock_request):
""" Test that api call returns a json file as requested
"""
fe = ForeignExchange(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=CNY&apikey=test"
path_file = self.get_file_from_url("mock_foreign_exchange")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = fe.get_currency_exchange_rate(
from_currency='BTC', to_currency='CNY')
self.assertIsInstance(
data, dict, 'Result Data must be a dictionary')
@requests_mock.Mocker()
def test_batch_quotes(self, mock_request):
""" Test that api call returns a json file as requested
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST)
url = "https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT,FB,AAPL&apikey=test"
path_file = self.get_file_from_url("mock_batch_quotes")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_batch_stock_quotes(symbols=('MSFT', 'FB', 'AAPL'))
self.assertIsInstance(
data[0], dict, 'Result Data must be a json dictionary')
@requests_mock.Mocker()
def test_batch_quotes_pandas(self, mock_request):
""" Test that api call returns a json file as requested
"""
ts = TimeSeries(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
url = "https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT,FB,AAPL&apikey=test&datatype=json"
path_file = self.get_file_from_url("mock_batch_quotes")
with open(path_file) as f:
mock_request.get(url, text=f.read())
data, _ = ts.get_batch_stock_quotes(symbols=('MSFT', 'FB', 'AAPL'))
self.assertIsInstance(
data, df, 'Result Data must be a pandas dataframe')