2020-06-26 17:34:45 -04:00
|
|
|
# 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.
|
|
|
|
# isort:skip_file
|
|
|
|
import copy
|
|
|
|
import json
|
2021-04-29 17:14:26 -04:00
|
|
|
import time
|
2023-01-05 12:10:40 -05:00
|
|
|
from unittest.mock import patch
|
2021-01-13 17:20:05 -05:00
|
|
|
import pytest
|
|
|
|
|
2021-07-01 11:03:07 -04:00
|
|
|
import tests.integration_tests.test_app # pylint: disable=unused-import
|
2023-01-05 12:10:40 -05:00
|
|
|
from superset import db, security_manager
|
2020-06-26 17:34:45 -04:00
|
|
|
from superset.dashboards.dao import DashboardDAO
|
|
|
|
from superset.models.dashboard import Dashboard
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.base_tests import SupersetTestCase
|
|
|
|
from tests.integration_tests.fixtures.world_bank_dashboard import (
|
|
|
|
load_world_bank_dashboard_with_slices,
|
2021-12-16 19:11:47 -05:00
|
|
|
load_world_bank_data,
|
2021-07-01 11:03:07 -04:00
|
|
|
)
|
2020-06-26 17:34:45 -04:00
|
|
|
|
|
|
|
|
2020-06-29 18:36:06 -04:00
|
|
|
class TestDashboardDAO(SupersetTestCase):
|
2021-01-13 17:20:05 -05:00
|
|
|
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
|
2020-06-26 17:34:45 -04:00
|
|
|
def test_set_dash_metadata(self):
|
2022-05-13 20:31:23 -04:00
|
|
|
dash: Dashboard = (
|
|
|
|
db.session.query(Dashboard).filter_by(slug="world_health").first()
|
|
|
|
)
|
2020-06-26 17:34:45 -04:00
|
|
|
data = dash.data
|
|
|
|
positions = data["position_json"]
|
|
|
|
data.update({"positions": positions})
|
|
|
|
original_data = copy.deepcopy(data)
|
|
|
|
|
|
|
|
# add filter scopes
|
2022-05-13 20:31:23 -04:00
|
|
|
filter_slice = next(slc for slc in dash.slices if slc.viz_type == "filter_box")
|
|
|
|
immune_slices = [slc for slc in dash.slices if slc != filter_slice]
|
2020-06-26 17:34:45 -04:00
|
|
|
filter_scopes = {
|
|
|
|
str(filter_slice.id): {
|
|
|
|
"region": {
|
|
|
|
"scope": ["ROOT_ID"],
|
|
|
|
"immune": [slc.id for slc in immune_slices],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.update({"filter_scopes": json.dumps(filter_scopes)})
|
|
|
|
DashboardDAO.set_dash_metadata(dash, data)
|
|
|
|
updated_metadata = json.loads(dash.json_metadata)
|
|
|
|
self.assertEqual(updated_metadata["filter_scopes"], filter_scopes)
|
|
|
|
|
|
|
|
# remove a slice and change slice ids (as copy slices)
|
|
|
|
removed_slice = immune_slices.pop()
|
2022-05-13 20:31:23 -04:00
|
|
|
removed_components = [
|
2020-06-26 17:34:45 -04:00
|
|
|
key
|
|
|
|
for (key, value) in positions.items()
|
|
|
|
if isinstance(value, dict)
|
|
|
|
and value.get("type") == "CHART"
|
|
|
|
and value["meta"]["chartId"] == removed_slice.id
|
|
|
|
]
|
2022-05-13 20:31:23 -04:00
|
|
|
for component_id in removed_components:
|
|
|
|
del positions[component_id]
|
2020-06-26 17:34:45 -04:00
|
|
|
|
|
|
|
data.update({"positions": positions})
|
|
|
|
DashboardDAO.set_dash_metadata(dash, data)
|
|
|
|
updated_metadata = json.loads(dash.json_metadata)
|
|
|
|
expected_filter_scopes = {
|
|
|
|
str(filter_slice.id): {
|
|
|
|
"region": {
|
|
|
|
"scope": ["ROOT_ID"],
|
|
|
|
"immune": [slc.id for slc in immune_slices],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertEqual(updated_metadata["filter_scopes"], expected_filter_scopes)
|
|
|
|
|
|
|
|
# reset dash to original data
|
|
|
|
DashboardDAO.set_dash_metadata(dash, original_data)
|
2021-04-29 17:14:26 -04:00
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
|
2023-01-05 12:10:40 -05:00
|
|
|
@patch("superset.utils.core.g")
|
|
|
|
@patch("superset.security.manager.g")
|
|
|
|
def test_get_dashboard_changed_on(self, mock_sm_g, mock_g):
|
|
|
|
mock_g.user = mock_sm_g.user = security_manager.find_user("admin")
|
|
|
|
with self.client.application.test_request_context():
|
|
|
|
self.login(username="admin")
|
|
|
|
dashboard = (
|
|
|
|
db.session.query(Dashboard).filter_by(slug="world_health").first()
|
|
|
|
)
|
2021-04-29 17:14:26 -04:00
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
changed_on = dashboard.changed_on.replace(microsecond=0)
|
|
|
|
assert changed_on == DashboardDAO.get_dashboard_changed_on(dashboard)
|
|
|
|
assert changed_on == DashboardDAO.get_dashboard_changed_on("world_health")
|
2021-04-29 17:14:26 -04:00
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
old_changed_on = dashboard.changed_on
|
2021-04-29 17:14:26 -04:00
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
# freezegun doesn't work for some reason, so we need to sleep here :(
|
|
|
|
time.sleep(1)
|
|
|
|
data = dashboard.data
|
|
|
|
positions = data["position_json"]
|
|
|
|
data.update({"positions": positions})
|
|
|
|
original_data = copy.deepcopy(data)
|
2021-04-29 17:14:26 -04:00
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
data.update({"foo": "bar"})
|
|
|
|
DashboardDAO.set_dash_metadata(dashboard, data)
|
|
|
|
db.session.merge(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
new_changed_on = DashboardDAO.get_dashboard_changed_on(dashboard)
|
|
|
|
assert old_changed_on.replace(microsecond=0) < new_changed_on
|
|
|
|
assert new_changed_on == DashboardDAO.get_dashboard_and_datasets_changed_on(
|
|
|
|
dashboard
|
|
|
|
)
|
|
|
|
assert new_changed_on == DashboardDAO.get_dashboard_and_slices_changed_on(
|
|
|
|
dashboard
|
|
|
|
)
|
2021-04-29 17:14:26 -04:00
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
DashboardDAO.set_dash_metadata(dashboard, original_data)
|
|
|
|
db.session.merge(dashboard)
|
|
|
|
db.session.commit()
|
2023-04-06 11:32:10 -04:00
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
|
|
|
|
@patch("superset.dashboards.dao.g")
|
|
|
|
def test_copy_dashboard(self, mock_g):
|
|
|
|
mock_g.user = security_manager.find_user("admin")
|
|
|
|
original_dash = (
|
|
|
|
db.session.query(Dashboard).filter_by(slug="world_health").first()
|
|
|
|
)
|
|
|
|
metadata = json.loads(original_dash.json_metadata)
|
|
|
|
metadata["positions"] = original_dash.position
|
|
|
|
dash_data = {
|
|
|
|
"dashboard_title": "copied dash",
|
|
|
|
"json_metadata": json.dumps(metadata),
|
|
|
|
"css": "<css>",
|
|
|
|
"duplicate_slices": False,
|
|
|
|
}
|
|
|
|
dash = DashboardDAO.copy_dashboard(original_dash, dash_data)
|
|
|
|
self.assertNotEqual(dash.id, original_dash.id)
|
|
|
|
self.assertEqual(len(dash.position), len(original_dash.position))
|
|
|
|
self.assertEqual(dash.dashboard_title, "copied dash")
|
|
|
|
self.assertEqual(dash.css, "<css>")
|
|
|
|
self.assertEqual(dash.owners, [security_manager.find_user("admin")])
|
|
|
|
self.assertCountEqual(dash.slices, original_dash.slices)
|
|
|
|
|
|
|
|
db.session.delete(dash)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
|
|
|
|
@patch("superset.dashboards.dao.g")
|
|
|
|
def test_copy_dashboard_duplicate_slices(self, mock_g):
|
|
|
|
mock_g.user = security_manager.find_user("admin")
|
|
|
|
original_dash = (
|
|
|
|
db.session.query(Dashboard).filter_by(slug="world_health").first()
|
|
|
|
)
|
|
|
|
metadata = json.loads(original_dash.json_metadata)
|
|
|
|
metadata["positions"] = original_dash.position
|
|
|
|
dash_data = {
|
|
|
|
"dashboard_title": "copied dash",
|
|
|
|
"json_metadata": json.dumps(metadata),
|
|
|
|
"css": "<css>",
|
|
|
|
"duplicate_slices": True,
|
|
|
|
}
|
|
|
|
dash = DashboardDAO.copy_dashboard(original_dash, dash_data)
|
|
|
|
self.assertNotEqual(dash.id, original_dash.id)
|
|
|
|
self.assertEqual(len(dash.position), len(original_dash.position))
|
|
|
|
self.assertEqual(dash.dashboard_title, "copied dash")
|
|
|
|
self.assertEqual(dash.css, "<css>")
|
|
|
|
self.assertEqual(dash.owners, [security_manager.find_user("admin")])
|
|
|
|
self.assertEqual(len(dash.slices), len(original_dash.slices))
|
|
|
|
for original_slc in original_dash.slices:
|
|
|
|
for slc in dash.slices:
|
|
|
|
self.assertNotEqual(slc.id, original_slc.id)
|
|
|
|
|
|
|
|
for slc in dash.slices:
|
|
|
|
db.session.delete(slc)
|
|
|
|
db.session.delete(dash)
|
|
|
|
db.session.commit()
|