How to use mock_dataframe method in pandera

Best Python code snippet using pandera_python

plot_workflow_result_tests.py

Source:plot_workflow_result_tests.py Github

copy

Full Screen

1from unittest import mock2import pandas as pd3from datetime import datetime as dt4from tethysext.atcore.models.resource_workflow_results import PlotWorkflowResult5from tethysext.atcore.tests.utilities.sqlalchemy_helpers import SqlAlchemyTestCase6from tethysext.atcore.tests.utilities.sqlalchemy_helpers import setup_module_for_sqlalchemy_tests, \7 tear_down_module_for_sqlalchemy_tests8import plotly.graph_objs as go9from bokeh.plotting import figure10def setUpModule():11 setup_module_for_sqlalchemy_tests()12def tearDownModule():13 tear_down_module_for_sqlalchemy_tests()14class PlotWorkflowResultTests(SqlAlchemyTestCase):15 def setUp(self):16 super().setUp()17 self.instance = PlotWorkflowResult(name='test')18 def bind_instance_to_session(self):19 self.session.add(self.instance)20 self.session.commit()21 def test_default_options(self):22 baseline = {23 'renderer': 'plotly',24 'axes': [],25 'plot_type': 'lines',26 'axis_labels': ['x', 'y'],27 'line_shape': 'linear',28 'x_axis_type': 'datetime',29 'no_dataset_message': 'No dataset found.'30 }31 self.assertDictEqual(baseline, self.instance.default_options)32 def test_datasets(self):33 self.assertDictEqual({}, self.instance.data)34 ret = self.instance.datasets35 self.assertListEqual([], ret)36 def test_reset(self):37 self.instance.data['datasets'] = 'Bad data to reset'38 self.instance.reset()39 ret = self.instance.data['datasets']40 self.assertEqual([], ret)41 def test__add_dataset(self):42 test_data = 'Test data'43 self.instance._add_dataset(test_data)44 self.assertListEqual([test_data], self.instance.datasets)45 def test__add_plot(self):46 test_data = 'Test plot'47 self.instance._add_plot(test_data)48 self.assertEqual(test_data, self.instance.plot)49 def test_datasets_bound(self):50 self.bind_instance_to_session()51 self.test_datasets()52 def test_reset_datasets_bound(self):53 self.bind_instance_to_session()54 self.test_reset()55 def test__add_dataset_bound(self):56 self.bind_instance_to_session()57 self.test__add_dataset()58 def test_add_series_no_title(self):59 self.assertRaises(ValueError, self.instance.add_series, '', ['test'])60 def test_add_series_no_data(self):61 self.assertRaises(ValueError, self.instance.add_series, 'title', '')62 def test_add_series_empty_data_frame(self):63 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=True)64 self.assertRaises(ValueError, self.instance.add_series, 'title', mock_dataframe)65 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult._add_dataset') # noqa: E50166 def test_add_series(self, mock_add_dataset):67 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=False)68 self.instance.add_series('series title', mock_dataframe)69 baseline = {70 'title': 'series title',71 'dataset': mock_dataframe,72 }73 mock_add_dataset.assert_called_with(baseline)74 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult._add_dataset') # noqa: E50175 def test_plot_from_dataframe(self, mock_add_dataset):76 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=False)77 self.instance.plot_from_dataframe(mock_dataframe)78 baseline = {79 'dataset': mock_dataframe,80 'series_axes': [],81 'series_labels': []82 }83 mock_add_dataset.assert_called_with(baseline)84 def test_plot_from_dataframe_not_dataframe(self):85 self.assertRaises(ValueError, self.instance.plot_from_dataframe, ['test'])86 def test_plot_from_dataframe_empty_dataframe(self):87 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=True)88 self.assertRaises(ValueError, self.instance.plot_from_dataframe, mock_dataframe)89 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult._add_plot') # noqa: E50190 def test_add_plot(self, mock_add_dataset):91 mock_plot = mock.MagicMock(spec=go.Figure, empty=False)92 self.instance.add_plot(mock_plot)93 baseline = {94 'plot_object': mock_plot,95 }96 mock_add_dataset.assert_called_with(baseline)97 def test_add_plot_not_plolty(self):98 self.assertRaises(ValueError, self.instance.add_plot, 'foo')99 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult.options')100 def test_get_plot_object_dict(self, mock_options):101 mock_plotly_plot = mock.MagicMock(spec=go.Figure, empty=False)102 mock_options.get = mock.MagicMock(side_effect=['plotly', 'lines', ['x', 'y'], 'linear', 'linear'])103 self.instance.add_plot(mock_plotly_plot)104 ret_plot = self.instance.get_plot_object()105 self.assertEqual(ret_plot, mock_plotly_plot)106 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult.options')107 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.figure')108 def test_get_plot_object_bokeh_line(self, mock_bokeh_figure, mock_options):109 mock_bokeh_plot = mock.MagicMock(110 spec=figure, xaxis=mock.MagicMock(axis_label='x'), yaxis=mock.MagicMock(axis_label='y'),111 line=mock.MagicMock(return_value='test line type')112 )113 mock_bokeh_figure.return_value = mock_bokeh_plot114 mock_dataframe = pd.DataFrame({115 'x': [dt(2020, 1, 2), dt(2020, 1, 3), dt(2020, 1, 4), dt(2020, 1, 5), dt(2020, 1, 6), dt(2020, 1, 7)],116 'y': [2, 3, 4, 5, 6, 7]117 })118 mock_datasets = [{119 'title': 'Test Title',120 'series_axes': [('x', 'y')],121 'series_labels': ['test_label'],122 'dataset': mock_dataframe123 }]124 mock_options.get = mock.MagicMock(side_effect=['bokeh', 'lines', ['x', 'y'], 'linear', 'linear'])125 self.instance.plot = mock_bokeh_plot126 self.instance.datasets = mock_datasets127 ret_plot = self.instance.get_plot_object()128 self.assertEqual(ret_plot, mock_bokeh_plot)129 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult.options')130 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.figure')131 def test_get_plot_object_bokeh_scatter(self, mock_bokeh_figure, mock_options):132 mock_bokeh_plot = mock.MagicMock(133 spec=figure, xaxis=mock.MagicMock(axis_label='x'), yaxis=mock.MagicMock(axis_label='y'),134 scatter=mock.MagicMock(return_value='test scatter type')135 )136 mock_bokeh_figure.return_value = mock_bokeh_plot137 mock_dataframe = pd.DataFrame(138 {'x': [dt(2020, 1, 2), dt(2020, 1, 3), dt(2020, 1, 4)], 'y': [2, 3, 4]},139 columns=['x', 'y']140 )141 mock_datasets = [{142 'title': 'Test Title',143 'series_axes': [],144 'series_labels': [],145 'dataset': mock_dataframe146 }]147 mock_options.get = mock.MagicMock(side_effect=['bokeh', 'scatter', ['x', 'y'], 'linear', 'linear'])148 self.instance.plot = mock_bokeh_plot149 self.instance.datasets = mock_datasets150 ret_plot = self.instance.get_plot_object()151 self.assertEqual(ret_plot, mock_bokeh_plot)152 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult.options')153 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.go.Figure')154 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.go.Scatter')155 def test_get_plot_object_plotly_line(self, mock_scatter_plot, mock_plotly_figure, mock_options):156 mock_plotly_plot = mock.MagicMock(157 spec=go.Figure, xaxis=mock.MagicMock(axis_label='x'), yaxis=mock.MagicMock(axis_label='y'),158 add_trace=mock.MagicMock(return_value=mock_scatter_plot)159 )160 mock_plotly_figure.return_value = mock_plotly_plot161 mock_dataframe = pd.DataFrame({162 'x': [dt(2020, 1, 2), dt(2020, 1, 3), dt(2020, 1, 4), dt(2020, 1, 5), dt(2020, 1, 6), dt(2020, 1, 7)],163 'y': [2, 3, 4, 5, 6, 7]164 })165 mock_datasets = [{166 'title': 'Test Title',167 'series_axes': [('x', 'y')],168 'series_labels': ['test_label'],169 'dataset': mock_dataframe170 }]171 mock_options.get = mock.MagicMock(side_effect=['plotly', 'lines', ['x', 'y'], 'linear', 'linear'])172 self.instance.plot = mock_plotly_plot173 self.instance.datasets = mock_datasets174 ret_plot = self.instance.get_plot_object()175 self.assertEqual(ret_plot, mock_plotly_plot)176 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.PlotWorkflowResult.options')177 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.go.Figure')178 @mock.patch('tethysext.atcore.models.resource_workflow_results.plot_workflow_result.go.Scatter')179 def test_get_plot_object_plotly_list_dataset(self, mock_scatter_plot, mock_plotly_figure,180 mock_options):181 mock_plotly_plot = mock.MagicMock(182 spec=go.Figure, xaxis=mock.MagicMock(axis_label='x'), yaxis=mock.MagicMock(axis_label='y'),183 add_trace=mock.MagicMock(return_value=mock_scatter_plot)184 )185 mock_plotly_figure.return_value = mock_plotly_plot186 mock_dataset = [187 [dt(2020, 1, 2), dt(2020, 1, 3), dt(2020, 1, 4), dt(2020, 1, 5), dt(2020, 1, 6), dt(2020, 1, 7)],188 [2, 3, 4, 5, 6, 7]189 ]190 mock_datasets = [{191 'title': 'Test Title',192 'series_axes': [('x', 'y')],193 'series_labels': ['test_label'],194 'dataset': mock_dataset195 }]196 mock_options.get = mock.MagicMock(side_effect=['plotly', 'scatter', ['x', 'y'], 'linear', 'linear'])197 self.instance.plot = mock_plotly_plot198 self.instance.datasets = mock_datasets199 ret_plot = self.instance.get_plot_object()...

