示例#1
0
def test_jinja_output():
    import dtale.views as views

    df = pd.DataFrame([1, 2, 3])
    df, _ = views.format_data(df)
    with build_app(url=URL).test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(df)}))
            stack.enter_context(
                mock.patch('dtale.dash_application.views.DATA', {c.port: df}))
            response = c.get('/dtale/main/{}'.format(c.port))
            assert 'span id="forkongithub"' not in str(response.data)
            response = c.get('/charts/{}'.format(c.port))
            assert 'span id="forkongithub"' not in str(response.data)

    with build_app(url=URL, github_fork=True).test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(df)}))
            stack.enter_context(
                mock.patch('dtale.dash_application.views.DATA', {c.port: df}))
            response = c.get('/dtale/main/1')
            assert 'span id="forkongithub"' in str(response.data)
            response = c.get('/charts/{}'.format(c.port))
            assert 'span id="forkongithub"' in str(response.data)
示例#2
0
def test_wordcloud():
    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:
        df, _ = views.format_data(df)
        build_data_inst({c.port: df})
        inputs = {
            "chart_type": "wordcloud",
            "x": "a",
            "y": ["b"],
            "z": None,
            "group": None,
            "agg": None,
            "window": None,
            "rolling_comp": None,
        }
        chart_inputs = {
            "cpg": False,
            "cpy": False,
            "barmode": "group",
            "barsort": None
        }
        extended_aggregation = [
            dict(col="b", agg="mean"),
            dict(col="b", agg="sum")
        ]
        params = build_chart_params(c.port,
                                    inputs,
                                    chart_inputs,
                                    extended_aggregation=extended_aggregation)
        response = c.post("/dtale/charts/_dash-update-component", json=params)
        resp_data = response.get_json()["response"]
        assert (resp_data["chart-content"]["children"]["props"]["children"][1]
                ["type"] == "Wordcloud")
示例#3
0
def test_failure():
    import dtale.views as views

    df, _ = views.format_data(
        pd.DataFrame(
            [
                [1, 1, 3.29, 3.41, 3.64],
                [1, 2, 2.44, 2.32, 2.42],
                [1, 3, 4.34, 4.17, 4.27],
                [2, 1, 3.08, 3.25, 3.07],
                [2, 2, 2.53, 1.78, 2.32],
                [2, 3, 4.19, 3.94, 4.34],
                [2, 4, 3.01, 4.03, 3.2],
                [2, 5, 2.44, 1.8, 1.72],
                [3, 1, 3.04, 2.89, 2.85],
                [3, 2, 1.62, 1.87, 2.04],
                [3, 3, 3.88, 4.09, 3.67],
                [3, 4, 3.14, 3.2, 3.11],
                [3, 5, 1.54, 1.93, 1.55],
            ],
            columns=["o", "p", "m1", "m2", "m3"],
        ))
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})

        resp = c.get(
            "/dtale/gage-rnr/{}".format(c.port),
            query_string=dict(operator=json.dumps(["o"])),
        )
        resp = resp.json
        assert "error" in resp
示例#4
0
def test_chart_building_scatter(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': 'scatter', 'x': 'a', 'y': ['b'], '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)
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']
            assert resp_data['chart-content']['children'][0]['props']['children'][1]['props']['id'] == 'scatter-all-b'

            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
示例#5
0
def test_login():
    import dtale.views as views

    df, _ = views.format_data(pd.DataFrame(dict(a=[1, 2, 3, 4, 5, 6])))
    with ExitStack() as stack:
        stack.enter_context(
            mock.patch(
                "dtale.auth.global_state.get_auth_settings",
                return_value={
                    "active": True,
                    "username": "******",
                    "password": "******"
                },
            ))
        mock_session = stack.enter_context(
            mock.patch("dtale.auth.session", dict()))
        with build_app(url=URL).test_client() as c:
            build_data_inst({c.port: df})

            resp = c.get("/dtale/static/css/main.css")
            assert resp.status_code == 200

            resp = c.get("/dtale/main/{}".format(c.port))
            assert resp.status_code == 302
            assert resp.location == "http://localhost:{}/login".format(c.port)

            resp = c.post("/login", data=dict(username="******", password="******"))
            assert resp.location == "http://localhost:{}/dtale/main/{}".format(
                c.port, c.port)
            assert mock_session["logged_in"]
            assert mock_session["username"] == "foo"

            resp = c.get("/logout")
            assert resp.location == "http://localhost:{}/login".format(c.port)
            assert mock_session.get("logged_in") is None
