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 2020/09/03 13:30:03 UTC

[GitHub] [incubator-superset] villebro commented on a change in pull request #10777: feat: CRUD REST API for saved queries

villebro commented on a change in pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777#discussion_r482947161



##########
File path: tests/queries/saved_queries/api_tests.py
##########
@@ -0,0 +1,379 @@
+# 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.
+# isort:skip_file
+"""Unit tests for Superset"""
+import json
+from typing import Optional
+
+import prison
+from sqlalchemy.sql import func, asc
+
+import tests.test_app
+from superset import db, security_manager
+from superset.models.core import Database
+from superset.models.sql_lab import SavedQuery
+from superset.utils.core import get_example_database
+
+from tests.base_tests import SupersetTestCase
+
+
+class TestSavedQueryApi(SupersetTestCase):

Review comment:
       It would be great if new tests would follow the pytest pattern: https://github.com/apache/incubator-superset/blob/master/tests/csv_upload_tests.py

##########
File path: tests/queries/saved_queries/api_tests.py
##########
@@ -0,0 +1,385 @@
+# 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.
+# isort:skip_file
+"""Unit tests for Superset"""
+import json
+from typing import Optional
+
+import prison
+from sqlalchemy.sql import func, asc
+
+import tests.test_app
+from superset import db, security_manager
+from superset.models.core import Database
+from superset.models.sql_lab import SavedQuery
+from superset.utils.core import get_example_database
+
+from tests.base_tests import SupersetTestCase
+
+
+class TestSavedQueryApi(SupersetTestCase):
+    def insert_saved_query(
+        self,
+        label: str,
+        sql: str,
+        db_id: Optional[int] = None,
+        user_id: Optional[int] = None,
+        schema: Optional[str] = "",
+    ) -> SavedQuery:
+        database = None
+        user = None
+        if db_id:
+            database = db.session.query(Database).get(db_id)
+        if user_id:
+            user = db.session.query(security_manager.user_model).get(user_id)
+        query = SavedQuery(
+            database=database, user=user, sql=sql, label=label, schema=schema
+        )
+        db.session.add(query)
+        db.session.commit()
+        return query
+
+    def insert_default_saved_query(
+        self, label: str = "saved1", schema: str = "schema1",
+    ) -> SavedQuery:
+        admin = self.get_user("admin")
+        example_db = get_example_database()
+        return self.insert_saved_query(
+            label,
+            "SELECT col1, col2 from table1",
+            db_id=example_db.id,
+            user_id=admin.id,
+            schema=schema,
+        )
+
+    def test_get_list_saved_query(self):
+        """
+        Saved Query API: Test get list saved query
+        """
+        query = self.insert_default_saved_query()
+        queries = db.session.query(SavedQuery).all()
+
+        self.login(username="admin")
+        uri = f"api/v1/saved_query/"
+        rv = self.get_assert_metric(uri, "get_list")
+        self.assertEqual(rv.status_code, 200)
+        data = json.loads(rv.data.decode("utf-8"))
+        self.assertEqual(data["count"], len(queries))
+        expected_columns = [
+            "user_id",
+            "db_id",
+            "schema",
+            "label",
+            "description",
+            "sql",
+            "user",
+            "database",
+        ]
+        for expected_column in expected_columns:
+            self.assertIn(expected_column, data["result"][0])
+        # rollback changes
+        db.session.delete(query)
+        db.session.commit()

Review comment:
       At the risk of abstracting too much, It might be slightly DRYer to have helpers for this too: `delete_query(query: SavedQuery)` and `delete_queries(queries: Sequence[SavedQuery])`




----------------------------------------------------------------
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:
users@infra.apache.org



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