Skip to content

Commit 95db5ac

Browse files
more tests
1 parent 6a14ade commit 95db5ac

File tree

3 files changed

+82
-19
lines changed

3 files changed

+82
-19
lines changed

src/mplfinance/_arg_validators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ def _process_kwargs(kwargs, vkwargs):
108108
try:
109109
valid = vkwargs[key]['Validator'](value)
110110
except Exception as ex:
111-
raise ValueError('kwarg "'+key+'" validator raised exception to value: "'+str(value)+'"') from ex
111+
ex.extra_info = 'kwarg "'+key+'" validator raised exception to value: "'+str(value)+'"'
112+
raise
112113
if not valid:
113114
import inspect
114115
v = inspect.getsource(vkwargs[key]['Validator']).strip()
115-
raise ValueError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
116+
raise TypeError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
116117

117118
# ---------------------------------------------------------------
118119
# At this point in the loop, if we have not raised an exception,

tests/test_exceptions.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os as os
2+
import io as io
3+
import pandas as pd
4+
import mplfinance as mpf
5+
import matplotlib.pyplot as plt
6+
from matplotlib.testing.compare import compare_images
7+
8+
print('pd.__version__ =',pd.__version__ ) # for the record
9+
print('mpf.__version__ =',mpf.__version__) # for the record
10+
print("plt.rcParams['backend'] =",plt.rcParams['backend']) # for the record
11+
12+
import pytest
13+
14+
def test_dataframe_typeErr():
15+
s = pd.Series([0,1,2])
16+
with pytest.raises(TypeError) as ex:
17+
mpf.plot(s)
18+
assert 'Expect data as DataFrame' in str(ex.value)
19+
20+
df = pd.DataFrame()
21+
with pytest.raises(TypeError) as ex:
22+
mpf.plot(df)
23+
assert 'Expect data.index as DatetimeIndex' in str(ex.value)
24+
25+
def test_kwarg_not_implemented(bolldata):
26+
df = bolldata
27+
with pytest.raises(NotImplementedError) as ex:
28+
mpf.plot(df,volume=True,study='Bollinger')
29+
assert 'kwarg NOT implemented' in str(ex.value)
30+
31+
def test_unrecognized_kwarg(bolldata):
32+
df = bolldata
33+
with pytest.raises(KeyError) as ex:
34+
mpf.plot(df,volume=True,foo='bar')
35+
assert 'Unrecognized kwarg' in str(ex.value)
36+
37+
def test_kwarg_validation_error(bolldata):
38+
'''
39+
We *could* very exhaustively test all kwargs, but that
40+
would be hundreds of lines of code, etc. So just going
41+
to more-or-less randomly spot check just a few.
42+
'''
43+
df = bolldata
44+
45+
apdict = mpf.make_addplot(df['LowerB'])
46+
with pytest.raises(TypeError) as ex:
47+
mpf.plot(df,volume='True',addplot=apdict)
48+
assert 'validator returned False for value' in str(ex.value)
49+
50+
apdict = {'data':df['LowerB']}
51+
with pytest.raises(KeyError) as ex:
52+
mpf.plot(df,volume=True,addplot=apdict)
53+
54+
with pytest.raises(TypeError) as ex:
55+
mpf.plot(df,volume=True,style='some random style name')
56+
assert 'validator returned False for value' in str(ex.value)
57+
58+
with pytest.raises(ValueError) as ex:
59+
mpf.make_marketcolors(base_mpf_style='classic',ohlc='chartreussse')
60+
assert 'NOT is_color_like' in str(ex.value)
61+
62+
def test_renko_addplot(bolldata):
63+
df = bolldata
64+
apdict = mpf.make_addplot(df['LowerB'])
65+
with pytest.raises(ValueError) as ex:
66+
mpf.plot(df,type='renko',volume=True,addplot=apdict)
67+
assert '`addplot` is not supported for `type=\'renko\'`' in str(ex.value)
68+
#mpf.plot(df,type='renko',volume=True)
69+
70+
def test_figratio_bounds(bolldata):
71+
df = bolldata
72+
buf = io.BytesIO()
73+
mpf.plot(df,volume=True,figratio=(10,5),savefig=buf)
74+
with pytest.raises(ValueError) as ex:
75+
mpf.plot(df,volume=True,figratio=(11,2),savefig=buf)
76+
assert '"figratio" (aspect ratio) must be between' in str(ex.value)
77+
with pytest.raises(ValueError) as ex:
78+
mpf.plot(df,volume=True,figratio=(10,41),savefig=buf)
79+
assert '"figratio" (aspect ratio) must be between' in str(ex.value)

tests/test_raise.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)