2021-01-11 08:57:55 -05:00
|
|
|
# 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.
|
2022-01-18 16:21:04 -05:00
|
|
|
from typing import Callable, List, Optional
|
2021-01-11 08:57:55 -05:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-06-21 07:22:39 -04:00
|
|
|
from superset import db
|
2021-01-11 08:57:55 -05:00
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
|
|
|
from superset.models.core import Database
|
|
|
|
from superset.models.dashboard import Dashboard
|
|
|
|
from superset.models.slice import Slice
|
2022-01-16 01:32:50 -05:00
|
|
|
from superset.utils.core import get_example_default_schema
|
|
|
|
from superset.utils.database import get_example_database
|
2022-01-18 16:21:04 -05:00
|
|
|
from tests.example_data.data_loading.base_data_loader import DataLoader
|
|
|
|
from tests.example_data.data_loading.data_definitions.types import Table
|
2021-12-16 19:11:47 -05:00
|
|
|
from tests.integration_tests.dashboard_utils import create_table_metadata
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.test_app import app
|
2021-01-11 08:57:55 -05:00
|
|
|
|
2021-12-16 19:11:47 -05:00
|
|
|
BIRTH_NAMES_TBL_NAME = "birth_names"
|
2021-01-11 08:57:55 -05:00
|
|
|
|
|
|
|
|
2021-12-16 19:11:47 -05:00
|
|
|
@pytest.fixture(scope="session")
|
2022-01-18 16:21:04 -05:00
|
|
|
def load_birth_names_data(
|
|
|
|
birth_names_table_factory: Callable[[], Table], data_loader: DataLoader
|
|
|
|
):
|
|
|
|
birth_names_table: Table = birth_names_table_factory()
|
|
|
|
data_loader.load_table(birth_names_table)
|
2021-12-16 19:11:47 -05:00
|
|
|
yield
|
2022-01-18 16:21:04 -05:00
|
|
|
data_loader.remove_table(birth_names_table.table_name)
|
2021-12-16 19:11:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def load_birth_names_dashboard_with_slices(load_birth_names_data):
|
|
|
|
with app.app_context():
|
|
|
|
dash_id_to_delete, slices_ids_to_delete = _create_dashboards()
|
|
|
|
yield
|
|
|
|
_cleanup(dash_id_to_delete, slices_ids_to_delete)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def load_birth_names_dashboard_with_slices_module_scope(load_birth_names_data):
|
|
|
|
with app.app_context():
|
|
|
|
dash_id_to_delete, slices_ids_to_delete = _create_dashboards()
|
|
|
|
yield
|
|
|
|
_cleanup(dash_id_to_delete, slices_ids_to_delete)
|
|
|
|
|
|
|
|
|
|
|
|
def _create_dashboards():
|
|
|
|
table = _create_table(
|
|
|
|
table_name=BIRTH_NAMES_TBL_NAME,
|
|
|
|
database=get_example_database(),
|
|
|
|
fetch_values_predicate="123 = 123",
|
|
|
|
)
|
2021-01-11 08:57:55 -05:00
|
|
|
|
2021-12-16 19:11:47 -05:00
|
|
|
from superset.examples.birth_names import create_dashboard, create_slices
|
2021-01-11 08:57:55 -05:00
|
|
|
|
2022-12-21 02:28:41 -05:00
|
|
|
slices, _ = create_slices(table)
|
2021-12-16 19:11:47 -05:00
|
|
|
dash = create_dashboard(slices)
|
|
|
|
slices_ids_to_delete = [slice.id for slice in slices]
|
|
|
|
dash_id_to_delete = dash.id
|
|
|
|
return dash_id_to_delete, slices_ids_to_delete
|
2021-01-11 08:57:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _create_table(
|
2022-03-29 13:03:09 -04:00
|
|
|
table_name: str,
|
|
|
|
database: "Database",
|
|
|
|
fetch_values_predicate: Optional[str] = None,
|
2021-01-11 08:57:55 -05:00
|
|
|
):
|
2021-12-16 19:11:47 -05:00
|
|
|
table = create_table_metadata(
|
2021-03-09 10:27:46 -05:00
|
|
|
table_name=table_name,
|
|
|
|
database=database,
|
|
|
|
fetch_values_predicate=fetch_values_predicate,
|
|
|
|
)
|
2021-01-11 08:57:55 -05:00
|
|
|
from superset.examples.birth_names import _add_table_metrics, _set_table_metadata
|
|
|
|
|
|
|
|
_set_table_metadata(table, database)
|
|
|
|
_add_table_metrics(table)
|
|
|
|
db.session.commit()
|
|
|
|
return table
|
|
|
|
|
|
|
|
|
2022-07-14 21:10:31 -04:00
|
|
|
def _cleanup(dash_id: int, slice_ids: List[int]) -> None:
|
2021-11-04 14:09:08 -04:00
|
|
|
schema = get_example_default_schema()
|
2022-07-14 21:10:31 -04:00
|
|
|
for datasource in db.session.query(SqlaTable).filter_by(
|
|
|
|
table_name="birth_names", schema=schema
|
|
|
|
):
|
|
|
|
for col in datasource.columns + datasource.metrics:
|
|
|
|
db.session.delete(col)
|
|
|
|
|
|
|
|
for dash in db.session.query(Dashboard).filter_by(id=dash_id):
|
|
|
|
db.session.delete(dash)
|
|
|
|
for slc in db.session.query(Slice).filter(Slice.id.in_(slice_ids)):
|
|
|
|
db.session.delete(slc)
|
2021-01-11 08:57:55 -05:00
|
|
|
db.session.commit()
|