From f2b802978d99b0cf329520c120b2decd5953ddd0 Mon Sep 17 00:00:00 2001 From: Karol Kostrzewa Date: Fri, 22 Jan 2021 11:38:33 +0100 Subject: [PATCH] fix: bar chart data order (#12665) * fix bar chart order * fix test_explore_json_dist_bar_order * fix quotes --- superset/viz.py | 2 +- tests/core_tests.py | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/superset/viz.py b/superset/viz.py index eeb4f2568..8a7ca7e3e 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -1648,7 +1648,7 @@ class DistributionBarViz(BaseViz): raise QueryObjectValidationError(_("Pick at least one metric")) if not fd.get("groupby"): raise QueryObjectValidationError(_("Pick at least one field for [Series]")) - d["orderby"] = [(d["metrics"][0], False)] + d["orderby"] = [(metric, False) for metric in d["metrics"]] return d def get_data(self, df: pd.DataFrame) -> VizData: diff --git a/tests/core_tests.py b/tests/core_tests.py index bcf5061cb..819a9c98f 100644 --- a/tests/core_tests.py +++ b/tests/core_tests.py @@ -889,6 +889,100 @@ class TestCore(SupersetTestCase): self.assertEqual(rv.status_code, 200) self.assertEqual(data["rowcount"], 2) + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") + def test_explore_json_dist_bar_order(self): + tbl_id = self.table_ids.get("birth_names") + form_data = { + "datasource": f"{tbl_id}__table", + "viz_type": "dist_bar", + "url_params": {}, + "time_range_endpoints": ["inclusive", "exclusive"], + "granularity_sqla": "ds", + "time_range": 'DATEADD(DATETIME("2021-01-22T00:00:00"), -100, year) : 2021-01-22T00:00:00', + "metrics": [ + { + "expressionType": "SIMPLE", + "column": { + "id": 334, + "column_name": "name", + "verbose_name": "null", + "description": "null", + "expression": "", + "filterable": True, + "groupby": True, + "is_dttm": False, + "type": "VARCHAR(255)", + "python_date_format": "null", + }, + "aggregate": "COUNT", + "sqlExpression": "null", + "isNew": False, + "hasCustomLabel": False, + "label": "COUNT(name)", + "optionName": "metric_xdzsijn42f9_khi4h3v3vci", + }, + { + "expressionType": "SIMPLE", + "column": { + "id": 332, + "column_name": "ds", + "verbose_name": "null", + "description": "null", + "expression": "", + "filterable": True, + "groupby": True, + "is_dttm": True, + "type": "TIMESTAMP WITHOUT TIME ZONE", + "python_date_format": "null", + }, + "aggregate": "COUNT", + "sqlExpression": "null", + "isNew": False, + "hasCustomLabel": False, + "label": "COUNT(ds)", + "optionName": "metric_80g1qb9b6o7_ci5vquydcbe", + }, + ], + "adhoc_filters": [], + "groupby": ["name"], + "columns": [], + "row_limit": 10, + "color_scheme": "supersetColors", + "label_colors": {}, + "show_legend": True, + "y_axis_format": "SMART_NUMBER", + "bottom_margin": "auto", + "x_ticks_layout": "auto", + } + + self.login(username="admin") + rv = self.client.post( + "/superset/explore_json/", data={"form_data": json.dumps(form_data)}, + ) + data = json.loads(rv.data.decode("utf-8")) + + resp = self.run_sql( + """ + SELECT count(name) AS count_name, count(ds) AS count_ds + FROM birth_names + WHERE ds >= '1921-01-22 00:00:00.000000' AND ds < '2021-01-22 00:00:00.000000' + GROUP BY name ORDER BY count_name DESC, count_ds DESC + LIMIT 10; + """, + client_id="client_id_1", + user_name="admin", + ) + count_ds = [] + count_name = [] + for series in data["data"]: + if series["key"] == "COUNT(ds)": + count_ds = series["values"] + if series["key"] == "COUNT(name)": + count_name = series["values"] + for expected, actual_ds, actual_name in zip(resp["data"], count_ds, count_name): + assert expected["count_name"] == actual_name["y"] + assert expected["count_ds"] == actual_ds["y"] + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") @mock.patch.dict( "superset.extensions.feature_flag_manager._feature_flags",