def test_ipython_notebook_funcs(): from dtale.views import DtaleData getter = namedtuple('get', 'ok') def mock_requests_get(url, verify=True): return getter(True) df = pd.DataFrame([1, 2, 3]) with ExitStack() as stack: mock_iframe = stack.enter_context( mock.patch('IPython.display.IFrame', mock.Mock())) stack.enter_context(mock.patch('requests.get', mock_requests_get)) stack.enter_context( mock.patch('dtale.views.in_ipython_frontend', return_value=True)) stack.enter_context( mock.patch('dtale.global_state.DATA', return_value={9999: df})) instance = DtaleData(9999, 'http://localhost:9999') instance.notebook_correlations(col1='col1', col2='col2') mock_iframe.assert_called_once() url_parser = get_url_parser() [path, query] = mock_iframe.call_args[0][0].split('?') assert path == 'http://localhost:9999/dtale/popup/correlations/9999' assert dict(url_parser(query)) == dict(col1='col1', col2='col2') instance.notebook_charts(x='col1', y='col2', group=['col3', 'col4'], agg='count') [path, query] = mock_iframe.call_args[0][0].split('?') assert path == 'http://localhost:9999/charts/9999' assert dict(url_parser(query)) == dict(chart_type='line', agg='count', group='["col3", "col4"]', x='col1', y='["col2"]', cpg='false') instance.notebook_charts(x='col1', y='col2', agg='count') [_path, query] = mock_iframe.call_args[0][0].split('?') assert dict(url_parser(query)) == dict(chart_type='line', agg='count', x='col1', y='["col2"]', cpg='false') instance.notebook_charts(x='col1', y='col2', group=['col3', 'col4']) [_path, query] = mock_iframe.call_args[0][0].split('?') assert dict(url_parser(query)) == dict(chart_type='line', x='col1', y='["col2"]', group='["col3", "col4"]', cpg='false')
def test_ipython_notebook_funcs(): from dtale.views import DtaleData getter = namedtuple("get", "ok") def mock_requests_get(url, verify=True): return getter(True) df = pd.DataFrame([1, 2, 3]) with ExitStack() as stack: mock_iframe = stack.enter_context( mock.patch("IPython.display.IFrame", mock.Mock())) stack.enter_context(mock.patch("requests.get", mock_requests_get)) stack.enter_context( mock.patch("dtale.views.in_ipython_frontend", return_value=True)) stack.enter_context( mock.patch("dtale.global_state.DATA", return_value={9999: df})) instance = DtaleData(9999, "http://localhost:9999") instance.notebook_correlations(col1="col1", col2="col2") mock_iframe.assert_called_once() url_parser = get_url_parser() [path, query] = mock_iframe.call_args[0][0].split("?") assert path == "http://localhost:9999/dtale/popup/correlations/9999" assert dict(url_parser(query)) == dict(col1="col1", col2="col2") instance.notebook_charts(x="col1", y="col2", group=["col3", "col4"], agg="count") [path, query] = mock_iframe.call_args[0][0].split("?") assert path == "http://localhost:9999/charts/9999" assert dict(url_parser(query)) == dict( chart_type="line", agg="count", group='["col3", "col4"]', x="col1", y='["col2"]', cpg="false", animate="false", ) instance.notebook_charts(x="col1", y="col2", agg="count") [_path, query] = mock_iframe.call_args[0][0].split("?") assert dict(url_parser(query)) == dict( chart_type="line", agg="count", x="col1", y='["col2"]', cpg="false", animate="false", ) instance.notebook_charts(x="col1", y="col2", group=["col3", "col4"]) [_path, query] = mock_iframe.call_args[0][0].split("?") assert dict(url_parser(query)) == dict( chart_type="line", x="col1", y='["col2"]', group='["col3", "col4"]', cpg="false", animate="false", )
def test_chart_building_bar_and_popup(unittest): import dtale.views as views df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9])) with app.test_client() as c: with ExitStack() as stack: df, _ = views.format_data(df) stack.enter_context(mock.patch('dtale.global_state.DATA', {c.port: df})) pathname = path_builder(c.port) inputs = { 'chart_type': 'bar', 'x': 'a', 'y': ['b', 'c'], 'z': None, 'group': None, 'agg': None, 'window': None, 'rolling_comp': None } chart_inputs = {'cpg': False, 'barmode': 'group', 'barsort': None} params = build_chart_params(pathname, inputs, chart_inputs, dict(type='multi', data={})) response = c.post('/charts/_dash-update-component', json=params) resp_data = response.get_json()['response'] links_div = resp_data['chart-content']['children']['props']['children'][0]['props']['children'] url = links_div[0]['props']['href'] assert url.startswith('/charts/{}?'.format(c.port)) url_params = dict(get_url_parser()(url.split('?')[-1])) unittest.assertEqual( url_params, {'chart_type': 'bar', 'x': 'a', 'barmode': 'group', 'cpg': 'false', 'y': '["b", "c"]'} ) unittest.assertEqual( resp_data['chart-content']['children']['props']['children'][1]['props']['figure']['layout'], {'barmode': 'group', 'legend': {'orientation': 'h', 'y': 1.2}, 'title': {'text': 'b, c by a'}, 'xaxis': {'tickformat': '.0f', 'title': {'text': 'a'}}, 'yaxis': {'tickformat': '.0f', 'title': {'text': 'b'}}, 'yaxis2': {'anchor': 'x', 'overlaying': 'y', 'side': 'right', 'tickformat': '.0f', 'title': {'text': 'c'}}} ) response = c.get(url) assert response.status_code == 200 [pathname_val, search_val] = url.split('?') response = c.post('/charts/_dash-update-component', json={ 'output': 'popup-content.children', 'changedPropIds': ['url.modified_timestamp'], 'inputs': [{'id': 'url', 'property': 'modified_timestamp'}], 'state': [ {'id': 'url', 'property': 'pathname', 'value': pathname_val}, {'id': 'url', 'property': 'search', 'value': '?{}'.format(search_val)} ] }) assert response.status_code == 200 chart_inputs['barmode'] = 'stack' params = build_chart_params(pathname, inputs, chart_inputs) response = c.post('/charts/_dash-update-component', json=params) resp_data = response.get_json()['response'] unittest.assertEqual( resp_data['chart-content']['children']['props']['children'][1]['props']['figure']['layout'], {'barmode': 'stack', 'legend': {'orientation': 'h', 'y': 1.2}, 'title': {'text': 'b, c by a'}, 'xaxis': {'tickformat': '.0f', 'title': {'text': 'a'}}, 'yaxis': {'tickformat': '.0f', 'title': {'text': 'b'}}} ) chart_inputs['barmode'] = 'group' chart_inputs['barsort'] = 'b' params = build_chart_params(pathname, inputs, chart_inputs) response = c.post('/charts/_dash-update-component', json=params) resp_data = response.get_json()['response'] unittest.assertEqual( resp_data['chart-content']['children']['props']['children'][1]['props']['figure']['layout'], {'barmode': 'group', 'legend': {'orientation': 'h', 'y': 1.2}, 'title': {'text': 'b, c by a'}, 'xaxis': {'tickmode': 'auto', 'nticks': 3, 'tickformat': '.0f', 'title': {'text': 'a'}}, 'yaxis': {'tickformat': '.0f', 'title': {'text': 'b, c'}}} ) inputs['y'] = ['b'] inputs['group'] = ['c'] chart_inputs['cpg'] = True params = build_chart_params(pathname, inputs, chart_inputs) response = c.post('/charts/_dash-update-component', json=params) resp_data = response.get_json()['response'] assert len(resp_data['chart-content']['children']) == 2