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 2022/03/21 11:06:03 UTC
[GitHub] [superset] michael-s-molina commented on a change in pull request #19220: feat: API for asset sync
michael-s-molina commented on a change in pull request #19220:
URL: https://github.com/apache/superset/pull/19220#discussion_r830982123
##########
File path: tests/unit_tests/views/importexport/api_test.py
##########
@@ -0,0 +1,116 @@
+# 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.
+
+import json
+from io import BytesIO
+from typing import Any
+from zipfile import is_zipfile, ZipFile
+
+from pytest_mock import MockFixture
+
+from superset import security_manager
+
+
+def test_export_assets(mocker: MockFixture, client: Any) -> None:
+ """
+ Test exporting assets.
+ """
+ # pylint: disable=import-outside-toplevel
+ from superset.commands.importers.v1.utils import get_contents_from_bundle
+
+ # grant access
+ mocker.patch(
+ "flask_appbuilder.security.decorators.verify_jwt_in_request", return_value=True
+ )
+ mocker.patch.object(security_manager, "has_access", return_value=True)
+
+ # pylint: disable=invalid-name
+ ExportAssetsCommand = mocker.patch(
+ "superset.views.importexport.api.ExportAssetsCommand"
+ )
+ ExportAssetsCommand().run.return_value = [
+ (
+ "metadata.yaml",
+ "version: 1.0.0\ntype: assets\ntimestamp: '2022-01-01T00:00:00+00:00'\n",
+ ),
+ ("databases/example.yaml", "<DATABASE CONTENTS>"),
+ ]
+
+ response = client.get("/api/v1/assets/export/")
+ assert response.status_code == 200
+
+ buf = BytesIO(response.data)
+ assert is_zipfile(buf)
+
+ buf.seek(0)
+ with ZipFile(buf) as bundle:
+ contents = get_contents_from_bundle(bundle)
+ assert contents == {
+ "metadata.yaml": (
Review comment:
Maybe store the contents into a variable to be used when mocking the command's return value and also when comparing the output?
##########
File path: tests/unit_tests/conftest.py
##########
@@ -40,32 +41,44 @@ def session() -> Iterator[Session]:
# flask calls session.remove()
in_memory_session.remove = lambda: None
+ # patch session
+ mocker.patch(
+ "superset.security.SupersetSecurityManager.get_session",
+ return_value=in_memory_session,
+ )
+ mocker.patch("superset.db.session", in_memory_session)
+
yield in_memory_session
-@pytest.fixture
-def app(mocker: MockFixture, session: Session) -> Iterator[SupersetApp]:
+@pytest.fixture(scope="module")
+def app() -> Iterator[SupersetApp]:
"""
A fixture that generates a Superset app.
"""
app = SupersetApp(__name__)
app.config.from_object("superset.config")
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
- app.config["FAB_ADD_SECURITY_VIEWS"] = False
+ app.config["TESTING"] = True
- app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
- app_initializer.init_app()
+ # ``superset.extensions.appbuilder`` is a singleton, and won't rebuild the
+ # routes when this fixture is called multiple times; we need to clear the
+ # registered views to ensure the initialization can happen more than once.
+ appbuilder.baseviews = []
- # patch session
- mocker.patch(
- "superset.security.SupersetSecurityManager.get_session", return_value=session,
- )
- mocker.patch("superset.db.session", session)
+ app_initializer = SupersetAppInitializer(app)
Review comment:
I noticed that previously the app initializer class could be configured and now it can't. Is this intended? Was it not being used?
##########
File path: tests/unit_tests/views/importexport/api_test.py
##########
@@ -0,0 +1,116 @@
+# 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.
+
+import json
+from io import BytesIO
+from typing import Any
+from zipfile import is_zipfile, ZipFile
+
+from pytest_mock import MockFixture
+
+from superset import security_manager
+
+
+def test_export_assets(mocker: MockFixture, client: Any) -> None:
+ """
+ Test exporting assets.
+ """
+ # pylint: disable=import-outside-toplevel
+ from superset.commands.importers.v1.utils import get_contents_from_bundle
+
+ # grant access
+ mocker.patch(
+ "flask_appbuilder.security.decorators.verify_jwt_in_request", return_value=True
+ )
+ mocker.patch.object(security_manager, "has_access", return_value=True)
+
+ # pylint: disable=invalid-name
+ ExportAssetsCommand = mocker.patch(
+ "superset.views.importexport.api.ExportAssetsCommand"
+ )
+ ExportAssetsCommand().run.return_value = [
+ (
+ "metadata.yaml",
+ "version: 1.0.0\ntype: assets\ntimestamp: '2022-01-01T00:00:00+00:00'\n",
+ ),
+ ("databases/example.yaml", "<DATABASE CONTENTS>"),
+ ]
+
+ response = client.get("/api/v1/assets/export/")
+ assert response.status_code == 200
+
+ buf = BytesIO(response.data)
+ assert is_zipfile(buf)
+
+ buf.seek(0)
+ with ZipFile(buf) as bundle:
+ contents = get_contents_from_bundle(bundle)
+ assert contents == {
+ "metadata.yaml": (
+ "version: 1.0.0\ntype: assets\ntimestamp: '2022-01-01T00:00:00+00:00'\n"
+ ),
+ "databases/example.yaml": "<DATABASE CONTENTS>",
+ }
+
Review comment:
Can we add tests for the exception cases like sending invalid files or files with wrong passwords?
##########
File path: superset/views/importexport/api.py
##########
@@ -0,0 +1,158 @@
+# 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.
+import json
+from datetime import datetime
+from io import BytesIO
+from zipfile import ZipFile
+
+from flask import request, Response, send_file
+from flask_appbuilder.api import BaseApi, expose, protect
+
+from superset.commands.export.assets import ExportAssetsCommand
+from superset.commands.importers.exceptions import NoValidFilesFoundError
+from superset.commands.importers.v1.assets import ImportAssetsCommand
+from superset.commands.importers.v1.utils import get_contents_from_bundle
+from superset.extensions import event_logger
+from superset.views.base_api import requires_form_data
+
+
+class ImportExportRestApi(BaseApi):
Review comment:
Should we move this outside the `views` folder? I'm understanding that `views` contain legacy files that should be migrated to the new 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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
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