1
1
import numpy as np
2
- from nose .tools import (
3
- assert_equals ,
4
- assert_true ,
5
- assert_false ,
6
- assert_raises ,
7
- )
2
+ import pytest
8
3
9
4
try :
10
5
from collections import OrderedDict
@@ -24,20 +19,20 @@ def test_pandas():
24
19
25
20
expected_k , expected_d = func .STOCH (ford_2012 ['high' ], ford_2012 ['low' ], ford_2012 ['close' ]) # 5, 3, 0, 3, 0
26
21
output = abstract .Function ('stoch' , input_df ).outputs
27
- assert_true ( isinstance (output , pandas .DataFrame ) )
22
+ assert isinstance (output , pandas .DataFrame )
28
23
assert_np_arrays_equal (expected_k , output ['slowk' ])
29
24
assert_np_arrays_equal (expected_d , output ['slowd' ])
30
25
output = abstract .Function ('stoch' , input_dict ).outputs
31
- assert_true ( isinstance (output , list ) )
26
+ assert isinstance (output , list )
32
27
assert_np_arrays_equal (expected_k , output [0 ])
33
28
assert_np_arrays_equal (expected_d , output [1 ])
34
29
35
30
expected = func .SMA (ford_2012 ['close' ], 10 )
36
31
output = abstract .Function ('sma' , input_df , 10 ).outputs
37
- assert_true ( isinstance (output , pandas .Series ) )
32
+ assert isinstance (output , pandas .Series )
38
33
assert_np_arrays_equal (expected , output )
39
34
output = abstract .Function ('sma' , input_dict , 10 ).outputs
40
- assert_true ( isinstance (output , np .ndarray ) )
35
+ assert isinstance (output , np .ndarray )
41
36
assert_np_arrays_equal (expected , output )
42
37
43
38
@@ -109,12 +104,13 @@ def test_doji_candle():
109
104
110
105
def test_MAVP ():
111
106
mavp = abstract .MAVP
112
- assert_raises (Exception , mavp .set_input_arrays , ford_2012 )
107
+ with pytest .raises (Exception ):
108
+ mavp .set_input_arrays (ford_2012 )
113
109
input_d = {}
114
110
input_d ['close' ] = ford_2012 ['close' ]
115
111
input_d ['periods' ] = np .arange (30 )
116
- assert_true ( mavp .set_input_arrays (input_d ) )
117
- assert_equals ( mavp .input_arrays , input_d )
112
+ assert mavp .set_input_arrays (input_d )
113
+ assert mavp .input_arrays == input_d
118
114
119
115
def test_info ():
120
116
stochrsi = abstract .Function ('STOCHRSI' )
@@ -138,7 +134,7 @@ def test_info():
138
134
('fastd_matype' , 1 ),
139
135
]),
140
136
}
141
- assert_equals ( expected , stochrsi .info )
137
+ assert expected == stochrsi .info
142
138
143
139
expected = {
144
140
'display_name' : 'Bollinger Bands' ,
@@ -159,11 +155,11 @@ def test_info():
159
155
('matype' , 0 ),
160
156
]),
161
157
}
162
- assert_equals ( expected , abstract .Function ('BBANDS' ).info )
158
+ assert expected == abstract .Function ('BBANDS' ).info
163
159
164
160
def test_input_names ():
165
161
expected = OrderedDict ([('price' , 'close' )])
166
- assert_equals ( expected , abstract .Function ('MAMA' ).input_names )
162
+ assert expected == abstract .Function ('MAMA' ).input_names
167
163
168
164
# test setting input_names
169
165
obv = abstract .Function ('OBV' )
@@ -172,16 +168,17 @@ def test_input_names():
172
168
('prices' , ['volume' ]),
173
169
])
174
170
obv .input_names = expected
175
- assert_equals ( obv .input_names , expected )
171
+ assert obv .input_names == expected
176
172
177
173
obv .input_names = {
178
174
'price' : 'open' ,
179
175
'prices' : ['volume' ],
180
176
}
181
- assert_equals ( obv .input_names , expected )
177
+ assert obv .input_names == expected
182
178
183
179
def test_input_arrays ():
184
180
mama = abstract .Function ('MAMA' )
181
+
185
182
# test default setting
186
183
expected = {
187
184
'open' : None ,
@@ -190,33 +187,35 @@ def test_input_arrays():
190
187
'close' : None ,
191
188
'volume' : None ,
192
189
}
193
- assert_equals (expected , mama .get_input_arrays ())
190
+ assert expected == mama .get_input_arrays ()
191
+
194
192
# test setting/getting input_arrays
195
- assert_true ( mama .set_input_arrays (ford_2012 ) )
196
- assert_equals ( mama .get_input_arrays (), ford_2012 )
197
- assert_raises (Exception ,
198
- mama .set_input_arrays , {'hello' : 'fail' , 'world' : 'bye' })
193
+ assert mama .set_input_arrays (ford_2012 )
194
+ assert mama .get_input_arrays () == ford_2012
195
+ with pytest . raises (Exception ):
196
+ mama .set_input_arrays ( {'hello' : 'fail' , 'world' : 'bye' })
199
197
200
198
# test only required keys are needed
201
199
willr = abstract .Function ('WILLR' )
202
200
reqd = willr .input_names ['prices' ]
203
201
input_d = dict ([(key , ford_2012 [key ]) for key in reqd ])
204
- assert_true ( willr .set_input_arrays (input_d ) )
205
- assert_equals ( willr .input_arrays , input_d )
202
+ assert willr .set_input_arrays (input_d )
203
+ assert willr .input_arrays == input_d
206
204
207
205
# test extraneous keys are ignored
208
206
input_d ['extra_stuffs' ] = 'you should never see me'
209
207
input_d ['date' ] = np .random .rand (100 )
210
- assert_true ( willr .set_input_arrays (input_d ) )
208
+ assert willr .set_input_arrays (input_d )
211
209
212
210
# test missing keys get detected
213
211
input_d ['open' ] = ford_2012 ['open' ]
214
212
input_d .pop ('close' )
215
- assert_raises (Exception , willr .set_input_arrays , input_d )
213
+ with pytest .raises (Exception ):
214
+ willr .set_input_arrays (input_d )
216
215
217
216
# test changing input_names on the Function
218
217
willr .input_names = {'prices' : ['high' , 'low' , 'open' ]}
219
- assert_true ( willr .set_input_arrays (input_d ) )
218
+ assert willr .set_input_arrays (input_d )
220
219
221
220
def test_parameters ():
222
221
stoch = abstract .Function ('STOCH' )
@@ -227,34 +226,34 @@ def test_parameters():
227
226
('slowd_period' , 3 ),
228
227
('slowd_matype' , 0 ),
229
228
])
230
- assert_equals ( expected , stoch .parameters )
229
+ assert expected == stoch .parameters
231
230
232
231
stoch .parameters = {'fastk_period' : 10 }
233
232
expected ['fastk_period' ] = 10
234
- assert_equals ( expected , stoch .parameters )
233
+ assert expected == stoch .parameters
235
234
236
235
stoch .parameters = {'slowk_period' : 8 , 'slowd_period' : 5 }
237
236
expected ['slowk_period' ] = 8
238
237
expected ['slowd_period' ] = 5
239
- assert_equals ( expected , stoch .parameters )
238
+ assert expected == stoch .parameters
240
239
241
240
stoch .parameters = {'slowd_matype' : talib .MA_Type .T3 }
242
241
expected ['slowd_matype' ] = 8
243
- assert_equals ( expected , stoch .parameters )
242
+ assert expected == stoch .parameters
244
243
245
244
stoch .parameters = {
246
245
'slowk_matype' : talib .MA_Type .WMA ,
247
246
'slowd_matype' : talib .MA_Type .EMA ,
248
247
}
249
248
expected ['slowk_matype' ] = 2
250
249
expected ['slowd_matype' ] = 1
251
- assert_equals ( expected , stoch .parameters )
250
+ assert expected == stoch .parameters
252
251
253
252
def test_lookback ():
254
- assert_equals ( abstract .Function ('SMA' , 10 ).lookback , 9 )
253
+ assert abstract .Function ('SMA' , 10 ).lookback == 9
255
254
256
255
stochrsi = abstract .Function ('stochrsi' , 20 , 5 , 3 )
257
- assert_equals ( stochrsi .lookback , 26 )
256
+ assert stochrsi .lookback == 26
258
257
259
258
def test_call_supports_same_signature_as_func_module ():
260
259
adx = abstract .Function ('ADX' )
@@ -263,36 +262,26 @@ def test_call_supports_same_signature_as_func_module():
263
262
output = adx (ford_2012 ['open' ], ford_2012 ['high' ], ford_2012 ['low' ])
264
263
assert_np_arrays_equal (expected , output )
265
264
266
- with assert_raises (TypeError ) as e :
265
+ with pytest . raises (TypeError ):
267
266
adx (ford_2012 ['open' ], ford_2012 ['high' ], ford_2012 ['low' ], ford_2012 ['close' ])
268
- assert 'Too many price arguments: expected 3 (open, high, low)' in str (
269
- e .exception .message )
270
267
271
- with assert_raises (TypeError ) as e :
268
+ with pytest . raises (TypeError ):
272
269
adx (ford_2012 ['open' ], ford_2012 ['high' ])
273
- assert 'Not enough price arguments: expected 3 (open, high, low)' in str (
274
- e .exception .message )
275
270
276
271
def test_parameter_type_checking ():
277
272
sma = abstract .Function ('SMA' , timeperiod = 10 )
278
273
279
- expected_error = 'Invalid parameter value for timeperiod (expected int, got float)'
280
-
281
- with assert_raises (TypeError ) as e :
274
+ with pytest .raises (TypeError ):
282
275
sma (ford_2012 ['close' ], 35.5 )
283
- assert expected_error in str (e .exception .message )
284
276
285
- with assert_raises (TypeError ) as e :
277
+ with pytest . raises (TypeError ):
286
278
abstract .Function ('SMA' , timeperiod = 35.5 )
287
- assert expected_error in str (e .exception .message )
288
279
289
- with assert_raises (TypeError ) as e :
280
+ with pytest . raises (TypeError ):
290
281
sma .parameters = {'timeperiod' : 35.5 }
291
- assert expected_error in str (e .exception .message )
292
282
293
- with assert_raises (TypeError ) as e :
283
+ with pytest . raises (TypeError ):
294
284
sma .set_parameters (timeperiod = 35.5 )
295
- assert expected_error in str (e .exception .message )
296
285
297
286
def test_call_doesnt_cache_parameters ():
298
287
sma = abstract .Function ('SMA' , timeperiod = 10 )
@@ -310,10 +299,8 @@ def test_call_doesnt_cache_parameters():
310
299
assert_np_arrays_equal (expected , output )
311
300
312
301
def test_call_without_arguments ():
313
- with assert_raises (TypeError ) as e :
302
+ with pytest . raises (TypeError ):
314
303
abstract .Function ('SMA' )()
315
- assert 'Not enough price arguments' in str (e .exception .message )
316
304
317
- with assert_raises (TypeError ) as e :
305
+ with pytest . raises (TypeError ):
318
306
abstract .Function ('SMA' )(10 )
319
- assert 'Not enough price arguments' in str (e .exception .message )
0 commit comments