mirror of
https://github.com/apache/superset.git
synced 2024-09-12 08:39:45 -04:00
142 lines
5.2 KiB
Python
142 lines
5.2 KiB
Python
# 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 copy
|
|
|
|
from pytest_mock import MockFixture
|
|
from sqlalchemy.orm.session import Session
|
|
from sqlalchemy.sql import select
|
|
|
|
from tests.unit_tests.fixtures.assets_configs import (
|
|
charts_config_1,
|
|
charts_config_2,
|
|
dashboards_config_1,
|
|
dashboards_config_2,
|
|
databases_config,
|
|
datasets_config,
|
|
)
|
|
|
|
|
|
def test_import_new_assets(mocker: MockFixture, session: Session) -> None:
|
|
"""
|
|
Test that all new assets are imported correctly.
|
|
"""
|
|
from superset import security_manager
|
|
from superset.commands.importers.v1.assets import ImportAssetsCommand
|
|
from superset.models.dashboard import dashboard_slices
|
|
from superset.models.slice import Slice
|
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
engine = session.get_bind()
|
|
Slice.metadata.create_all(engine) # pylint: disable=no-member
|
|
configs = {
|
|
**copy.deepcopy(databases_config),
|
|
**copy.deepcopy(datasets_config),
|
|
**copy.deepcopy(charts_config_1),
|
|
**copy.deepcopy(dashboards_config_1),
|
|
}
|
|
expected_number_of_dashboards = len(dashboards_config_1)
|
|
expected_number_of_charts = len(charts_config_1)
|
|
|
|
ImportAssetsCommand._import(session, configs)
|
|
dashboard_ids = session.scalars(
|
|
select(dashboard_slices.c.dashboard_id).distinct()
|
|
).all()
|
|
chart_ids = session.scalars(select(dashboard_slices.c.slice_id)).all()
|
|
|
|
assert len(chart_ids) == expected_number_of_charts
|
|
assert len(dashboard_ids) == expected_number_of_dashboards
|
|
|
|
|
|
def test_import_adds_dashboard_charts(mocker: MockFixture, session: Session) -> None:
|
|
"""
|
|
Test that existing dashboards are updated with new charts.
|
|
"""
|
|
from superset import security_manager
|
|
from superset.commands.importers.v1.assets import ImportAssetsCommand
|
|
from superset.models.dashboard import dashboard_slices
|
|
from superset.models.slice import Slice
|
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
engine = session.get_bind()
|
|
Slice.metadata.create_all(engine) # pylint: disable=no-member
|
|
base_configs = {
|
|
**copy.deepcopy(databases_config),
|
|
**copy.deepcopy(datasets_config),
|
|
**copy.deepcopy(charts_config_2),
|
|
**copy.deepcopy(dashboards_config_2),
|
|
}
|
|
new_configs = {
|
|
**copy.deepcopy(databases_config),
|
|
**copy.deepcopy(datasets_config),
|
|
**copy.deepcopy(charts_config_1),
|
|
**copy.deepcopy(dashboards_config_1),
|
|
}
|
|
expected_number_of_dashboards = len(dashboards_config_1)
|
|
expected_number_of_charts = len(charts_config_1)
|
|
|
|
ImportAssetsCommand._import(session, base_configs)
|
|
ImportAssetsCommand._import(session, new_configs)
|
|
dashboard_ids = session.scalars(
|
|
select(dashboard_slices.c.dashboard_id).distinct()
|
|
).all()
|
|
chart_ids = session.scalars(select(dashboard_slices.c.slice_id)).all()
|
|
|
|
assert len(chart_ids) == expected_number_of_charts
|
|
assert len(dashboard_ids) == expected_number_of_dashboards
|
|
|
|
|
|
def test_import_removes_dashboard_charts(mocker: MockFixture, session: Session) -> None:
|
|
"""
|
|
Test that existing dashboards are updated without old charts.
|
|
"""
|
|
from superset import security_manager
|
|
from superset.commands.importers.v1.assets import ImportAssetsCommand
|
|
from superset.models.dashboard import dashboard_slices
|
|
from superset.models.slice import Slice
|
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
engine = session.get_bind()
|
|
Slice.metadata.create_all(engine) # pylint: disable=no-member
|
|
base_configs = {
|
|
**copy.deepcopy(databases_config),
|
|
**copy.deepcopy(datasets_config),
|
|
**copy.deepcopy(charts_config_1),
|
|
**copy.deepcopy(dashboards_config_1),
|
|
}
|
|
new_configs = {
|
|
**copy.deepcopy(databases_config),
|
|
**copy.deepcopy(datasets_config),
|
|
**copy.deepcopy(charts_config_2),
|
|
**copy.deepcopy(dashboards_config_2),
|
|
}
|
|
expected_number_of_dashboards = len(dashboards_config_2)
|
|
expected_number_of_charts = len(charts_config_2)
|
|
|
|
ImportAssetsCommand._import(session, base_configs)
|
|
ImportAssetsCommand._import(session, new_configs)
|
|
dashboard_ids = session.scalars(
|
|
select(dashboard_slices.c.dashboard_id).distinct()
|
|
).all()
|
|
chart_ids = session.scalars(select(dashboard_slices.c.slice_id)).all()
|
|
|
|
assert len(chart_ids) == expected_number_of_charts
|
|
assert len(dashboard_ids) == expected_number_of_dashboards
|