mirror of
https://github.com/apache/superset.git
synced 2024-09-12 00:29:39 -04:00
added unit tests
This commit is contained in:
parent
cf68455cba
commit
a8416b00b6
@ -20,13 +20,13 @@ from typing import Any, Dict, Set
|
||||
from marshmallow import Schema
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from supereset.queries.saved_queries.commands.exceptions import SavedQueryImportError
|
||||
from superset.queries.saved_queries.commands.exceptions import SavedQueryImportError
|
||||
from superset.commands.importers.v1 import ImportModelsCommand
|
||||
from superset.connectors.sqla.models import SqlaTable
|
||||
from superset.databases.commands.importers.v1.utils import import_database
|
||||
from superset.datasets.commands.importers.v1.utils import import_dataset
|
||||
from superset.dataset.schemas import ImportV1DatasetSchema
|
||||
from superset.queries.dao import SavedQueryDAO
|
||||
from superset.datasets.schemas import ImportV1DatasetSchema
|
||||
from superset.queries.saved_queries.dao import SavedQueryDAO
|
||||
from superset.queries.saved_queries.commands.importers.v1.utils import import_saved_query
|
||||
from superset.queries.saved_queries.schemas import ImportV1SavedQuerySchema
|
||||
|
@ -197,7 +197,7 @@ class TestImportChartsCommand(SupersetTestCase):
|
||||
db.session.commit()
|
||||
|
||||
def test_import_v1_chart_multiple(self):
|
||||
"""Test that a dataset can be imported multiple times"""
|
||||
"""Test that a chart can be imported multiple times"""
|
||||
contents = {
|
||||
"metadata.yaml": yaml.safe_dump(chart_metadata_config),
|
||||
"databases/imported_database.yaml": yaml.safe_dump(database_config),
|
||||
|
@ -17,17 +17,24 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from superset import db, security_manager
|
||||
from superset.queries.saved_queries.commands.importers.v1 import ImportSavedQueriesCommand
|
||||
from superset.queries.saved_queries.commands.importers.v1 import (
|
||||
ImportSavedQueriesCommand
|
||||
)
|
||||
from superset.commands.exceptions import CommandInvalidError
|
||||
from superset.commands.importers.exceptions import IncorrectVersionError
|
||||
from superset.models.sql_lab import SavedQuery
|
||||
from superset.models.core import Database
|
||||
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,
|
||||
database_metadata_config,
|
||||
saved_queries_config,
|
||||
saved_queries_metadata_config,
|
||||
)
|
||||
@ -125,8 +132,97 @@ class TestImportSavedQueriesCommand(SupersetTestCase):
|
||||
command = ImportSavedQueriesCommand(contents)
|
||||
command.run()
|
||||
|
||||
saved_query = db.session.query(Slice).filter_by(
|
||||
saved_query = db.session.query(SavedQuery).filter_by(
|
||||
uuid=saved_queries_config["uuid"]
|
||||
).one()
|
||||
database = saved_query.datasource
|
||||
assert
|
||||
assert saved_query.schema == "public"
|
||||
assert saved_query.sql == (
|
||||
"""
|
||||
-- Note: Unless you save your query,
|
||||
these tabs will NOT persist if you clear
|
||||
your cookies or change browsers.
|
||||
|
||||
SELECT * from birth_names
|
||||
"""
|
||||
)
|
||||
assert saved_query.uuid == "05b679b5-8eaf-452c-b874-a7a774cfa4e9"
|
||||
assert saved_query.database_uuid == "b8a1ccd3-779d-4ab7-8ad8-9ab119d7fe89"
|
||||
|
||||
database = (
|
||||
db.session.query(Database).filter_by(uuid=database_config["uuid"]).one()
|
||||
)
|
||||
|
||||
db.session.delete(saved_query)
|
||||
db.session.delete(database)
|
||||
db.session.commit()
|
||||
def test_import_v1_saved_queries_multiple(self):
|
||||
"""Test that a saved query can be imported multiple times"""
|
||||
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, overwrite=True)
|
||||
command.run()
|
||||
command.run()
|
||||
database = (
|
||||
db.session.query(SavedQuery).filter_by(uuid=database_config["uuid"]).one()
|
||||
)
|
||||
saved_query = db.session.query(SavedQuery).filter_by(datasource_id=database.id).all()
|
||||
assert len(saved_query) == 1
|
||||
|
||||
db.session.delete(saved_query[0])
|
||||
db.session.delete(database)
|
||||
db.session.commit()
|
||||
|
||||
def test_import_v1_saved_queries_validation(self):
|
||||
"""Test different validations applied when importing a chart"""
|
||||
# metadata.yaml must be present
|
||||
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)
|
||||
with pytest.raises(IncorrectVersionError) as excinfo:
|
||||
command.run()
|
||||
assert str(excinfo.value) == "Missing metadata.yaml"
|
||||
|
||||
#version should be 1.0.0
|
||||
contents["metadata.yaml"] = yaml.safe_dump(
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"type": "SavedQuery",
|
||||
"timestamp": "2021-03-30T20:37:54.791187+00:00",
|
||||
}
|
||||
)
|
||||
command = ImportSavedQueriesCommand(contents)
|
||||
with pytest.raises(IncorrectVersionError) as excinfo:
|
||||
command.run()
|
||||
assert str(excinfo.value) == "Must be equal to 1.0.0"
|
||||
|
||||
#type should be a SavedQuery
|
||||
contents["metadata.yaml"] = yaml.safe_dump(database_metadata_config)
|
||||
command = ImportSavedQueriesCommand(contents)
|
||||
with pytest.raises(CommandInvalidError) as excinfo:
|
||||
command.run()
|
||||
assert str(excinfo.value) == "Error importing saved query."
|
||||
assert excinfo.value.normalized_messages() == {
|
||||
"metadata.yaml": {"type": ["Must be equal to SavedQuery."]}
|
||||
}
|
||||
|
||||
# must also validate databases
|
||||
broken_config = database_config.copy()
|
||||
del broken_config["database_name"]
|
||||
contents["metadata.yaml"] = yaml.safe_dump(saved_queries_metadata_config)
|
||||
contents["databases/imported_database.yaml"] = yaml.safe_dump(broken_config)
|
||||
command = ImportSavedQueriesCommand(contents)
|
||||
with pytest.raises(CommandInvalidError) as excinfo:
|
||||
command.run()
|
||||
assert str(excinfo.value) = "Error importing saved query."
|
||||
assert excinfo.value.normalized_messages() == {
|
||||
"databases/imported_database.yaml": {
|
||||
"database_name": ["Missing data for required field."],
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user