示例#6
0
def test_upload():
    import dtale.views as views

    df, _ = views.format_data(pd.DataFrame([1, 2, 3]))
    with build_app(url=URL).test_client() as c:
        with ExitStack() as stack:
            data = {c.port: df}
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            stack.enter_context(
                mock.patch("dtale.global_state.DTYPES",
                           {c.port: views.build_dtypes_state(df)}))

            resp = c.post("/dtale/upload")
            assert not resp.get_json()["success"]

            c.post(
                "/dtale/upload",
                data={
                    "contents": build_upload_data(),
                    "filename": "test_df.csv"
                },
            )
            assert len(data) == 2
            new_key = next((k for k in data if k != c.port), None)
            assert list(data[new_key].columns) == ["a", "b", "c"]
示例#7
0
def test_edit_to_nan():
    from dtale.views import build_dtypes_state, format_data

    df = edit_data()
    df, _ = format_data(df)
    with app.test_client() as c:
        with ExitStack() as stack:
            data = {c.port: df}
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            settings = {c.port: {"locked": ["a"]}}
            stack.enter_context(
                mock.patch("dtale.global_state.SETTINGS", settings))
            dtypes = {c.port: build_dtypes_state(df)}
            stack.enter_context(mock.patch("dtale.global_state.DTYPES",
                                           dtypes))
            c.get(
                "/dtale/edit-cell/{}/a".format(c.port),
                query_string=dict(rowIndex=0, updated="nan"),
            )
            assert pd.isnull(data[c.port].a.values[0])
            c.get(
                "/dtale/edit-cell/{}/b".format(c.port),
                query_string=dict(rowIndex=0, updated="inf"),
            )
            assert np.isinf(data[c.port].b.values[0])
示例#8
0
def test_main():
    import dtale.views as views

    test_data = pd.DataFrame(build_ts_data(),
                             columns=['date', 'security_id', 'foo', 'bar'])
    test_data, _ = views.format_data(test_data)
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch('dtale.views.METADATA',
                           {c.port: dict(name='test_name')}))
            stack.enter_context(
                mock.patch('dtale.views.SETTINGS', {c.port: dict(locked=[])}))
            response = c.get('/dtale/main')
            assert '<title>D-Tale (test_name)</title>' in str(response.data)
            response = c.get('/dtale/iframe')
            assert '<title>D-Tale (test_name)</title>' in str(response.data)
            response = c.get('/dtale/popup/test', query_string=dict(col='foo'))
            assert '<title>D-Tale (test_name) - test (col: foo)</title>' in str(
                response.data)

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch('dtale.views.METADATA', {c.port: dict()}))
            stack.enter_context(
                mock.patch('dtale.views.SETTINGS', {c.port: dict(locked=[])}))
            response = c.get('/dtale/main')
            assert '<title>D-Tale</title>' in str(response.data)
示例#9
0
def test_network_analysis(network_data, unittest):
    import dtale.views as views

    df, _ = views.format_data(network_data)
    with build_app(url=URL).test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch("dtale.global_state.DATA", {c.port: df}))

            resp = c.get(
                "/dtale/network-analysis/{}".format(c.port),
                query_string={
                    "to": "to",
                    "from": "from",
                    "weight": "weight"
                },
            )
            unittest.assertEqual(
                resp.json["data"],
                {
                    "avg_weight": 2.68,
                    "edge_ct": 36,
                    "leaf_ct": 3,
                    "max_edge": "10 (source: h, target: j)",
                    "min_edge": "1 (source: j, target: k)",
                    "most_connected_node": "g (Connections: 5)",
                    "node_ct": 14,
                    "triangle_ct": 2,
                },
            )
