mirror of
https://github.com/apache/superset.git
synced 2024-09-12 00:29:39 -04:00
started tests
This commit is contained in:
parent
e36fecdc0b
commit
cf68455cba
@ -23,7 +23,7 @@ from marshmallow.exceptions import ValidationError
|
||||
from superset.commands.base import BaseCommand
|
||||
from superset.commands.exceptions import CommandInvalidError
|
||||
from superset.commands.importers.exceptions import IncorrectVersionError
|
||||
from superset.queries.saved_queries.importers import v1
|
||||
from superset.queries.saved_queries.commands.importers import v1
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
15
tests/fixtures/importexport.py
vendored
15
tests/fixtures/importexport.py
vendored
@ -343,7 +343,11 @@ dashboard_metadata_config: Dict[str, Any] = {
|
||||
"type": "Dashboard",
|
||||
"timestamp": "2020-11-04T21:27:44.423819+00:00",
|
||||
}
|
||||
|
||||
saved_queries_metadata_config: Dict[str, Any] = {
|
||||
"version": "1.0.0",
|
||||
"type": "SavedQuery",
|
||||
"timestamp": "2021-03-30T20:37:54.791187+00:00"
|
||||
}
|
||||
database_config: Dict[str, Any] = {
|
||||
"allow_csv_upload": True,
|
||||
"allow_ctas": True,
|
||||
@ -499,3 +503,12 @@ dashboard_config = {
|
||||
},
|
||||
"version": "1.0.0",
|
||||
}
|
||||
saved_queries_config = {
|
||||
"schema": "public",
|
||||
"label": "Test Saved Query",
|
||||
"description": None,
|
||||
"sql": "-- Note: Unless you save your query, these tabs will NOT persist if you clear\nyour cookies or change browsers.\n\n\nSELECT * from birth_names",
|
||||
"uuid": "05b679b5-8eaf-452c-b874-a7a774cfa4e9",
|
||||
"version": "1.0.0",
|
||||
"database_uuid": "b8a1ccd3-779d-4ab7-8ad8-9ab119d7fe89"
|
||||
}
|
||||
|
@ -19,8 +19,9 @@
|
||||
import json
|
||||
from io import BytesIO
|
||||
from typing import Optional
|
||||
from zipfile import is_zipfile
|
||||
from zipfile import is_zipfile, ZipFile
|
||||
|
||||
import yaml
|
||||
import pytest
|
||||
import prison
|
||||
from sqlalchemy.sql import func, and_
|
||||
@ -33,6 +34,11 @@ from superset.models.sql_lab import SavedQuery
|
||||
from superset.utils.core import get_example_database
|
||||
|
||||
from tests.base_tests import SupersetTestCase
|
||||
from tests.fixtures.importexport import (
|
||||
database_config,
|
||||
saved_queries_config,
|
||||
saved_queries_metadata_config,
|
||||
)
|
||||
|
||||
|
||||
SAVED_QUERIES_FIXTURE_COUNT = 10
|
||||
@ -745,3 +751,48 @@ class TestSavedQueryApi(SupersetTestCase):
|
||||
uri = f"api/v1/saved_query/export/?q={prison.dumps(argument)}"
|
||||
rv = self.client.get(uri)
|
||||
assert rv.status_code == 404
|
||||
|
||||
def create_saved_query_import(self):
|
||||
buf = BytesIO()
|
||||
with ZipFile(buf, "w") as bundle:
|
||||
with bundle.open("saved_query_export/metadata.yaml", "w") as fp:
|
||||
fp.write(yaml.safe_dump(saved_queries_metadata_config).encode())
|
||||
with bundle.open(
|
||||
"saved_query_export/databases/imported_database.yaml", "w"
|
||||
) as fp:
|
||||
fp.write(yaml.safe_dump(database_config).encode())
|
||||
with bundle.open("saved_query_export/queries/imported_database/public/imported_saved_query.yaml", "w") as fp:
|
||||
fp.write(yaml.safe_dump(saved_queries_config).encode())
|
||||
buf.seek(0)
|
||||
return buf
|
||||
|
||||
@pytest.mark.usefixtures("create_saved_queries")
|
||||
def test_import_saved_queries(self):
|
||||
"""
|
||||
Saved Query API: Test import
|
||||
"""
|
||||
self.login(username="admin")
|
||||
uri = "api/v1/saved_query/import/"
|
||||
|
||||
buf = self.create_saved_query_import()
|
||||
form_data = {
|
||||
"formData": (buf, "saved_query.zip"),
|
||||
}
|
||||
rv = self.client.post(uri, data=form_data, content_type="multipart/form-data")
|
||||
response = json.loads(rv.data.decode("utf-8"))
|
||||
|
||||
assert rv.status_code == 200
|
||||
assert response == {"message": "OK"}
|
||||
database = (
|
||||
db.session.query(Database).filter_by(uuid=database_config["uuid"]).one()
|
||||
)
|
||||
assert database.database_name == "imported_database"
|
||||
|
||||
assert len(database.tables) == 1
|
||||
|
||||
saved_query = (
|
||||
db.session
|
||||
.query(SavedQuery)
|
||||
.filter_by(uuid=saved_queries_config["uuid"]).one()
|
||||
)
|
||||
assert saved_query.database == database
|
||||
|
@ -20,12 +20,17 @@ from unittest.mock import patch
|
||||
import yaml
|
||||
|
||||
from superset import db, security_manager
|
||||
from superset.queries.saved_queries.commands.importers.v1 import ImportSavedQueriesCommand
|
||||
from superset.models.sql_lab import SavedQuery
|
||||
from superset.queries.saved_queries.commands.exceptions import SavedQueryNotFoundError
|
||||
from superset.queries.saved_queries.commands.export import ExportSavedQueriesCommand
|
||||
from superset.utils.core import get_example_database
|
||||
from tests.base_tests import SupersetTestCase
|
||||
|
||||
from tests.fixtures.importexport import (
|
||||
database_config,
|
||||
saved_queries_config,
|
||||
saved_queries_metadata_config,
|
||||
)
|
||||
|
||||
class TestExportSavedQueriesCommand(SupersetTestCase):
|
||||
def setUp(self):
|
||||
@ -108,3 +113,20 @@ class TestExportSavedQueriesCommand(SupersetTestCase):
|
||||
"version",
|
||||
"database_uuid",
|
||||
]
|
||||
class TestImportSavedQueriesCommand(SupersetTestCase):
|
||||
def test_import_v1_saved_queries(self):
|
||||
"""Test that we can import a saved query"""
|
||||
contents = {
|
||||
"metadata.yaml": yaml.safe_dump(saved_queries_metadata_config),
|
||||
"databases/imported_database.yaml": yaml.safe_dump(database_config),
|
||||
"queries/imported_query.yaml": yaml.safe_dump(saved_queries_config)
|
||||
}
|
||||
|
||||
command = ImportSavedQueriesCommand(contents)
|
||||
command.run()
|
||||
|
||||
saved_query = db.session.query(Slice).filter_by(
|
||||
uuid=saved_queries_config["uuid"]
|
||||
).one()
|
||||
database = saved_query.datasource
|
||||
assert
|
Loading…
Reference in New Issue
Block a user