From ca680cf976cb8361db91d38cd6a217abb4108889 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Tue, 22 Dec 2020 14:19:05 -0800 Subject: [PATCH] fix: append ID to filename when exporting chart (#12166) --- superset/charts/commands/export.py | 2 +- tests/charts/commands_tests.py | 14 +++++++++----- tests/dashboards/commands_tests.py | 14 ++++---------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/superset/charts/commands/export.py b/superset/charts/commands/export.py index 88a87d901..7fc778fef 100644 --- a/superset/charts/commands/export.py +++ b/superset/charts/commands/export.py @@ -45,7 +45,7 @@ class ExportChartsCommand(ExportModelsCommand): @staticmethod def _export(model: Slice) -> Iterator[Tuple[str, str]]: chart_slug = secure_filename(model.slice_name) - file_name = f"charts/{chart_slug}.yaml" + file_name = f"charts/{chart_slug}_{model.id}.yaml" payload = model.export_to_dict( recursive=False, diff --git a/tests/charts/commands_tests.py b/tests/charts/commands_tests.py index 8d8ecb812..d1e862eab 100644 --- a/tests/charts/commands_tests.py +++ b/tests/charts/commands_tests.py @@ -49,20 +49,22 @@ class TestExportChartsCommand(SupersetTestCase): mock_g.user = security_manager.find_user("admin") example_chart = ( - db.session.query(Slice).filter_by(slice_name="Energy Sankey").one_or_none() + db.session.query(Slice).filter_by(slice_name="Energy Sankey").one() ) command = ExportChartsCommand([example_chart.id]) contents = dict(command.run()) expected = [ "metadata.yaml", - "charts/Energy_Sankey.yaml", + f"charts/Energy_Sankey_{example_chart.id}.yaml", "datasets/examples/energy_usage.yaml", "databases/examples.yaml", ] assert expected == list(contents.keys()) - metadata = yaml.safe_load(contents["charts/Energy_Sankey.yaml"]) + metadata = yaml.safe_load( + contents[f"charts/Energy_Sankey_{example_chart.id}.yaml"] + ) assert metadata == { "slice_name": "Energy Sankey", "viz_type": "sankey", @@ -107,12 +109,14 @@ class TestExportChartsCommand(SupersetTestCase): mock_g.user = security_manager.find_user("admin") example_chart = ( - db.session.query(Slice).filter_by(slice_name="Energy Sankey").one_or_none() + db.session.query(Slice).filter_by(slice_name="Energy Sankey").one() ) command = ExportChartsCommand([example_chart.id]) contents = dict(command.run()) - metadata = yaml.safe_load(contents["charts/Energy_Sankey.yaml"]) + metadata = yaml.safe_load( + contents[f"charts/Energy_Sankey_{example_chart.id}.yaml"] + ) assert list(metadata.keys()) == [ "slice_name", "viz_type", diff --git a/tests/dashboards/commands_tests.py b/tests/dashboards/commands_tests.py index 31c6f6098..8be5bcc98 100644 --- a/tests/dashboards/commands_tests.py +++ b/tests/dashboards/commands_tests.py @@ -21,6 +21,7 @@ from unittest.mock import patch import pytest import yaml +from werkzeug.utils import secure_filename from superset import db, security_manager from superset.commands.exceptions import CommandInvalidError @@ -58,19 +59,12 @@ class TestExportDashboardsCommand(SupersetTestCase): expected_paths = { "metadata.yaml", "dashboards/World_Banks_Data.yaml", - "charts/Region_Filter.yaml", "datasets/examples/wb_health_population.yaml", "databases/examples.yaml", - "charts/Worlds_Population.yaml", - "charts/Most_Populated_Countries.yaml", - "charts/Growth_Rate.yaml", - "charts/Rural.yaml", - "charts/Life_Expectancy_VS_Rural.yaml", - "charts/Rural_Breakdown.yaml", - "charts/Worlds_Pop_Growth.yaml", - "charts/Box_plot.yaml", - "charts/Treemap.yaml", } + for chart in example_dashboard.slices: + chart_slug = secure_filename(chart.slice_name) + expected_paths.add(f"charts/{chart_slug}_{chart.id}.yaml") assert expected_paths == set(contents.keys()) metadata = yaml.safe_load(contents["dashboards/World_Banks_Data.yaml"])