示例#10
0
def test_rolling(rolling_data):
    import dtale.views as views

    df, _ = views.format_data(rolling_data)
    data_id, column_type = "1", "rolling"
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: df}))

        cfg = {"col": "0", "comp": "mean", "window": "5", "min_periods": 1}
        builder = ColumnBuilder(data_id, column_type, "0_rolling_mean", cfg)
        verify_builder(
            builder,
            lambda col: col.isnull().sum() == 0,
        )

        cfg = {
            "col": "0",
            "comp": "mean",
            "window": "5",
            "min_periods": 1,
            "on": "date",
            "center": True,
        }
        builder = ColumnBuilder(data_id, column_type, "0_rolling_mean", cfg)
        verify_builder(
            builder,
            lambda col: col.isnull().sum() == 0,
        )
示例#11
0
def test_building_choropleth_map_w_custom_geojson(unittest):
    import dtale.views as views

    df = pd.DataFrame([
        dict(id="US.MA", name="mass", pop=125),
        dict(id="US.WA", name="wash", pop=500),
        dict(id="US.CA", name="cali", pop=1000),
    ])

    with app.test_client() as c:
        with ExitStack() as stack:
            custom_geojson_data = []
            stack.enter_context(
                mock.patch(
                    "dtale.dash_application.custom_geojson.CUSTOM_GEOJSON",
                    custom_geojson_data,
                ))
            params = {
                "output":
                "..output-geojson-upload.children...geojson-dropdown.options..",
                "changedPropIds": ["upload-geojson.content"],
                "inputs": [{
                    "id": "upload-geojson",
                    "property": "content",
                    "value": build_geojson_data(),
                }],
                "state": [{
                    "id": "upload-geojson",
                    "property": "filename",
                    "value": "USA.json",
                }],
            }
            c.post("/dtale/charts/_dash-update-component", json=params)

            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": "maps", "agg": "raw"}
            map_inputs = {
                "map_type": "choropleth",
                "loc_mode": "geojson-id",
                "geojson": "USA",
                "featureidkey": "HASC_1",
                "loc": "id",
                "map_val": "pop",
            }
            chart_inputs = {"colorscale": "Reds"}
            params = build_chart_params(pathname,
                                        inputs,
                                        chart_inputs,
                                        map_inputs=map_inputs)
            response = c.post("/dtale/charts/_dash-update-component",
                              json=params)
            chart_markup = response.get_json(
            )["response"]["chart-content"]["children"]["props"]["children"][1]
            unittest.assertEqual(
                chart_markup["props"]["figure"]["layout"]["title"],
                {"text": "Map of pop (No Aggregation)"},
            )
示例#12
0
def test_rolling(rolling_data):
    import dtale.views as views

    df, _ = views.format_data(rolling_data)
    data_id, column_type = "1", "rolling"
    build_data_inst({data_id: df})

    cfg = {"col": "0", "comp": "mean", "window": "5", "min_periods": 1}
    builder = ColumnBuilder(data_id, column_type, "0_rolling_mean", cfg)
    verify_builder(
        builder,
        lambda col: col.isnull().sum() == 0,
    )

    cfg = {
        "col": "0",
        "comp": "mean",
        "window": "5",
        "min_periods": 1,
        "on": "date",
        "center": True,
    }
    builder = ColumnBuilder(data_id, column_type, "0_rolling_mean", cfg)
    verify_builder(
        builder,
        lambda col: col.isnull().sum() == 0,
    )
