feat: Adds Histogram chart migration logic (#28780)

This commit is contained in:
Michael S. Molina
2024-06-05 13:33:50 -03:00
committed by GitHub
parent dabb4e064f
commit df0b1cb8ed
11 changed files with 118 additions and 6 deletions

View File

@@ -0,0 +1,52 @@
# 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.
from typing import Any
from superset.migrations.shared.migrate_viz import MigrateHistogramChart
from tests.unit_tests.migrations.viz.utils import migrate_and_assert
SOURCE_FORM_DATA: dict[str, Any] = {
"all_columns_x": ["category"],
"adhoc_filters": [],
"cumulative": True,
"linear_color_scheme": "blue",
"link_length": "5",
"normalized": True,
"row_limit": 100,
"viz_type": "histogram",
"x_axis_label": "X",
"y_axis_label": "Y",
}
TARGET_FORM_DATA: dict[str, Any] = {
"adhoc_filters": [],
"bins": 5,
"column": "category",
"cumulative": True,
"form_data_bak": SOURCE_FORM_DATA,
"groupby": [],
"linear_color_scheme": "blue",
"normalize": True,
"row_limit": 100,
"viz_type": "histogram_v2",
"x_axis_title": "X",
"y_axis_title": "Y",
}
def test_migration() -> None:
migrate_and_assert(MigrateHistogramChart, SOURCE_FORM_DATA, TARGET_FORM_DATA)

View File

@@ -120,3 +120,25 @@ def test_histogram_with_non_numeric_column():
histogram(data, "b", ["group"], bins)
except ValueError as e:
assert str(e) == "The column 'b' must be numeric."
# test histogram ignore null values
def test_histogram_ignore_null_values():
data_with_null = DataFrame(
{
"group": ["A", "A", "B", "B", "A", "A", "B", "B", "A", "A"],
"a": [1, 2, 3, 4, 5, 6, 7, 8, 9, None],
"b": [1, 2, 3, 4, 5, 6, 7, 8, 9, None],
}
)
result = histogram(data_with_null, "a", ["group"], bins)
assert result.shape == (2, bins + 1)
assert result.columns.tolist() == [
"group",
"1 - 2",
"2 - 4",
"4 - 5",
"5 - 7",
"7 - 9",
]
assert result.values.tolist() == [["A", 2, 0, 1, 1, 1], ["B", 0, 2, 0, 1, 1]]