You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by cc...@apache.org on 2018/05/22 23:42:06 UTC
[incubator-superset] branch dashboard-builder updated: fix
dashboard server-side unit tests (#5009)
This is an automated email from the ASF dual-hosted git repository.
ccwilliams pushed a commit to branch dashboard-builder
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
The following commit(s) were added to refs/heads/dashboard-builder by this push:
new 9c08dbf fix dashboard server-side unit tests (#5009)
9c08dbf is described below
commit 9c08dbf8c976a15fbc7b0eb1be36b615144b197e
Author: Grace Guo <gr...@airbnb.com>
AuthorDate: Tue May 22 16:42:02 2018 -0700
fix dashboard server-side unit tests (#5009)
---
superset/models/core.py | 9 ++++
superset/views/core.py | 17 ++++---
tests/dashboard_tests.py | 123 ++++++++++++++++++++++++++++++++++++-----------
3 files changed, 112 insertions(+), 37 deletions(-)
diff --git a/superset/models/core.py b/superset/models/core.py
index d1c9437..95ee6dd 100644
--- a/superset/models/core.py
+++ b/superset/models/core.py
@@ -343,6 +343,15 @@ class Dashboard(Model, AuditMixinNullable, ImportMixin):
@property
def url(self):
+ if self.json_metadata:
+ # add default_filters to the preselect_filters of dashboard
+ json_metadata = json.loads(self.json_metadata)
+ default_filters = json_metadata.get('default_filters')
+ # make sure default_filters is not empty
+ if json.loads(default_filters):
+ filters = parse.quote(default_filters.encode('utf8'))
+ return '/superset/dashboard/{}/?preselect_filters={}'.format(
+ self.slug or self.id, filters)
return '/superset/dashboard/{}/'.format(self.slug or self.id)
@property
diff --git a/superset/views/core.py b/superset/views/core.py
index 2ff3500..8b426f3 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1558,9 +1558,10 @@ class Superset(BaseSupersetView):
# update chartId of layout entities
for value in data['positions'].values():
- if isinstance(value, dict) and value.get('meta') \
- and value.get('meta').get('chartId'):
-
+ if (
+ isinstance(value, dict) and value.get('meta') and
+ value.get('meta').get('chartId')
+ ):
old_id = value.get('meta').get('chartId')
new_id = old_to_new_sliceids[old_id]
value['meta']['chartId'] = new_id
@@ -1586,7 +1587,6 @@ class Superset(BaseSupersetView):
.filter_by(id=dashboard_id).first())
check_ownership(dash, raise_if_false=True)
data = json.loads(request.form.get('data'))
- original_slice_names = {(slc.id): slc.slice_name for slc in dash.slices}
self._set_dash_metadata(dash, data)
session.merge(dash)
session.commit()
@@ -1600,9 +1600,10 @@ class Superset(BaseSupersetView):
slice_ids = []
slice_id_to_name = {}
for value in positions.values():
- if isinstance(value, dict) and value.get('meta') \
- and value.get('meta').get('chartId'):
-
+ if (
+ isinstance(value, dict) and value.get('meta') and
+ value.get('meta').get('chartId')
+ ):
slice_id = value.get('meta').get('chartId')
slice_ids.append(slice_id)
slice_id_to_name[slice_id] = value.get('meta').get('chartName')
@@ -1634,7 +1635,7 @@ class Superset(BaseSupersetView):
if 'filter_immune_slice_fields' not in md:
md['filter_immune_slice_fields'] = {}
md['expanded_slices'] = data['expanded_slices']
- default_filters_data = json.loads(data.get('default_filters', ''))
+ default_filters_data = json.loads(data.get('default_filters', '{}'))
for key in default_filters_data.keys():
if int(key) not in slice_ids:
del default_filters_data[key]
diff --git a/tests/dashboard_tests.py b/tests/dashboard_tests.py
index 3c8ed76..6b6d656 100644
--- a/tests/dashboard_tests.py
+++ b/tests/dashboard_tests.py
@@ -48,7 +48,12 @@ class DashboardTests(SupersetTestCase):
.filter_by(slug='births')
.first()
)
- resp = self.get_resp(dash.url + '?edit=true&standalone=true')
+ url = dash.url
+ if dash.url.find('?') == -1:
+ url += '?'
+ else:
+ url += '&'
+ resp = self.get_resp(url + 'edit=true&standalone=true')
self.assertIn('editMode": true', resp)
self.assertIn('standalone_mode": true', resp)
@@ -56,15 +61,20 @@ class DashboardTests(SupersetTestCase):
self.login(username=username)
dash = db.session.query(models.Dashboard).filter_by(
slug='births').first()
- positions = []
+ positions = {}
for i, slc in enumerate(dash.slices):
+ id = 'DASHBOARD_CHART_TYPE-{}'.format(i)
d = {
- 'col': 0,
- 'row': i * 4,
- 'size_x': 4,
- 'size_y': 4,
- 'slice_id': '{}'.format(slc.id)}
- positions.append(d)
+ 'type': 'DASHBOARD_CHART_TYPE',
+ 'id': id,
+ 'children': [],
+ 'meta': {
+ 'width': 4,
+ 'height': 50,
+ 'chartId': slc.id,
+ },
+ }
+ positions[id] = d
data = {
'css': '',
'expanded_slices': {},
@@ -79,15 +89,20 @@ class DashboardTests(SupersetTestCase):
self.login(username=username)
dash = db.session.query(models.Dashboard).filter_by(
slug='world_health').first()
- positions = []
+ positions = {}
for i, slc in enumerate(dash.slices):
+ id = 'DASHBOARD_CHART_TYPE-{}'.format(i)
d = {
- 'col': 0,
- 'row': i * 4,
- 'size_x': 4,
- 'size_y': 4,
- 'slice_id': '{}'.format(slc.id)}
- positions.append(d)
+ 'type': 'DASHBOARD_CHART_TYPE',
+ 'id': id,
+ 'children': [],
+ 'meta': {
+ 'width': 4,
+ 'height': 50,
+ 'chartId': slc.id,
+ },
+ }
+ positions[id] = d
filters = {str(dash.slices[0].id): {'region': ['North America']}}
default_filters = json.dumps(filters)
@@ -119,15 +134,20 @@ class DashboardTests(SupersetTestCase):
.first()
)
origin_title = dash.dashboard_title
- positions = []
+ positions = {}
for i, slc in enumerate(dash.slices):
+ id = 'DASHBOARD_CHART_TYPE-{}'.format(i)
d = {
- 'col': 0,
- 'row': i * 4,
- 'size_x': 4,
- 'size_y': 4,
- 'slice_id': '{}'.format(slc.id)}
- positions.append(d)
+ 'type': 'DASHBOARD_CHART_TYPE',
+ 'id': id,
+ 'children': [],
+ 'meta': {
+ 'width': 4,
+ 'height': 50,
+ 'chartId': slc.id,
+ },
+ }
+ positions[id] = d
data = {
'css': '',
'expanded_slices': {},
@@ -150,15 +170,20 @@ class DashboardTests(SupersetTestCase):
self.login(username=username)
dash = db.session.query(models.Dashboard).filter_by(
slug='births').first()
- positions = []
+ positions = {}
for i, slc in enumerate(dash.slices):
+ id = 'DASHBOARD_CHART_TYPE-{}'.format(i)
d = {
- 'col': 0,
- 'row': i * 4,
- 'size_x': 4,
- 'size_y': 4,
- 'slice_id': '{}'.format(slc.id)}
- positions.append(d)
+ 'type': 'DASHBOARD_CHART_TYPE',
+ 'id': id,
+ 'children': [],
+ 'meta': {
+ 'width': 4,
+ 'height': 50,
+ 'chartId': slc.id,
+ },
+ }
+ positions[id] = d
data = {
'css': '',
'duplicate_slices': False,
@@ -213,6 +238,46 @@ class DashboardTests(SupersetTestCase):
o for o in dash.slices if o.slice_name != 'Mapbox Long/Lat']
db.session.commit()
+ def test_remove_slices(self, username='admin'):
+ self.login(username=username)
+ dash = db.session.query(models.Dashboard).filter_by(
+ slug='births').first()
+ positions = {}
+ origin_slices_length = len(dash.slices)
+ for i, slc in enumerate(dash.slices):
+ id = 'DASHBOARD_CHART_TYPE-{}'.format(i)
+ d = {
+ 'type': 'DASHBOARD_CHART_TYPE',
+ 'id': id,
+ 'children': [],
+ 'meta': {
+ 'width': 4,
+ 'height': 50,
+ 'chartId': slc.id,
+ },
+ }
+ # remove last slice
+ if i < len(dash.slices) - 1:
+ positions[id] = d
+
+ data = {
+ 'css': '',
+ 'expanded_slices': {},
+ 'positions': positions,
+ 'dashboard_title': dash.dashboard_title,
+ }
+
+ # save dash
+ dash_id = dash.id
+ url = '/superset/save_dash/{}/'.format(dash_id)
+ self.client.post(url, data=dict(data=json.dumps(data)))
+ dash = db.session.query(models.Dashboard).filter_by(
+ id=dash_id).first()
+
+ # verify slices data
+ data = dash.data
+ self.assertEqual(len(data['slices']), origin_slices_length - 1)
+
def test_public_user_dashboard_access(self):
table = (
db.session
--
To stop receiving notification emails like this one, please contact
ccwilliams@apache.org.