From baf3ab2bda1ae60c686880dee6e98043daef2915 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 6 Sep 2023 20:09:15 -0700 Subject: [PATCH] fix: granularity_sqla and GENERIC_CHART_AXES (#25213) (cherry picked from commit 749274e635248d2506315c0dd7b7376797c18ae4) --- superset/legacy.py | 25 ++++++-- tests/unit_tests/legacy_tests.py | 100 +++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tests/unit_tests/legacy_tests.py diff --git a/superset/legacy.py b/superset/legacy.py index 7c25d2de984df..52b63a013b7c0 100644 --- a/superset/legacy.py +++ b/superset/legacy.py @@ -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" - ] = f'{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") diff --git a/tests/unit_tests/legacy_tests.py b/tests/unit_tests/legacy_tests.py new file mode 100644 index 0000000000000..9a8f7fbcd2c1a --- /dev/null +++ b/tests/unit_tests/legacy_tests.py @@ -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"