示例#13
0
def test_query_changes(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.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            params = {
                'output': '..query-data.data...query-input.style...query-input.title..',
                'changedPropIds': ['query-input.value'],
                'inputs': [{'id': 'query-input', 'property': 'value', 'value': 'd'}],
                'state': [pathname, {'id': 'query-data', 'property': 'data'}]
            }
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']
            assert resp_data['query-data']['data'] is None
            assert resp_data['query-input']['title'] == "name 'd' is not defined"

            params['inputs'][0]['value'] = 'a == 1'
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']
            assert resp_data['query-data']['data'] == 'a == 1'
示例#14
0
def test_get_correlations(unittest, test_data):
    import dtale.views as views

    with app.test_client() as c:
        with ExitStack() as stack:
            test_data, _ = views.format_data(test_data)
            stack.enter_context(
                mock.patch('dtale.views.DATA', {c.port: test_data}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(test_data)}))
            response = c.get('/dtale/correlations')
            response_data = json.loads(response.data)
            expected = dict(data=[
                dict(column='security_id', security_id=1.0, foo=None,
                     bar=None),
                dict(column='foo', security_id=None, foo=None, bar=None),
                dict(column='bar', security_id=None, foo=None, bar=None)
            ],
                            dates=[])
            unittest.assertEqual(response_data, expected,
                                 'should return correlations')

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch('dtale.views.DATA', {c.port: test_data}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(test_data)}))
            response = c.get('/dtale/correlations',
                             query_string=dict(query="missing_col == 'blah'"))
            response_data = json.loads(response.data)
            unittest.assertEqual(response_data['error'],
                                 "name 'missing_col' is not defined",
                                 'should handle correlations exception')

    with app.test_client() as c:
        with ExitStack() as stack:
            test_data.loc[test_data.security_id == 1, 'bar'] = np.nan
            test_data2 = test_data.copy()
            test_data2.loc[:, 'date'] = pd.Timestamp('20000102')
            test_data = pd.concat([test_data, test_data2], ignore_index=True)
            stack.enter_context(
                mock.patch('dtale.views.DATA', {c.port: test_data}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(test_data)}))
            response = c.get('/dtale/correlations')
            response_data = json.loads(response.data)
            expected = expected = dict(data=[
                dict(column='security_id', security_id=1.0, foo=None,
                     bar=None),
                dict(column='foo', security_id=None, foo=None, bar=None),
                dict(column='bar', security_id=None, foo=None, bar=None)
            ],
                                       dates=['date'])
            unittest.assertEqual(response_data, expected,
                                 'should return correlations')
示例#15
0
def test_get_correlations_ts(unittest, rolling_data):
    import dtale.views as views

    test_data = pd.DataFrame(build_ts_data(size=50),
                             columns=['date', 'security_id', 'foo', 'bar'])

    with app.test_client() as c:
        with mock.patch('dtale.views.DATA', {c.port: test_data}):
            params = dict(dateCol='date', cols=json.dumps(['foo', 'bar']))
            response = c.get('/dtale/correlations-ts/{}'.format(c.port),
                             query_string=params)
            response_data = json.loads(response.data)
            expected = {
                'data': {
                    'all': {
                        'x': [
                            '2000-01-01', '2000-01-02', '2000-01-03',
                            '2000-01-04', '2000-01-05'
                        ],
                        'corr': [1.0, 1.0, 1.0, 1.0, 1.0]
                    }
                },
                'max': {
                    'corr': 1.0,
                    'x': '2000-01-05'
                },
                'min': {
                    'corr': 1.0,
                    'x': '2000-01-01'
                },
                'success': True,
            }
            unittest.assertEqual(response_data, expected,
                                 'should return timeseries correlation')

    df, _ = views.format_data(rolling_data)
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
            stack.enter_context(
                mock.patch('dtale.views.DTYPES',
                           {c.port: views.build_dtypes_state(df)}))
            params = dict(dateCol='date',
                          cols=json.dumps(['0', '1']),
                          rollingWindow='4')
            response = c.get('/dtale/correlations-ts/{}'.format(c.port),
                             query_string=params)
            response_data = json.loads(response.data)
            unittest.assertEqual(response_data['success'], True,
                                 'should return rolling correlation')

    with app.test_client() as c:
        with mock.patch('dtale.views.DATA', {c.port: test_data}):
            response = c.get('/dtale/correlations-ts/{}'.format(c.port),
                             query_string=dict(query="missing_col == 'blah'"))
            response_data = json.loads(response.data)
            unittest.assertEqual(response_data['error'],
                                 "name 'missing_col' is not defined",
                                 'should handle correlations exception')
示例#16
0
def test_matrix():
    import dtale.views as views

    df, _ = views.format_data(pd.DataFrame(dict(a=[1, 2, 3, 4, 5, 6])))
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})

        resp = c.get("/dtale/missingno/matrix/{}".format(c.port))
        assert resp.content_type == "image/png"
