|
| 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) |
0 commit comments