Skip to content

Commit

Permalink
fix: granularity_sqla and GENERIC_CHART_AXES (#25213)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored and michael-s-molina committed Sep 7, 2023
1 parent 80df8bc commit 2ae9d2e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
25 changes: 21 additions & 4 deletions superset/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@
"""Code related with dealing with legacy / change management"""
from typing import Any

from superset import is_feature_enabled


def update_time_range(form_data: dict[str, Any]) -> None:
"""Move since and until to time_range."""
"""
Legacy adjustments to time range.
- Move `since` and `until` to `time_range`.
- Define `time_range` when `granularity_sqla` is set and unfiltered.
"""
if "since" in form_data or "until" in form_data:
form_data["time_range"] = "{} : {}".format(
form_data.pop("since", "") or "", form_data.pop("until", "") or ""
)
since = form_data.pop("since", "") or ""
until = form_data.pop("until", "") or ""
form_data["time_range"] = f"{since} : {until}"

if is_feature_enabled("GENERIC_CHART_AXES"):
if temporal_column := form_data.get("granularity_sqla"):
if any(
adhoc_filter.get("subject") == temporal_column
and adhoc_filter.get("comparator") == "No filter"
for adhoc_filter in form_data.get("adhoc_filters", [])
):
form_data.setdefault("time_range", "No filter")
100 changes: 100 additions & 0 deletions tests/unit_tests/legacy_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=invalid-name, use-implicit-booleaness-not-comparison

import copy
from typing import Any

from superset.legacy import update_time_range
from tests.unit_tests.conftest import with_feature_flags

original_form_data = {
"granularity_sqla": "order_date",
"datasource": "22__table",
"viz_type": "table",
"query_mode": "raw",
"groupby": [],
"time_grain_sqla": "P1D",
"temporal_columns_lookup": {"order_date": True},
"all_columns": ["order_date", "state", "product_code"],
"percent_metrics": [],
"adhoc_filters": [
{
"clause": "WHERE",
"subject": "order_date",
"operator": "TEMPORAL_RANGE",
"comparator": "No filter",
"expressionType": "SIMPLE",
}
],
"order_by_cols": [],
"row_limit": 1000,
"server_page_length": 10,
"order_desc": True,
"table_timestamp_format": "smart_date",
"show_cell_bars": True,
"color_pn": True,
"extra_form_data": {},
"dashboards": [19],
"force": False,
"result_format": "json",
"result_type": "full",
"include_time": False,
}


def test_update_time_range_since_until() -> None:
"""
Tests for the old `since` and `until` parameters.
"""
form_data: dict[str, Any]

form_data = {}
update_time_range(form_data)
assert form_data == {}

form_data = {"since": "yesterday"}
update_time_range(form_data)
assert form_data == {"time_range": "yesterday : "}

form_data = {"until": "tomorrow"}
update_time_range(form_data)
assert form_data == {"time_range": " : tomorrow"}

form_data = {"since": "yesterday", "until": "tomorrow"}
update_time_range(form_data)
assert form_data == {"time_range": "yesterday : tomorrow"}


@with_feature_flags(GENERIC_CHART_AXES=False)
def test_update_time_range_granularity_sqla_no_feature_flag() -> None:
"""
Tests for the unfiltered `granularity_sqla` when `GENERIC_CHART_AXES` is off.
"""
form_data = copy.deepcopy(original_form_data)
update_time_range(form_data)
assert form_data == original_form_data


@with_feature_flags(GENERIC_CHART_AXES=True)
def test_update_time_range_granularity_sqla_with_feature_flag() -> None:
"""
Tests for the unfiltered `granularity_sqla` when `GENERIC_CHART_AXES` is on.
"""
form_data = copy.deepcopy(original_form_data)
update_time_range(form_data)
assert form_data["time_range"] == "No filter"

0 comments on commit 2ae9d2e

Please sign in to comment.