Full Screen

Full Screen

test_stock_data.py

Source:test_stock_data.py Github

copy

Full Screen

1import os2import unittest3import datetime as dt4import requests_mock5from unittest.mock import Mock, patch, call6import logging7import pandas as pd8import pandas_datareader.data as web9import requests10import scan_data as scan_data11from alpaca import Alpaca12from stock_data import Stock13logging.disable(logging.CRITICAL)14class TestStockData(unittest.TestCase):15 def setUp(self):16 self.mock_stock = Stock('FAKE')17 self.mock_dict = {18 'date': ['2019-11-12', '2019-11-13'],19 'open': [261.55, 261.13],20 'high': [262.79, 264.78],21 'low': [260.92, 261.07],22 'close': [261.96, 264.47],23 'adjusted close': [260.6421, 263.1395],24 'volume': [21847226, 25817593],25 'dividend amount': [0., 0.],26 'split coefficient': [1., 1.],27 }28 self.mock_dataframe = pd.DataFrame.from_dict(data=self.mock_dict)29 self.mock_dataframe.set_index('date')30 ### ------------------- DATA GETTERS TESTS ------------------- ###31 @patch('stock_data.web.DataReader')32 def test_get_trend_data(self, mock_datareader):33 mock_datareader.return_value = self.mock_dataframe34 mock_result_dataframe = self.mock_dataframe.copy()35 mock_result_dataframe['sma50'] = [float('nan'), float('nan')]36 actual_result = self.mock_stock.get_trend_data()37 pd.testing.assert_frame_equal(actual_result, mock_result_dataframe)38 39 @patch.object(Stock, 'get_stochastic')40 @patch('stock_data.TimeSeries')41 def test_get_tactical_data(self, mock_timeseries, mock_get_stochastic):42 mock_timeseries.return_value.get_intraday.return_value = (self.mock_dataframe, '')43 mock_result_dataframe = self.mock_dataframe.copy()44 mock_result_dataframe.rename(lambda s: ' ' + s, axis=1, inplace=True)45 mock_result_dataframe['fast k'] = [float('nan'), float('nan')]46 mock_result_dataframe['k'] = [float('nan'), float('nan')]47 mock_result_dataframe['d'] = [float('nan'), float('nan')]48 mock_get_stochastic.return_value = mock_result_dataframe49 actual_result = self.mock_stock.get_tactical_data()50 mock_get_stochastic.assert_called_once_with(self.mock_dataframe)51 pd.testing.assert_frame_equal(actual_result, mock_result_dataframe)52 53 @patch('stock_data.json.loads')54 #@patch('stock_data.pd.DataFrame.from_dict')55 @requests_mock.Mocker()56 def test_get_execution_data(self, mock_json_loads, mock_request):57 58 mock_bars = {59 "FAKE": [60 {61 "t": 1544129220,62 "o": 172.26,63 "h": 172.3,64 "l": 172.16,65 "c": 172.18,66 "v": 3892,67 }68 ]69 }70 url = 'https://data.alpaca.markets/v1/bars/1Min?symbols=FAKE&limit=10'71 mock_request.get(url, text='request ok')72 mock_json_loads.return_value = mock_bars73 actual_result = self.mock_stock.get_execution_data()74 self.assertIsInstance(actual_result, pd.DataFrame)75 76 ### ------------------- POTENTIAL GETTERS TESTS ------------------- ###77 78 def test_get_trend_potential(self):79 pass80 81 82 def test_get_tactical_potential(self):83 pass84 85 def test_get_execution_potential(self):86 pass87 ### ------------------- STATIC METHODS TESTS ------------------- ###88 def test_is_in_range_true_default_range(self):89 x = 9590 y = 10591 actual_result = self.mock_stock.is_in_range(x, y)92 self.assertEqual(actual_result, True)93 94 95 def test_is_in_range_false_default_range(self):96 x = 8997 y = 11198 actual_result = self.mock_stock.is_in_range(x, y)99 self.assertEqual(actual_result, False)100 101 def test_is_in_range_true_custom_range(self):102 x = 89103 y = 111104 range_percent = 20105 actual_result = self.mock_stock.is_in_range(x, y, range_percent)106 self.assertEqual(actual_result, True)107 108 def test_is_trending_up_true_default_step(self):109 array = [1, 2, 3]110 actual_result = self.mock_stock.is_trending_up(array)111 self.assertEqual(actual_result, True)112 113 def test_is_trending_up_false_default_step(self):114 array = [3, 1, 2]115 actual_result = self.mock_stock.is_trending_up(array)116 self.assertEqual(actual_result, False)117 118 def test_is_trending_up_true_custom_step(self):119 array = [1, 2, 3, 4, 5, 6, 7, 6, 9, 10]120 step = 2121 actual_result = self.mock_stock.is_trending_up(array, step)122 self.assertEqual(actual_result, True)123 124 def test_is_trending_up_false_custom_step(self):125 array = [1, 2, 3, 8, 5, 6, 7, 8, 9, 10]126 step = 2127 actual_result = self.mock_stock.is_trending_up(array, step)128 self.assertEqual(actual_result, False)...

