superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccwilli...@apache.org
Subject [incubator-superset] 10/26: fix dashboard server-side unit tests (#5009)
Date Fri, 22 Jun 2018 00:54:25 GMT
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

commit 82ab75e39dbd290ea1195007833937fd3fa26786
Author: Grace Guo <grace.guo@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 b450be0..df0aafc 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 1e0d78f..b14050a 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1563,9 +1563,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
@@ -1591,7 +1592,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()
@@ -1605,9 +1605,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')
@@ -1639,7 +1640,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 60c749b..e9cd85b 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&#34;: true', resp)
         self.assertIn('standalone_mode&#34;: 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


Mime
View raw message