Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Nov 13, 2023
1 parent 5f59515 commit 2f59321
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 224 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
[pytest]
testpaths =
tests
python_files = *_test.py test_*.py *_tests.py
python_files = *_test.py test_*.py *_tests.py *viz/utils.py
17 changes: 11 additions & 6 deletions superset/migrations/shared/migrate_viz/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ class TimeseriesChart(MigrateViz):
rename_keys = {
"bottom_margin": "x_axis_title_margin",
"left_margin": "y_axis_title_margin",
"show_controls": "show_extra_controls",
"x_axis_label": "x_axis_title",
"x_axis_format": "x_axis_time_format",
"x_ticks_layout": "xAxisLabelRotation",
"y_axis_label": "y_axis_title",
"y_axis_showminmax": "truncateYAxis",
"y_log_scale": "logAxis",
}
remove_keys = {"contribution", "show_brush", "show_markers"}

def _pre_action(self) -> None:
self.data["contributionMode"] = "row" if self.data.get("contribution") else None
Expand All @@ -139,6 +141,9 @@ def _pre_action(self) -> None:
"difference" if comparison_type == "absolute" else comparison_type
)

if x_ticks_layout := self.data.get("x_ticks_layout"):
self.data["x_ticks_layout"] = 45 if x_ticks_layout == "45°" else 0


class MigrateLineChart(TimeseriesChart):
source_viz_type = "line"
Expand All @@ -147,6 +152,8 @@ class MigrateLineChart(TimeseriesChart):
def _pre_action(self) -> None:
super()._pre_action()

self.remove_keys.add("line_interpolation")

line_interpolation = self.data.get("line_interpolation")
if line_interpolation == "cardinal":
self.target_viz_type = "echarts_timeseries_smooth"
Expand All @@ -161,7 +168,6 @@ def _pre_action(self) -> None:
class MigrateAreaChart(TimeseriesChart):
source_viz_type = "area"
target_viz_type = "echarts_area"
remove_keys = {"contribution", "stacked_style", "x_axis_label"}
stacked_map = {
"expand": "Expand",
"stack": "Stack",
Expand All @@ -171,11 +177,10 @@ class MigrateAreaChart(TimeseriesChart):
def _pre_action(self) -> None:
super()._pre_action()

self.data["stack"] = (
self.stacked_map.get(self.data.get("stacked_style")) or "Stack"
)
self.remove_keys.add("stacked_style")

if x_ticks_layout := self.data.get("x_ticks_layout"):
self.data["x_ticks_layout"] = 45 if x_ticks_layout == "45°" else 0
self.data["stack"] = self.stacked_map.get(
self.data.get("stacked_style") or "stack"
)

self.data["opacity"] = 0.7

This file was deleted.

35 changes: 5 additions & 30 deletions tests/unit_tests/migrations/viz/dual_line_to_mixed_chart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
from typing import Any

from superset.migrations.shared.migrate_viz import MigrateDualLine
from tests.unit_tests.migrations.viz.utils import migrate_and_assert

ADHOC_FILTERS = [
{
Expand All @@ -28,7 +29,7 @@
}
]

SOURCE_FORM_DATA = {
SOURCE_FORM_DATA: dict[str, Any] = {
"metric": "num_boys",
"y_axis_format": ",d",
"y_axis_bounds": [50, 100],
Expand All @@ -42,7 +43,7 @@
"yAxisIndex": 0,
}

TARGET_FORM_DATA = {
TARGET_FORM_DATA: dict[str, Any] = {
"metrics": ["num_boys"],
"y_axis_format": ",d",
"y_axis_bounds": [50, 100],
Expand All @@ -68,30 +69,4 @@ def test_migration() -> None:


def upgrade_downgrade(source, target) -> None:
from superset.models.slice import Slice

dumped_form_data = json.dumps(source)

slc = Slice(
viz_type=MigrateDualLine.source_viz_type,
datasource_type="table",
params=dumped_form_data,
query_context=f'{{"form_data": {dumped_form_data}}}',
)

# upgrade
slc = MigrateDualLine.upgrade_slice(slc)

# verify form_data
new_form_data = json.loads(slc.params)
assert new_form_data == target
assert new_form_data["form_data_bak"] == source

# verify query_context
new_query_context = json.loads(slc.query_context)
assert new_query_context["form_data"]["viz_type"] == "mixed_timeseries"

# downgrade
slc = MigrateDualLine.downgrade_slice(slc)
assert slc.viz_type == MigrateDualLine.source_viz_type
assert json.loads(slc.params) == source
migrate_and_assert(MigrateDualLine, source, target)
42 changes: 42 additions & 0 deletions tests/unit_tests/migrations/viz/nvd3_area_chart_to_echarts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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 MigrateAreaChart
from tests.unit_tests.migrations.viz.utils import (
migrate_and_assert,
TIMESERIES_SOURCE_FORM_DATA,
TIMESERIES_TARGET_FORM_DATA,
)

SOURCE_FORM_DATA: dict[str, Any] = {
"viz_type": "area",
"stacked_style": "stream",
}

TARGET_FORM_DATA: dict[str, Any] = {
"form_data_bak": SOURCE_FORM_DATA,
"viz_type": "echarts_area",
"opacity": 0.7,
"stack": "Stream",
}


def test_migration() -> None:
SOURCE_FORM_DATA.update(TIMESERIES_SOURCE_FORM_DATA)
TARGET_FORM_DATA.update(TIMESERIES_TARGET_FORM_DATA)
migrate_and_assert(MigrateAreaChart, SOURCE_FORM_DATA, TARGET_FORM_DATA)
39 changes: 39 additions & 0 deletions tests/unit_tests/migrations/viz/nvd3_line_chart_to_echarts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 MigrateLineChart
from tests.unit_tests.migrations.viz.utils import (
migrate_and_assert,
TIMESERIES_SOURCE_FORM_DATA,
TIMESERIES_TARGET_FORM_DATA,
)

SOURCE_FORM_DATA: dict[str, Any] = {
"viz_type": "line",
}

TARGET_FORM_DATA: dict[str, Any] = {
"form_data_bak": SOURCE_FORM_DATA,
"viz_type": "echarts_timeseries_line",
}


def test_migration() -> None:
SOURCE_FORM_DATA.update(TIMESERIES_SOURCE_FORM_DATA)
TARGET_FORM_DATA.update(TIMESERIES_TARGET_FORM_DATA)
migrate_and_assert(MigrateLineChart, SOURCE_FORM_DATA, TARGET_FORM_DATA)
Loading

0 comments on commit 2f59321

Please sign in to comment.