diff --git a/superset/viz.py b/superset/viz.py index 31e06967a1285..3595ebf767fa0 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -151,9 +151,6 @@ def get_df(self, query_obj=None): # If the datetime format is unix, the parse will use the corresponding # parsing logic. if df is None or df.empty: - self.status = utils.QueryStatus.FAILED - if not self.error_message: - self.error_message = 'No data.' return pd.DataFrame() else: if DTTM_ALIAS in df.columns: @@ -290,10 +287,11 @@ def get_payload(self, query_obj=None): payload = self.get_df_payload(query_obj) df = payload.get('df') - if df is not None and len(df.index) == 0: - raise Exception('No data') if self.status != utils.QueryStatus.FAILED: - payload['data'] = self.get_data(df) + if df is None or df.empty: + payload['error'] = 'No data' + else: + payload['data'] = self.get_data(df) if 'df' in payload: del payload['df'] return payload @@ -327,8 +325,9 @@ def get_df_payload(self, query_obj=None): if query_obj and not is_loaded: try: df = self.get_df(query_obj) - stats_logger.incr('loaded_from_source') - is_loaded = True + if self.status != utils.QueryStatus.FAILED: + stats_logger.incr('loaded_from_source') + is_loaded = True except Exception as e: logging.exception(e) if not self.error_message: @@ -612,7 +611,7 @@ def query_obj(self): return None def get_df(self, query_obj=None): - return None + return pd.DataFrame() def get_data(self, df): markup_type = self.form_data.get('markup_type') diff --git a/tests/core_tests.py b/tests/core_tests.py index 91ae56f158679..43a3bdfc716c7 100644 --- a/tests/core_tests.py +++ b/tests/core_tests.py @@ -909,6 +909,34 @@ def test_slice_url_overrides(self): resp = self.get_resp(url) assert '"CA"' in resp + def test_slice_payload_no_data(self): + self.login(username='admin') + slc = self.get_slice('Girls', db.session) + + url = slc.get_explore_url( + base_url='/superset/explore_json', + overrides={ + 'filters': [{'col': 'state', 'op': 'in', 'val': ['N/A']}], + }, + ) + + data = self.get_json_resp(url) + self.assertEqual(data['status'], utils.QueryStatus.SUCCESS) + assert 'No data' in data['error'] + + def test_slice_payload_invalid_query(self): + self.login(username='admin') + slc = self.get_slice('Girls', db.session) + + url = slc.get_explore_url( + base_url='/superset/explore_json', + overrides={'groupby': ['N/A']}, + ) + + data = self.get_json_resp(url) + self.assertEqual(data['status'], utils.QueryStatus.FAILED) + assert 'KeyError' in data['stacktrace'] + if __name__ == '__main__': unittest.main() diff --git a/tests/viz_tests.py b/tests/viz_tests.py index 2fe73677bd19d..6822837e28312 100644 --- a/tests/viz_tests.py +++ b/tests/viz_tests.py @@ -10,7 +10,6 @@ from mock import Mock, patch import pandas as pd -import superset.utils as utils from superset.utils import DTTM_ALIAS import superset.viz as viz @@ -53,8 +52,6 @@ def test_get_df_returns_empty_df(self): result = test_viz.get_df(query_obj) self.assertEqual(type(result), pd.DataFrame) self.assertTrue(result.empty) - self.assertEqual(test_viz.error_message, 'No data.') - self.assertEqual(test_viz.status, utils.QueryStatus.FAILED) def test_get_df_handles_dttm_col(self): datasource = Mock()