Full Screen

Full Screen

dataset_workflow_result_tests.py

Source:dataset_workflow_result_tests.py Github

copy

Full Screen

1from unittest import mock2import pandas as pd3from tethysext.atcore.models.resource_workflow_results import DatasetWorkflowResult4from tethysext.atcore.tests.utilities.sqlalchemy_helpers import SqlAlchemyTestCase5from tethysext.atcore.tests.utilities.sqlalchemy_helpers import setup_module_for_sqlalchemy_tests, \6 tear_down_module_for_sqlalchemy_tests7def setUpModule():8 setup_module_for_sqlalchemy_tests()9def tearDownModule():10 tear_down_module_for_sqlalchemy_tests()11class DatasetWorkflowResultTests(SqlAlchemyTestCase):12 def setUp(self):13 super().setUp()14 self.instance = DatasetWorkflowResult(name='test')15 def bind_instance_to_session(self):16 self.session.add(self.instance)17 self.session.commit()18 def test_default_options(self):19 baseline = {20 'data_table_kwargs': {21 'searching': False,22 'paging': False,23 'info': False24 },25 'no_dataset_message': 'No dataset found.'26 }27 self.assertDictEqual(baseline, self.instance.default_options)28 def test_datasets(self):29 self.assertDictEqual({}, self.instance.data)30 ret = self.instance.datasets31 self.assertListEqual([], ret)32 def test_reset(self):33 self.instance.data['datasets'] = 'Bad data to reset'34 self.instance.reset()35 ret = self.instance.data['datasets']36 self.assertEqual([], ret)37 def test__add_dataset(self):38 test_data = 'Test data'39 self.instance._add_dataset(test_data)40 self.assertListEqual([test_data], self.instance.datasets)41 def test_datasets_bound(self):42 self.bind_instance_to_session()43 self.test_datasets()44 def test_reset_datasets_bound(self):45 self.bind_instance_to_session()46 self.test_reset()47 def test__add_dataset_bound(self):48 self.bind_instance_to_session()49 self.test__add_dataset()50 @mock.patch('tethysext.atcore.models.resource_workflow_results.dataset_workflow_result.DatasetWorkflowResult._add_dataset') # noqa: E50151 def test_add_pandas_dataframe(self, mock_add_dataset):52 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=False)53 self.instance.add_pandas_dataframe('foo', mock_dataframe)54 baseline = {55 'title': 'foo',56 'dataset': mock_dataframe,57 'show_export_button': False58 }59 mock_add_dataset.assert_called_with(baseline)60 def test_add_pandas_dataframe_no_title(self):61 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=False)62 self.assertRaises(ValueError, self.instance.add_pandas_dataframe, '', mock_dataframe)63 self.assertRaises(ValueError, self.instance.add_pandas_dataframe, None, mock_dataframe)64 def test_add_pandas_dataframe_not_dataframe(self):65 self.assertRaises(ValueError, self.instance.add_pandas_dataframe, 'foo', ['test'])66 def test_add_pandas_dataframe_empty_dataframe(self):67 mock_dataframe = mock.MagicMock(spec=pd.DataFrame, empty=True)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful