mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
refactor copy filter_scopes and add tests (#9224)
* refactor copy filter_scopes and add tests * fix review comments
This commit is contained in:
@@ -16,12 +16,14 @@
|
||||
# under the License.
|
||||
# isort:skip_file
|
||||
"""Unit tests for Superset"""
|
||||
import copy
|
||||
import json
|
||||
import unittest
|
||||
from random import random
|
||||
|
||||
from flask import escape
|
||||
from sqlalchemy import func
|
||||
from typing import Dict
|
||||
|
||||
import tests.test_app
|
||||
from superset import db, security_manager
|
||||
@@ -29,6 +31,7 @@ from superset.connectors.sqla.models import SqlaTable
|
||||
from superset.models import core as models
|
||||
from superset.models.dashboard import Dashboard
|
||||
from superset.models.slice import Slice
|
||||
from superset.views import core as views
|
||||
|
||||
from .base_tests import SupersetTestCase
|
||||
|
||||
@@ -52,7 +55,7 @@ class DashboardTests(SupersetTestCase):
|
||||
for i, slc in enumerate(dash.slices):
|
||||
id = "DASHBOARD_CHART_TYPE-{}".format(i)
|
||||
d = {
|
||||
"type": "DASHBOARD_CHART_TYPE",
|
||||
"type": "CHART",
|
||||
"id": id,
|
||||
"children": [],
|
||||
"meta": {"width": 4, "height": 50, "chartId": slc.id},
|
||||
@@ -235,6 +238,58 @@ class DashboardTests(SupersetTestCase):
|
||||
if key not in ["modified", "changed_on"]:
|
||||
self.assertEqual(slc[key], resp["slices"][index][key])
|
||||
|
||||
def test_set_dash_metadata(self, username="admin"):
|
||||
self.login(username=username)
|
||||
dash = db.session.query(Dashboard).filter_by(slug="world_health").first()
|
||||
data = dash.data
|
||||
positions = data["position_json"]
|
||||
data.update({"positions": positions})
|
||||
original_data = copy.deepcopy(data)
|
||||
|
||||
# add filter scopes
|
||||
filter_slice = dash.slices[0]
|
||||
immune_slices = dash.slices[2:]
|
||||
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)})
|
||||
views.Superset._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()
|
||||
removed_component = [
|
||||
key
|
||||
for (key, value) in positions.items()
|
||||
if isinstance(value, dict)
|
||||
and value.get("type") == "CHART"
|
||||
and value["meta"]["chartId"] == removed_slice.id
|
||||
]
|
||||
positions.pop(removed_component[0], None)
|
||||
|
||||
data.update({"positions": positions})
|
||||
old_to_new_sliceids = {slc.id: slc.id + 10 for slc in dash.slices}
|
||||
views.Superset._set_dash_metadata(dash, data, old_to_new_sliceids)
|
||||
updated_metadata = json.loads(dash.json_metadata)
|
||||
updated_filter_scopes = {
|
||||
str(filter_slice.id + 10): {
|
||||
"region": {
|
||||
"scope": ["ROOT_ID"],
|
||||
"immune": [slc.id + 10 for slc in immune_slices],
|
||||
}
|
||||
}
|
||||
}
|
||||
self.assertEqual(updated_metadata["filter_scopes"], updated_filter_scopes)
|
||||
|
||||
# reset dash to original data
|
||||
views.Superset._set_dash_metadata(dash, original_data)
|
||||
|
||||
def test_add_slices(self, username="admin"):
|
||||
self.login(username=username)
|
||||
dash = db.session.query(Dashboard).filter_by(slug="births").first()
|
||||
|
||||
Reference in New Issue
Block a user