-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathtest_td_api.py
125 lines (107 loc) · 3.62 KB
/
test_td_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
"""
Test file for:
Tradier Extract Data
"""
import json
import requests
import analysis_engine.consts as ae_consts
import analysis_engine.options_dates as opt_dates
import analysis_engine.url_helper as url_helper
import analysis_engine.td.consts as td_consts
import analysis_engine.api_requests as api_requests
import analysis_engine.mocks.base_test as base_test
import analysis_engine.td.fetch_data as td_fetch
import spylunking.log.setup_logging as log_utils
log = log_utils.build_colorized_logger(name=__name__)
class TestTDAPI(base_test.BaseTestCase):
"""TestTDAPI"""
def setUp(self):
"""setUp"""
self.ticker = ae_consts.TICKER
# end of setUp
"""
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_account_credentials(self):
"""test_integration_account_credentials"""
if ae_consts.ev('INT_TESTS', '0') == '0':
return
headers = td_consts.get_auth_headers()
session = requests.Session()
session.headers = headers
self.exp_date = opt_dates.option_expiration().strftime(
ae_consts.COMMON_DATE_FORMAT)
use_url = td_consts.TD_URLS['options'].format(
self.ticker,
self.exp_date)
response = url_helper.url_helper(sess=session).get(
use_url
)
self.assertEqual(
response.status_code,
200)
self.assertTrue(
len(json.loads(response.text)) > 0)
# end of test_integration_account_credentials
def test_integration_fetch_calls_dataset(self):
"""test_integration_fetch_calls_dataset"""
if ae_consts.ev('INT_TESTS', '0') == '0':
return
ticker = 'SPY'
label = 'TD calls dataset'
# build dataset cache dictionary
work = api_requests.get_ds_dict(
ticker=ticker,
label=label)
status, df = td_fetch.fetch_data(
work_dict=work,
fetch_type='tdcalls')
if status == ae_consts.SUCCESS:
self.assertIsNotNone(
df)
self.debug_df(df=df)
else:
log.critical(
f'{label} is missing in redis '
f'for ticker={work["ticker"]} '
f'status={ae_consts.get_status(status=status)}')
# end of test_integration_fetch_calls_dataset
def test_integration_fetch_puts_dataset(self):
"""test_integration_fetch_puts_dataset"""
if ae_consts.ev('INT_TESTS', '0') == '0':
return
ticker = 'SPY'
label = 'TD puts dataset'
# build dataset cache dictionary
work = api_requests.get_ds_dict(
ticker=ticker,
label=label)
status, df = td_fetch.fetch_data(
work_dict=work,
fetch_type='tdputs')
if status == ae_consts.SUCCESS:
self.assertIsNotNone(
df)
self.debug_df(df=df)
else:
log.critical(
f'{label} is missing in redis '
f'for ticker={work["ticker"]} '
f'status={ae_consts.get_status(status=status)}')
# end of test_integration_fetch_puts_dataset
# end of TestTDAPI