示例#17
0
def test_dendrogram(rolling_data):
    import dtale.views as views

    df, _ = views.format_data(rolling_data)
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})

        resp = c.get("/dtale/missingno/dendrogram/{}".format(c.port))
        assert resp.content_type == "image/png"
示例#18
0
def test_chart_building_pie(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.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            inputs = {
                'chart_type': 'pie', 'x': 'a', 'y': ['b'], 'z': None, 'group': ['c'], 'agg': None,
                'window': None, 'rolling_comp': None
            }
            chart_inputs = {'cpg': True, 'barmode': 'group', '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']
            assert len(resp_data['chart-content']['children']) == 2

            inputs['group'] = None
            chart_inputs['cpg'] = False
            params = build_chart_params(pathname, inputs, chart_inputs)
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']
            assert resp_data['chart-content']['children'][0]['type'] == 'Div'

    df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, -6]))
    with app.test_client() as c:
        with ExitStack() as stack:
            df, _ = views.format_data(df)
            stack.enter_context(mock.patch('dtale.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            inputs = {
                'chart_type': 'pie', 'x': 'a', 'y': ['b'], 'z': None, 'group': None, 'agg': None,
                'window': None, 'rolling_comp': None
            }
            chart_inputs = {'cpg': False, 'barmode': 'group', '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']
            error = resp_data['chart-content']['children'][0]['props']['children'][0]['props']['children']
            assert error['props']['children'][2]['props']['children']['props']['children'] == '3 (-6)'
示例#19
0
def test_replace():
    import dtale.views as views

    df, _ = views.format_data(pd.DataFrame({"A": ["foo_bar"]}))
    data_id, column_type = "1", "replace"
    build_data_inst({data_id: df})

    cfg = {"col": "A", "search": "_bar", "replacement": "_baz"}
    builder = ColumnBuilder(data_id, column_type, "A_replace", cfg)
    verify_builder(builder, lambda col: col.values[0] == "foo_baz")
示例#20
0
def test_dtypes(test_data):
    from dtale.views import build_dtypes_state, format_data

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
            stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: build_dtypes_state(test_data)}))
            response = c.get('/dtale/dtypes/{}'.format(c.port))
            response_data = json.loads(response.data)
            assert response_data['success']

            for col in test_data.columns:
                response = c.get('/dtale/describe/{}/{}'.format(c.port, col))
                response_data = json.loads(response.data)
                assert response_data['success']

    lots_of_groups = pd.DataFrame([dict(a=i, b=1) for i in range(150)])
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: lots_of_groups}))
            stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: build_dtypes_state(lots_of_groups)}))
            response = c.get('/dtale/dtypes/{}'.format(c.port))
            response_data = json.loads(response.data)
            assert response_data['success']

            response = c.get('/dtale/describe/{}/{}'.format(c.port, 'a'))
            response_data = json.loads(response.data)
            assert response_data['uniques']['top']
            assert response_data['success']

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DTYPES', {}))
            response = c.get('/dtale/dtypes/{}'.format(c.port))
            response_data = json.loads(response.data)
            assert 'error' in response_data

            response = c.get('/dtale/describe/{}/foo'.format(c.port))
            response_data = json.loads(response.data)
            assert 'error' in response_data

    df = pd.DataFrame([
        dict(date=pd.Timestamp('now'), security_id=1, foo=1.0, bar=2.0),
        dict(date=pd.Timestamp('now'), security_id=1, foo=2.0, bar=np.inf)
    ], columns=['date', 'security_id', 'foo', 'bar'])
    df, _ = format_data(df)
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: build_dtypes_state(df)}))
            response = c.get('/dtale/describe/{}/{}'.format(c.port, 'bar'))
            response_data = json.loads(response.data)
            assert response_data['describe']['min'] == '2'
            assert response_data['describe']['max'] == 'inf'
