From c0ebd7f434b2c6902dd79a8c2ba46ca71fafd329 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt <33317356+villebro@users.noreply.github.com> Date: Thu, 13 Aug 2020 20:51:03 +0300 Subject: [PATCH] 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 88d67bb03..34054cb40 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 3689901dc..637cd33cc 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 @@ -1266,6 +1267,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):