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 11:59:49 UTC

[GitHub] [incubator-superset] dpgaspar opened a new pull request #10777: feat: CRUD REST API for saved queries

dpgaspar opened a new pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777


   ### SUMMARY
   <!--- Describe the change below, including rationale and design decisions -->
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   <!--- Skip this if not applicable -->
   
   ### TEST PLAN
   <!--- What steps should be taken to verify the changes -->
   
   ### ADDITIONAL INFORMATION
   <!--- Check any relevant boxes with "x" -->
   <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
   - [ ] Has associated issue:
   - [ ] Changes UI
   - [ ] Requires DB Migration.
   - [ ] Confirm DB Migration upgrade and downgrade tested.
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   


----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
dpgaspar commented on a change in pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777#discussion_r483058253



##########
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:
       fixtures have DRYed it!




----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
dpgaspar commented on a change in pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777#discussion_r483058933



##########
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:
       pytest fixtures are great!




----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
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


[GitHub] [incubator-superset] willbarrett merged pull request #10777: feat: CRUD REST API for saved queries

Posted by GitBox <gi...@apache.org>.
willbarrett merged pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777


   


----------------------------------------------------------------
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


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

Posted by GitBox <gi...@apache.org>.
dpgaspar commented on a change in pull request #10777:
URL: https://github.com/apache/incubator-superset/pull/10777#discussion_r482992229



##########
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:
       Agree, very good opportunity to try the new pytest fixtures (they really look promising)




----------------------------------------------------------------
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