feat(tags): Export and Import Functionality for Superset Dashboards and Charts (#30833)

Co-authored-by: Asher Manangan <amanangan@powercosts.com>
This commit is contained in:
Asher Manangan
2025-04-08 03:12:22 +08:00
committed by GitHub
parent e1383d3821
commit fd947a097d
18 changed files with 512 additions and 18 deletions

View File

@@ -16,9 +16,15 @@
# under the License.
# pylint: disable=invalid-name, unused-argument, import-outside-toplevel
from unittest.mock import patch
import pytest
import yaml
from freezegun import freeze_time
from pytest_mock import MockerFixture
from superset.extensions import feature_flag_manager
def test_export_assets_command(mocker: MockerFixture) -> None:
"""
@@ -80,7 +86,6 @@ def test_export_assets_command(mocker: MockerFixture) -> None:
with freeze_time("2022-01-01T00:00:00Z"):
command = ExportAssetsCommand()
output = [(file[0], file[1]()) for file in list(command.run())]
assert output == [
(
"metadata.yaml",
@@ -92,3 +97,61 @@ def test_export_assets_command(mocker: MockerFixture) -> None:
("dashboards/sales.yaml", "<DASHBOARD CONTENTS>"),
("queries/example/metric.yaml", "<SAVED QUERY CONTENTS>"),
]
@pytest.fixture
def mock_export_tags_command_charts_dashboards(mocker):
export_tags = mocker.patch("superset.commands.tag.export.ExportTagsCommand")
def _mock_export(dashboard_ids=None, chart_ids=None):
if not feature_flag_manager.is_feature_enabled("TAGGING_SYSTEM"):
return iter([])
return [
(
"tags.yaml",
lambda: yaml.dump(
{
"tags": [
{
"tag_name": "tag_1",
"description": "Description for tag_1",
}
]
},
sort_keys=False,
),
),
("charts/pie.yaml", lambda: "tag:\n- tag_1"),
]
export_tags.return_value._export.side_effect = _mock_export
return export_tags
def test_export_tags_with_charts_dashboards(
mock_export_tags_command_charts_dashboards, mocker
):
with patch.object(feature_flag_manager, "is_feature_enabled", return_value=True):
command = mock_export_tags_command_charts_dashboards()
result = list(command._export(chart_ids=[1]))
file_name, file_content_func = result[0]
file_content = file_content_func()
assert file_name == "tags.yaml"
payload = yaml.safe_load(file_content)
assert payload["tags"] == [
{"tag_name": "tag_1", "description": "Description for tag_1"}
]
file_name, file_content_func = result[1]
file_content = file_content_func()
assert file_name == "charts/pie.yaml"
assert file_content == "tag:\n- tag_1"
with patch.object(feature_flag_manager, "is_feature_enabled", return_value=False):
command = mock_export_tags_command_charts_dashboards()
result = list(command._export(chart_ids=[1]))
assert not any(file_name == "tags.yaml" for file_name, _ in result)
assert all(
file_content_func() != "tag:\n- tag_1" for _, file_content_func in result
)