Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Fixing regression introduced in #4396 #4500

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the df.empty check is only required by the test_get_df_returns_empty_df unit test due to the mocking. Otherwise it would be safe to proceed with an empty pd.DataFrame.

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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more consistent to log the payload error as opposed to throwing an exception here.

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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try/except block may no longer be required per the PR description. This would make the stacktrace obsolete as well.

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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this for consistency, i.e. get_df(...) should return a 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