Skip to content

Commit

Permalink
[bugfix] Fix Tableviz metrics column disorder (apache#5566)
Browse files Browse the repository at this point in the history
* fix metrics disorder

* add tableviz metric order tests

* lint code

* use OrderedDict to avoid metrics disorder

* fix unit test
  • Loading branch information
yamyamyuo authored and betodealmeida committed Oct 4, 2018
1 parent df64eca commit ab5c910
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def __init__(self, datasource, form_data, force=False):
self.process_metrics()

def process_metrics(self):
# metrics in TableViz is order sensitive, so metric_dict should be
# OrderedDict
self.metric_dict = OrderedDict()
fd = self.form_data
for mkey in METRIC_KEYS:
Expand Down
48 changes: 48 additions & 0 deletions tests/viz_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ def test_constructor_exception_no_datasource(self):
with self.assertRaises(Exception):
viz.BaseViz(datasource, form_data)

def test_process_metrics(self):
# test TableViz metrics in correct order
form_data = {
'url_params': {},
'row_limit': 500,
'metric': 'sum__SP_POP_TOTL',
'entity': 'country_code',
'secondary_metric': 'sum__SP_POP_TOTL',
'granularity_sqla': 'year',
'page_length': 0,
'all_columns': [],
'viz_type': 'table',
'since': '2014-01-01',
'until': '2014-01-02',
'metrics': [
'sum__SP_POP_TOTL',
'SUM(SE_PRM_NENR_MA)',
'SUM(SP_URB_TOTL)',
],
'country_fieldtype': 'cca3',
'percent_metrics': [
'count',
],
'slice_id': 74,
'time_grain_sqla': None,
'order_by_cols': [],
'groupby': [
'country_name',
],
'compare_lag': '10',
'limit': '25',
'datasource': '2__table',
'table_timestamp_format': '%Y-%m-%d %H:%M:%S',
'markup_type': 'markdown',
'where': '',
'compare_suffix': 'o10Y',
}
datasource = Mock()
datasource.type = 'table'
test_viz = viz.BaseViz(datasource, form_data)
expect_metric_labels = [u'sum__SP_POP_TOTL',
u'SUM(SE_PRM_NENR_MA)',
u'SUM(SP_URB_TOTL)',
u'count',
]
self.assertEqual(test_viz.metric_labels, expect_metric_labels)
self.assertEqual(test_viz.all_metrics, expect_metric_labels)

def test_get_fillna_returns_default_on_null_columns(self):
form_data = {
'viz_type': 'table',
Expand Down

0 comments on commit ab5c910

Please sign in to comment.