示例#21
0
def test_get_pps_matrix(unittest, test_data):
    import dtale.views as views

    with app.test_client() as c:
        test_data, _ = views.format_data(test_data)
        build_data_inst({c.port: test_data})
        build_dtypes({c.port: views.build_dtypes_state(test_data)})
        response = c.get("/dtale/correlations/{}?pps=true".format(c.port))
        response_data = response.json
        expected = [
            {
                "bar": 1,
                "column": "bar",
                "foo": 0,
                "security_id": 0
            },
            {
                "bar": 0,
                "column": "foo",
                "foo": 1,
                "security_id": 0
            },
            {
                "bar": 0,
                "column": "security_id",
                "foo": 0,
                "security_id": 1
            },
        ]
        unittest.assertEqual(
            response_data["data"],
            expected,
            "should return scores",
        )
        pps_val = next(
            (p for p in response_data["pps"]
             if p["y"] == "security_id" and p["x"] == "foo"),
            None,
        )
        expected = {
            "baseline_score": 12.5,
            "case": "regression",
            "is_valid_score": "True",
            "metric": "mean absolute error",
            "model": "DecisionTreeRegressor()",
            "model_score": 12.635071,
            "ppscore": 0,
            "x": "foo",
            "y": "security_id",
        }
        unittest.assertEqual(pps_val, expected,
                             "should return PPS information")
        assert "import ppscore" in response_data["code"]
        assert "corr_data = ppscore.matrix(corr_data)" in response_data["code"]
示例#22
0
def test_build_chart_type():
    from dtale.dash_application.charts import build_chart

    import dtale.views as views

    with app.test_client() as c:
        with ExitStack() as stack:
            df, _ = views.format_data(pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9])))
            stack.enter_context(mock.patch('dtale.global_state.DATA', {c.port: df}))
            output = build_chart(c.port, chart_type='unknown', x='a', y='b')
            assert output[0].children[1].children == 'chart type: unknown'
示例#23
0
def test_chart_building_surface(unittest, test_data):
    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.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            inputs = {
                'chart_type': 'surface', 'x': 'a', 'y': ['b'], 'z': 'c', 'group': None, 'agg': None,
                'window': None, 'rolling_comp': None
            }
            chart_inputs = {'cpg': False, 'barmode': 'group', 'barsort': 'b'}
            params = build_chart_params(pathname, inputs, chart_inputs)
            response = c.post('/charts/_dash-update-component', json=params)
            chart_markup = response.get_json()['response']['chart-content']['children'][0]['props']['children'][1]
            unittest.assertEqual(
                chart_markup['props']['figure']['layout']['title'],
                {'text': 'b by a weighted by c'}
            )

    with app.test_client() as c:
        with ExitStack() as stack:
            df, _ = views.format_data(test_data)
            stack.enter_context(mock.patch('dtale.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            inputs = {
                'chart_type': 'surface', 'x': 'date', 'y': ['security_id'], 'z': 'bar', 'group': None, 'agg': 'mean',
                'window': None, 'rolling_comp': None
            }
            chart_inputs = {'cpg': False, 'barmode': 'group', 'barsort': 'b'}
            params = build_chart_params(pathname, inputs, chart_inputs)
            response = c.post('/charts/_dash-update-component', json=params)
            chart_markup = response.get_json()['response']['chart-content']['children'][0]['props']['children'][1]
            unittest.assertEqual(
                chart_markup['props']['figure']['layout']['title'],
                {'text': 'security_id by date weighted by bar (Mean)'}
            )
示例#24
0
def test_hpfilter(custom_data, ts_analysis_data, unittest):
    import dtale.views as views

    df, _ = views.format_data(ts_analysis_data)
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})

        resp = c.get(
            "/dtale/timeseries-analysis/{}".format(c.port),
            query_string=dict(type="not_implemented", cfg=json.dumps({})),
        )
        assert not resp.json["success"]

        cfg = dict(index="date", col="realgdp", lamb=1600)
        resp = c.get(
            "/dtale/timeseries-analysis/{}".format(c.port),
            query_string=dict(type="hpfilter", cfg=json.dumps(cfg)),
        )
        unittest.assertEqual(
            sorted(resp.json["data"]["all"].keys()), ["cycle", "realgdp", "trend", "x"]
        )

    df, _ = views.format_data(custom_data)
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})

        cfg = dict(index="date", col="Col1", lamb=1600)
        resp = c.get(
            "/dtale/timeseries-analysis/{}".format(c.port),
            query_string=dict(type="hpfilter", cfg=json.dumps(cfg)),
        )
        assert not resp.json["success"]

        cfg["agg"] = "mean"
        resp = c.get(
            "/dtale/timeseries-analysis/{}".format(c.port),
            query_string=dict(type="hpfilter", cfg=json.dumps(cfg)),
        )
        unittest.assertEqual(
            sorted(resp.json["data"]["all"].keys()), ["Col1", "cycle", "trend", "x"]
        )
