This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 0.37 in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
commit d06e023d884d1acd3e1fd9e2d2fe2b937e3e6c8a Author: Ville Brofeldt <[email protected]> AuthorDate: Thu Aug 13 20:51:03 2020 +0300 fix: show error if rolling window returns empty df (#10572) * fix: show error if rolling window returns empty df * add test --- superset/viz.py | 8 ++++++++ tests/viz_tests.py | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/superset/viz.py b/superset/viz.py index 53acbda..994f47c 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -216,6 +216,14 @@ class BaseViz: df = df.cumsum() if min_periods: df = df[min_periods:] + if df.empty: + raise QueryObjectValidationError( + _( + "Applied rolling window did not return any data. Please make sure " + "the source query satisfies the minimum periods defined in the " + "rolling window." + ) + ) return df def get_samples(self) -> List[Dict[str, Any]]: diff --git a/tests/viz_tests.py b/tests/viz_tests.py index b76c95c..b52ed7f 100644 --- a/tests/viz_tests.py +++ b/tests/viz_tests.py @@ -24,12 +24,13 @@ from typing import Any, Dict, List, Set import numpy as np import pandas as pd +import pytest import tests.test_app import superset.viz as viz from superset import app from superset.constants import NULL_STRING -from superset.exceptions import SpatialException +from superset.exceptions import QueryObjectValidationError, SpatialException from superset.utils.core import DTTM_ALIAS from .base_tests import SupersetTestCase @@ -1258,6 +1259,26 @@ class TestTimeSeriesViz(SupersetTestCase): [1.0, 1.5, 2.0, 2.5], ) + def test_apply_rolling_without_data(self): + datasource = self.get_datasource_mock() + df = pd.DataFrame( + index=pd.to_datetime( + ["2019-01-01", "2019-01-02", "2019-01-05", "2019-01-07"] + ), + data={"y": [1.0, 2.0, 3.0, 4.0]}, + ) + test_viz = viz.BigNumberViz( + datasource, + { + "metrics": ["y"], + "rolling_type": "cumsum", + "rolling_periods": 4, + "min_periods": 4, + }, + ) + with pytest.raises(QueryObjectValidationError): + test_viz.apply_rolling(df) + class TestBigNumberViz(SupersetTestCase): def test_get_data(self):
