dpgaspar commented on a change in pull request #11349:
URL: 
https://github.com/apache/incubator-superset/pull/11349#discussion_r510136165



##########
File path: superset/charts/api.py
##########
@@ -709,3 +714,62 @@ def thumbnail(
         return Response(
             FileWrapper(screenshot), mimetype="image/png", 
direct_passthrough=True
         )
+
+    @expose("/export/", methods=["GET"])
+    @protect()
+    @safe
+    @statsd_metrics
+    @rison(get_export_ids_schema)
+    def export(self, **kwargs: Any) -> Response:
+        """Export charts
+        ---
+        get:
+          description: >-
+            Exports multiple charts and downloads them as YAML files
+          parameters:
+          - in: query
+            name: q
+            content:
+              application/json:

Review comment:
       nit: instead you can reference it like this: 
https://github.com/apache/incubator-superset/blob/master/superset/charts/api.py#L393

##########
File path: tests/charts/commands_tests.py
##########
@@ -0,0 +1,101 @@
+# 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.
+
+from unittest.mock import patch
+
+import yaml
+
+from superset import db, security_manager
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.charts.commands.export import ExportChartsCommand
+from superset.models.slice import Slice
+from tests.base_tests import SupersetTestCase
+
+
+class TestExportChartsCommand(SupersetTestCase):
+    @patch("superset.security.manager.g")
+    def test_export_chart_command(self, mock_g):
+        mock_g.user = security_manager.find_user("admin")
+
+        example_chart = db.session.query(Slice).all()[0]
+        command = ExportChartsCommand(chart_ids=[example_chart.id])
+        contents = dict(command.run())
+
+        expected = [
+            "charts/energy_sankey.yaml",
+            "datasets/examples/energy_usage.yaml",
+            "databases/examples.yaml",
+        ]
+        assert expected == list(contents.keys())
+
+        metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"])
+        assert metadata == {
+            "slice_name": "Energy Sankey",
+            "viz_type": "sankey",
+            "params": {
+                "collapsed_fieldsets": "",
+                "groupby": ["source", "target",],
+                "metric": "sum__value",
+                "row_limit": "5000",
+                "slice_name": "Energy Sankey",
+                "viz_type": "sankey",
+            },
+            "cache_timeout": None,
+            "dataset_uuid": str(example_chart.table.uuid),
+            "uuid": str(example_chart.uuid),
+            "version": "1.0.0",
+        }
+
+    @patch("superset.security.manager.g")
+    def test_export_chart_command_no_access(self, mock_g):
+        """Test that users can't export datasets they don't have access to"""
+        mock_g.user = security_manager.find_user("gamma")
+
+        example_chart = db.session.query(Slice).all()[0]

Review comment:
       nit: `example_chart = db.session.query(Slice).first()`

##########
File path: tests/charts/commands_tests.py
##########
@@ -0,0 +1,101 @@
+# 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.
+
+from unittest.mock import patch
+
+import yaml
+
+from superset import db, security_manager
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.charts.commands.export import ExportChartsCommand
+from superset.models.slice import Slice
+from tests.base_tests import SupersetTestCase
+
+
+class TestExportChartsCommand(SupersetTestCase):
+    @patch("superset.security.manager.g")
+    def test_export_chart_command(self, mock_g):
+        mock_g.user = security_manager.find_user("admin")
+
+        example_chart = db.session.query(Slice).all()[0]

Review comment:
       nit: example_chart = db.session.query(Slice).first()

##########
File path: tests/charts/commands_tests.py
##########
@@ -0,0 +1,101 @@
+# 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.
+
+from unittest.mock import patch
+
+import yaml
+
+from superset import db, security_manager
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.charts.commands.export import ExportChartsCommand
+from superset.models.slice import Slice
+from tests.base_tests import SupersetTestCase
+
+
+class TestExportChartsCommand(SupersetTestCase):
+    @patch("superset.security.manager.g")
+    def test_export_chart_command(self, mock_g):
+        mock_g.user = security_manager.find_user("admin")
+
+        example_chart = db.session.query(Slice).all()[0]
+        command = ExportChartsCommand(chart_ids=[example_chart.id])
+        contents = dict(command.run())
+
+        expected = [
+            "charts/energy_sankey.yaml",
+            "datasets/examples/energy_usage.yaml",
+            "databases/examples.yaml",
+        ]
+        assert expected == list(contents.keys())
+
+        metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"])
+        assert metadata == {
+            "slice_name": "Energy Sankey",
+            "viz_type": "sankey",
+            "params": {
+                "collapsed_fieldsets": "",
+                "groupby": ["source", "target",],
+                "metric": "sum__value",
+                "row_limit": "5000",
+                "slice_name": "Energy Sankey",
+                "viz_type": "sankey",
+            },
+            "cache_timeout": None,
+            "dataset_uuid": str(example_chart.table.uuid),
+            "uuid": str(example_chart.uuid),
+            "version": "1.0.0",
+        }
+
+    @patch("superset.security.manager.g")
+    def test_export_chart_command_no_access(self, mock_g):
+        """Test that users can't export datasets they don't have access to"""
+        mock_g.user = security_manager.find_user("gamma")
+
+        example_chart = db.session.query(Slice).all()[0]
+        command = ExportChartsCommand(chart_ids=[example_chart.id])
+        contents = command.run()
+        with self.assertRaises(ChartNotFoundError):
+            next(contents)
+
+    @patch("superset.security.manager.g")
+    def test_export_chart_command_invalid_dataset(self, mock_g):
+        """Test that an error is raised when exporting an invalid dataset"""
+        mock_g.user = security_manager.find_user("admin")
+        command = ExportChartsCommand(chart_ids=[-1])
+        contents = command.run()
+        with self.assertRaises(ChartNotFoundError):
+            next(contents)
+
+    @patch("superset.security.manager.g")
+    def test_export_chart_command_key_order(self, mock_g):
+        """Test that they keys in the YAML have the same order as 
export_fields"""
+        mock_g.user = security_manager.find_user("admin")
+
+        example_chart = db.session.query(Slice).all()[0]

Review comment:
       nit: example_chart = db.session.query(Slice).first()




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to