You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2018/05/22 23:42:04 UTC

[GitHub] williaster closed pull request #5009: Fix dashboard builder server-side unit tests

williaster closed pull request #5009: Fix dashboard builder server-side unit tests
URL: https://github.com/apache/incubator-superset/pull/5009
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/models/core.py b/superset/models/core.py
index d1c94372e1..95ee6dd90a 100644
--- a/superset/models/core.py
+++ b/superset/models/core.py
@@ -343,6 +343,15 @@ def table_names(self):
 
     @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 2ff3500149..8b426f3afd 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1558,9 +1558,10 @@ def copy_dash(self, dashboard_id):
 
             # 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 @@ def save_dash(self, dashboard_id):
                 .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 @@ def _set_dash_metadata(dashboard, data):
         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 @@ def _set_dash_metadata(dashboard, data):
         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 3c8ed76e24..6b6d656cff 100644
--- a/tests/dashboard_tests.py
+++ b/tests/dashboard_tests.py
@@ -48,7 +48,12 @@ def test_dashboard_modes(self):
             .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 @@ def test_save_dash(self, username='admin'):
         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 @@ def test_save_dash_with_filter(self, username='admin'):
         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 @@ def test_save_dash_with_dashboard_title(self, username='admin'):
             .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 @@ def test_copy_dash(self, username='admin'):
         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 @@ def test_add_slices(self, username='admin'):
             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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org