-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathtest_finviz_fetch_api.py
274 lines (249 loc) · 8.48 KB
/
test_finviz_fetch_api.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Test file for:
FinViz Fetch API
"""
import mock
from analysis_engine.mocks.base_test import BaseTestCase
from analysis_engine.consts import ev
from analysis_engine.consts import get_status
from analysis_engine.finviz.fetch_api \
import fetch_tickers_from_screener
class MockResponse:
"""MockResponse"""
def __init__(
self):
self.text = None
self.status_code = 200
# end of __init__
# end of MockResponse
def mock_request_get(
url):
"""mock_request_get_success
:param url: url to test
"""
res = MockResponse()
if 'success' in url:
res.status_code = 200
res.text = (
'<html><body>'
'<table>'
'<tbody>'
'<tr>'
'<td class="screener-body-table-nw">1</td>'
'<td class="screener-body-table-nw">QS</td>'
'<td class="screener-body-table-nw">Quant Solutions</td>'
'<td class="screener-body-table-nw">Financial</td>'
'<td class="screener-body-table-nw">Quant Engines</td>'
'<td class="screener-body-table-nw">USA</td>'
'<td class="screener-body-table-nw">1.1B</td>' # mcap
'<td class="screener-body-table-nw">30.01</td>' # p/e
'<td class="screener-body-table-nw">45.01</td>' # prc
'<td class="screener-body-table-nw">3.05%</td>' # chg
'<td class="screener-body-table-nw">177,777</td>' # vol
'</tr>'
'<tr>'
'<td class="screener-body-table-nw">2</td>'
'<td class="screener-body-table-nw">SPY</td>'
'<td class="screener-body-table-nw">Spyder</td>'
'<td class="screener-body-table-nw">Financial</td>'
'<td class="screener-body-table-nw">ETF</td>'
'<td class="screener-body-table-nw">USA</td>'
'<td class="screener-body-table-nw">-</td>'
'<td class="screener-body-table-nw">-</td>'
'<td class="screener-body-table-nw">280.40</td>'
'<td class="screener-body-table-nw">2.05%</td>'
'<td class="screener-body-table-nw">117,872,097</td>'
'</tr>'
'<tr>'
'<td class="screener-body-table-nw">3</td>'
'<td class="screener-body-table-nw">VXX</td>'
'<td class="screener-body-table-nw">iPath S&P 500 VIX</td>'
'<td class="screener-body-table-nw">Financial</td>'
'<td class="screener-body-table-nw">ETF</td>'
'<td class="screener-body-table-nw">USA</td>'
'<td class="screener-body-table-nw">-</td>'
'<td class="screener-body-table-nw">-</td>'
'<td class="screener-body-table-nw">31.98</td>'
'<td class="screener-body-table-nw">-7.33%</td>'
'<td class="screener-body-table-nw">53,700,383</td>'
'</tr>'
'</tbody>'
'</table>'
'</body>'
'</html>')
elif 'empty' in url:
res.status_code = 200
res.text = (
'<html><body>'
'<table>'
'<tr>'
'</tr>'
'</table>'
'</body>'
'</html>')
elif 'failure' in url:
res.status_code = 500
res.text = (
'<html><body>'
'error'
'</body>'
'</html>')
elif 'exception' in url:
raise Exception(f'mock_request_get - threw for url={url}')
return res
# end of mock_request_get
class TestFinVizFetchAPI(BaseTestCase):
"""TestFinVizFetchAPI"""
@mock.patch(
('requests.get'),
new=mock_request_get)
def test_fetch_tickers_from_screener_success(self):
"""test_fetch_tickers_from_screener_success"""
url = (
'success-'
'https://finviz.com/screener.ashx?'
'v=111&'
'f=an_recom_strongbuy,'
'exch_nyse,fa_ltdebteq_low,fa_sales5years_o10&'
'ft=4')
res = fetch_tickers_from_screener(
url=url)
self.assertIsNotNone(
res)
self.assertTrue(
len(res['rec']['data']) > 0)
self.assertEqual(
get_status(status=res['status']),
'SUCCESS')
self.assertEqual(
res['rec']['data']['ticker'][0],
'QS')
self.assertEqual(
res['rec']['data']['ticker'][1],
'SPY')
self.assertEqual(
res['rec']['data']['ticker'][2],
'VXX')
self.assertEqual(
res['rec']['tickers'][0],
'QS')
self.assertEqual(
res['rec']['tickers'][1],
'SPY')
self.assertEqual(
res['rec']['tickers'][2],
'VXX')
# end of test_fetch_tickers_from_screener_success
@mock.patch(
('requests.get'),
new=mock_request_get)
def test_fetch_tickers_from_screener_empty_data(self):
"""test_fetch_tickers_from_screener_empty_data"""
url = (
'empty-'
'https://finviz.com/screener.ashx?'
'v=111&'
'f=an_recom_strongbuy,'
'exch_nyse,fa_ltdebteq_low,fa_sales5years_o10&'
'ft=4')
res = fetch_tickers_from_screener(
url=url)
self.assertIsNotNone(
res)
self.assertTrue(
len(res['rec']['data']) == 0)
self.assertEqual(
get_status(status=res['status']),
'SUCCESS')
# end of test_fetch_tickers_from_screener_empty_data
@mock.patch(
('requests.get'),
new=mock_request_get)
def test_fetch_tickers_from_screener_failure_data(self):
"""test_fetch_tickers_from_screener_failure_data"""
url = (
'failure-'
'https://finviz.com/screener.ashx?'
'v=111&'
'f=an_recom_strongbuy,'
'exch_nyse,fa_ltdebteq_low,fa_sales5years_o10&'
'ft=4')
res = fetch_tickers_from_screener(
url=url)
self.assertIsNotNone(
res)
self.assertEqual(
get_status(status=res['status']),
'ERR')
self.assertTrue(
'finviz returned non-ok HTTP' in res['err'])
self.assertIsNone(
res['rec']['data'])
# end of test_fetch_tickers_from_screener_failure_data
@mock.patch(
('requests.get'),
new=mock_request_get)
def test_fetch_tickers_from_screener_exception(self):
"""test_fetch_tickers_from_screener_exception"""
url = (
'exception-'
'https://finviz.com/screener.ashx?'
'v=111&'
'f=an_recom_strongbuy,'
'exch_nyse,fa_ltdebteq_low,fa_sales5years_o10&'
'ft=4')
res = fetch_tickers_from_screener(
url=url)
self.assertIsNotNone(
res)
self.assertEqual(
get_status(status=res['status']),
'EX')
self.assertIsNone(
res['rec']['data'])
# end of test_fetch_tickers_from_screener_exception
"""
Integration Tests
Please ensure redis and minio are running and run this:
::
export INT_TESTS=1
"""
def debug_df(
self,
df):
"""debug_df
:param df: ``pandas.DataFrame`` from a fetch
"""
print('-----------------------------------')
print(f'dataframe: {df}')
print('')
print(f'dataframe columns:\n{df.columns.values}')
print('-----------------------------------')
# end of debug_df
def test_integration_test_fetch_tickers_from_screener(self):
"""test_integration_test_fetch_tickers_from_screener"""
# 2020-09-05 - Finviz is kicking back:
# 403 - Forbidden: Access is denied
# sounds like they require some kind of auth now
return 0
if ev('INT_TESTS', '0') == '0':
return
default_url = (
'https://finviz.com/screener.ashx?'
'v=111&'
'f=an_recom_strongbuy,'
'exch_nyse,fa_ltdebteq_low,fa_sales5years_o10&'
'ft=4')
url = ev('INT_TEST_FINVIZ_SCREEN_URL', default_url)
res = fetch_tickers_from_screener(
url=url)
self.assertIsNotNone(
res)
self.assertTrue(
len(res['rec']['data']) > 0)
self.assertEqual(
get_status(status=res['status']),
'SUCCESS')
self.debug_df(df=res['rec']['data'])
# end of test_integration_test_fetch_tickers_from_screener
# end of TestFinVizFetchAPI