forked from Nasdaq/data-link-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_get_point_in_time_data.py
167 lines (143 loc) · 6.07 KB
/
test_get_point_in_time_data.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
import re
import unittest
import httpretty
import json
import pandas
from mock import patch, call
import nasdaqdatalink
from nasdaqdatalink.utils.request_type_util import RequestType
from nasdaqdatalink.errors.data_link_error import InvalidRequestError
from datetime import date
class GetPointInTimeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
httpretty.enable()
httpretty.register_uri(httpretty.GET,
re.compile('https://data.nasdaq.com/api/v3/pit*'),
body=json.dumps({}))
@classmethod
def tearDownClass(cls):
httpretty.disable()
httpretty.reset()
def tearDown(self):
RequestType.USE_GET_REQUEST = True
@patch('nasdaqdatalink.connection.Connection.request')
def test_get_point_in_time_returns_data_frame_object(self, mock):
with self.assertWarns(UserWarning):
df = nasdaqdatalink.get_point_in_time(
'ZACKS/FC', interval='asofdate', date='2020-01-01'
)
self.assertIsInstance(df, pandas.core.frame.DataFrame)
@patch('nasdaqdatalink.connection.Connection.request')
def test_asofdate_call_connection(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time('ZACKS/FC', interval='asofdate', date='2020-01-01')
expected = call('get', 'pit/ZACKS/FC/asofdate/2020-01-01', params={})
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_asofdate_call_connection_with_datetimes(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time(
'ZACKS/FC', interval='asofdate', date='2020-01-01T12:55'
)
expected = call('get', 'pit/ZACKS/FC/asofdate/2020-01-01T12:55', params={})
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_asofdate_call_without_date(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time('ZACKS/FC', interval='asofdate')
expected = call('get', "pit/ZACKS/FC/asofdate/%s" % date.today(), params={})
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_from_call_connection(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time(
'ZACKS/FC',
interval='from',
start_date='2020-01-01',
end_date='2020-01-02'
)
expected = call('get', 'pit/ZACKS/FC/from/2020-01-01/to/2020-01-02', params={})
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_from_call_connection_with_datetimes(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time(
'ZACKS/FC',
interval='from',
start_date='2020-01-01T12:00',
end_date='2020-01-02T14:00'
)
expected = call(
'get', 'pit/ZACKS/FC/from/2020-01-01T12:00/to/2020-01-02T14:00', params={}
)
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_between_call_connection(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time(
'ZACKS/FC',
interval='between',
start_date='2020-01-01',
end_date='2020-01-02'
)
expected = call('get', 'pit/ZACKS/FC/between/2020-01-01/2020-01-02', params={})
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_between_call_connection_with_datetimes(self, mock):
with self.assertWarns(UserWarning):
nasdaqdatalink.get_point_in_time(
'ZACKS/FC',
interval='between',
start_date='2020-01-01T12:00',
end_date='2020-01-02T14:00'
)
expected = call(
'get', 'pit/ZACKS/FC/between/2020-01-01T12:00/2020-01-02T14:00', params={}
)
self.assertEqual(mock.call_args, expected)
@patch('nasdaqdatalink.connection.Connection.request')
def test_invalid_interval_connection(self, mock):
self.assertRaises(InvalidRequestError, lambda: nasdaqdatalink.get_point_in_time('ZACKS/FC'))
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time('ZACKS/FC', interval='nasdaqdatalink')
)
@patch('nasdaqdatalink.connection.Connection.request')
def test_invalid_from_connection(self, mock):
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time('ZACKS/FC', interval='from')
)
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time(
'ZACKS/FC', interval='from', start_date='2020-01-01'
)
)
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time(
'ZACKS/FC', interval='from', end_date='2020-01-02'
)
)
@patch('nasdaqdatalink.connection.Connection.request')
def test_invalid_between_connection(self, mock):
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time('ZACKS/FC', interval='between')
)
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time(
'ZACKS/FC',
interval='between',
start_date='2020-01-01'
)
)
self.assertRaises(
InvalidRequestError,
lambda: nasdaqdatalink.get_point_in_time(
'ZACKS/FC', interval='between', end_date='2020-01-02'
)
)