You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/01/21 08:54:03 UTC

[GitHub] [superset] villebro commented on a change in pull request #12417: fix(explore): Preserve Column Order in Bar chart

villebro commented on a change in pull request #12417:
URL: https://github.com/apache/superset/pull/12417#discussion_r561699562



##########
File path: tests/viz_tests.py
##########
@@ -531,6 +531,92 @@ def test_column_nulls(self):
         ]
         self.assertEqual(expected, data)
 
+    def test_column_metrics_in_order(self):
+        form_data = {
+            "metrics": ["z_column", "votes", "a_column"],
+            "adhoc_filters": [],
+            "groupby": ["toppings"],
+            "columns": [],
+        }
+        datasource = self.get_datasource_mock()
+        df = pd.DataFrame(
+            {
+                "toppings": ["cheese", "pepperoni", "cheese", "pepperoni"],
+                "role": ["engineer", "engineer", None, None],
+                "votes": [3, 5, 1, 2],
+                "a_column": [3, 5, 1, 2],
+                "z_column": [3, 5, 1, 2],
+            }
+        )
+        test_viz = viz.DistributionBarViz(datasource, form_data)
+        data = test_viz.get_data(df)
+
+        expected = [
+            {
+                "key": "z_column",
+                "values": [{"x": "pepperoni", "y": 3.5}, {"x": "cheese", "y": 2.0}],
+            },
+            {
+                "key": "votes",
+                "values": [{"x": "pepperoni", "y": 3.5}, {"x": "cheese", "y": 2.0}],
+            },
+            {
+                "key": "a_column",
+                "values": [{"x": "pepperoni", "y": 3.5}, {"x": "cheese", "y": 2.0}],
+            },
+        ]
+
+        self.assertEqual(expected, data)
+
+    def test_column_metrics_in_order_with_breakdowns(self):
+        form_data = {
+            "metrics": ["z_column", "votes", "a_column"],
+            "adhoc_filters": [],
+            "groupby": ["toppings"],
+            "columns": ["role"],
+        }
+        datasource = self.get_datasource_mock()
+        df = pd.DataFrame(
+            {
+                "toppings": ["cheese", "pepperoni", "cheese", "pepperoni"],
+                "role": ["engineer", "engineer", None, None],
+                "votes": [3, 5, 1, 2],
+                "a_column": [3, 5, 1, 2],
+                "z_column": [3, 5, 1, 2],
+            }
+        )
+        test_viz = viz.DistributionBarViz(datasource, form_data)
+        data = test_viz.get_data(df)
+
+        expected = [
+            {
+                "key": "z_column, {}".format(NULL_STRING),
+                "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
+            },
+            {
+                "key": "z_column, engineer",
+                "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
+            },
+            {
+                "key": "votes, {}".format(NULL_STRING),
+                "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
+            },
+            {
+                "key": "votes, engineer",
+                "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
+            },
+            {
+                "key": "a_column, {}".format(NULL_STRING),
+                "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
+            },
+            {
+                "key": "a_column, engineer",
+                "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
+            },
+        ]

Review comment:
       nit: let's use f-strings here
   ```suggestion
           expected = [
               {
                   "key": f"z_column, {NULL_STRING}",
                   "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
               },
               {
                   "key": "z_column, engineer",
                   "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
               },
               {
                   "key": f"votes, {NULL_STRING}",
                   "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
               },
               {
                   "key": "votes, engineer",
                   "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
               },
               {
                   "key": f"a_column, {NULL_STRING}",
                   "values": [{"x": "pepperoni", "y": 2}, {"x": "cheese", "y": 1}],
               },
               {
                   "key": "a_column, engineer",
                   "values": [{"x": "pepperoni", "y": 5}, {"x": "cheese", "y": 3}],
               },
           ]
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org