fix: show error if rolling window returns empty df (#10572)

* fix: show error if rolling window returns empty df

* add test
This commit is contained in:
Ville Brofeldt 2020-08-13 20:51:03 +03:00 committed by GitHub
parent 11da6ee812
commit c0ebd7f434
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -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]]:

View File

@ -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):