示例#25
0
def test_302():
    import dtale.views as views

    df = pd.DataFrame([1, 2, 3])
    df, _ = views.format_data(df)
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(df)}))
            for path in ['/', '/dtale', '/dtale/main', '/dtale/iframe', '/dtale/popup/test', '/favicon.ico']:
                response = c.get(path)
                assert response.status_code == 302, '{} should return 302 response'.format(path)
示例#26
0
def test_load_saved_chart(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():
            df, _ = views.format_data(df)
            build_data_inst({c.port: df})

            input_data = {
                "data_id": str(c.port),
                "query": None,
                "chart_type": "line",
                "agg": "sum",
                "x": "a",
                "y": ["b"],
                "yaxis": {},
                "cpg": False,
                "cpy": False,
                "animate": False,
                "trendline": False,
            }
            params = {
                "output": (
                    "..saved-chart-div-1.style...saved-chart-1.children...prev-saved-chart-config-1.data."
                    "..saved-chart-header-1.children.."
                ),
                "inputs": [ts_builder("saved-chart-config-1")],
                "state": [
                    {
                        "id": "saved-chart-config-1",
                        "property": "data",
                        "value": input_data,
                    },
                    {
                        "id": "prev-saved-chart-config-1",
                        "property": "data",
                        "value": None,
                    },
                ],
            }
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            response = response.json["response"]
            assert response["saved-chart-div-1"]["style"]["display"] == "block"

            params["state"][1]["value"] = input_data
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            assert response.status_code == 204

            params["state"][0]["value"] = None
            response = c.post("/dtale/charts/_dash-update-component", json=params)
            response = response.json["response"]
            assert response["saved-chart-div-1"]["style"]["display"] == "none"
示例#27
0
def test_display_error(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.dash_application.charts.DATA', {c.port: df}))
            stack.enter_context(mock.patch(
                'dtale.dash_application.components.Wordcloud',
                mock.Mock(side_effect=Exception('error test'))
            ))
            pathname = {'id': 'url', 'property': 'pathname', 'value': '/charts/{}'.format(c.port)}
            inputs = {'id': 'input-data', 'property': 'data', 'value': {
                'chart_type': 'wordcloud', 'x': 'a', 'y': ['b'], 'z': None, 'group': None, 'agg': None,
                'window': None, 'rolling_comp': None}}
            chart_inputs = {
                'id': 'chart-input-data', 'property': 'data', 'value': {
                    'cpg': False, 'barmode': 'group', 'barsort': None
                }
            }
            params = {
                'output': '..chart-content.children...last-chart-input-data.data...range-data.data..',
                'changedPropIds': ['input-data.modified_timestamp'],
                'inputs': [ts_builder('input-data'), ts_builder('chart-input-data'), ts_builder('yaxis-data')],
                'state': [
                    pathname,
                    inputs,
                    chart_inputs,
                    {'id': 'yaxis-data', 'property': 'data', 'value': {}},
                    {'id': 'last-chart-input-data', 'property': 'data', 'value': {}}
                ]
            }
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']['chart-content']['children']
            assert resp_data['props']['children'][1]['props']['children'] == 'error test'

            params = {
                'output': '..chart-content.children...last-chart-input-data.data...range-data.data..',
                'changedPropIds': ['input-data.modified_timestamp'],
                'inputs': [ts_builder('input-data'), ts_builder('chart-input-data'), ts_builder('yaxis-data')],
                'state': [
                    pathname,
                    inputs,
                    chart_inputs,
                    {'id': 'yaxis-data', 'property': 'data', 'value': {}},
                    {'id': 'last-chart-input-data', 'property': 'data', 'value': {}}
                ]
            }
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']['chart-content']['children']
            assert resp_data['props']['children'][1]['props']['children'] == 'error test'
示例#28
0
def test_exponential_smoothing(rolling_data):
    import dtale.views as views

    df, _ = views.format_data(rolling_data)
    data_id, column_type = "1", "exponential_smoothing"
    build_data_inst({data_id: df})

    cfg = {"col": "0", "alpha": 0.3}
    builder = ColumnBuilder(data_id, column_type, "0_exp_smooth", cfg)
    verify_builder(
        builder,
        lambda col: col.isnull().sum() == 0,
    )
示例#29
0
def test_input_changes(unittest):
    import dtale.views as views

    df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9], d=pd.date_range('20200101', '20200103')))
    with app.test_client() as c:
        with ExitStack() as stack:
            df, _ = views.format_data(df)
            stack.enter_context(mock.patch('dtale.dash_application.views.DATA', {c.port: df}))
            stack.enter_context(mock.patch('dtale.dash_application.charts.DATA', {c.port: df}))
            pathname = path_builder(c.port)
            params = {
                'output': (
                    '..input-data.data...x-dropdown.options...y-single-dropdown.options...y-multi-dropdown.options.'
                    '..z-dropdown.options...group-dropdown.options...barsort-dropdown.options.'
                    '..yaxis-dropdown.options..'
                ),
                'changedPropIds': ['chart-tabs.value'],
                'inputs': [
                    ts_builder('query-data'),
                    {'id': 'chart-tabs', 'property': 'value', 'value': 'line'},
                    {'id': 'x-dropdown', 'property': 'value'},
                    {'id': 'y-multi-dropdown', 'property': 'value'},
                    {'id': 'y-single-dropdown', 'property': 'value'},
                    {'id': 'z-dropdown', 'property': 'value'},
                    {'id': 'group-dropdown', 'property': 'value'},
                    {'id': 'agg-dropdown', 'property': 'value'},
                    {'id': 'window-input', 'property': 'value'},
                    {'id': 'rolling-comp-dropdown', 'property': 'value'}
                ],
                'state': [pathname, {'id': 'query-data', 'property': 'data'}]
            }
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()
            unittest.assertEqual(resp_data['response']['input-data']['data'], {
                'chart_type': 'line', 'x': None, 'y': [], 'z': None, 'group': None, 'agg': None, 'window': None,
                'rolling_comp': None, 'query': None
            })
            unittest.assertEqual(
                resp_data['response']['x-dropdown']['options'],
                [{'label': 'a', 'value': 'a'}, {'label': 'b', 'value': 'b'}, {'label': 'c', 'value': 'c'},
                 {'label': 'd (Hourly)', 'value': 'd|H'}, {'label': 'd (Hour)', 'value': 'd|H2'},
                 {'label': 'd (Weekday)', 'value': 'd|WD'}, {'label': 'd', 'value': 'd'},
                 {'label': 'd (Weekly)', 'value': 'd|W'}, {'label': 'd (Monthly)', 'value': 'd|M'},
                 {'label': 'd (Quarterly)', 'value': 'd|Q'}, {'label': 'd (Yearly)', 'value': 'd|Y'}]
            )
            params['inputs'][2]['value'] = 'a'
            params['inputs'][3]['value'] = ['b', 'c']
            response = c.post('/charts/_dash-update-component', json=params)
            resp_data = response.get_json()['response']
            unittest.assertEqual([o['value'] for o in resp_data['barsort-dropdown']['options']], ['a', 'b', 'c'])
            unittest.assertEqual([o['value'] for o in resp_data['yaxis-dropdown']['options']], ['b', 'c'])
示例#30
0
def test_correlation_analysis(unittest, rolling_data):
    import dtale.views as views

    with app.test_client() as c:
        df, _ = views.format_data(rolling_data)
        build_data_inst({c.port: df})
        build_dtypes({c.port: views.build_dtypes_state(df)})
        response = c.get("/dtale/corr-analysis/{}".format(c.port))
        response_data = json.loads(response.data)
        ranks = response_data["ranks"]
        corrs = response_data["corrs"]
        assert len(ranks) == 5
        assert all(r["column"] in corrs for r in ranks)
        assert all(r["missing"] == 0 for r in ranks)