Skip to content

Commit

Permalink
Merge pull request apache#4500 from john-bodley/john-bodley-fix-pr-4396
Browse files Browse the repository at this point in the history
[bugfix] Fixing regression introduced in apache#4396
  • Loading branch information
john-bodley authored Mar 6, 2018
2 parents 007ad35 + 7440d34 commit 48430a1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
17 changes: 8 additions & 9 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down
28 changes: 28 additions & 0 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 0 additions & 3 deletions tests/viz_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 48430a1

Please sign in to comment.