2019-01-15 18:53:27 -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.
|
2020-08-27 12:49:18 -04:00
|
|
|
# isort:skip_file
|
2021-05-14 15:49:25 -04:00
|
|
|
import functools
|
2020-08-10 16:20:19 -04:00
|
|
|
from typing import Any
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
import pytest
|
2020-08-27 12:49:18 -04:00
|
|
|
from sqlalchemy.engine import Engine
|
2021-05-14 15:49:25 -04:00
|
|
|
from unittest.mock import patch
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.test_app import app
|
2020-08-27 12:49:18 -04:00
|
|
|
from superset import db
|
2021-05-14 15:49:25 -04:00
|
|
|
from superset.extensions import feature_flag_manager
|
2021-04-01 21:10:17 -04:00
|
|
|
from superset.utils.core import get_example_database, json_dumps_w_dates
|
2019-11-20 10:47:06 -05:00
|
|
|
|
2020-08-27 12:49:18 -04:00
|
|
|
|
|
|
|
CTAS_SCHEMA_NAME = "sqllab_test_db"
|
|
|
|
ADMIN_SCHEMA_NAME = "admin_database"
|
2019-11-20 10:47:06 -05:00
|
|
|
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2021-04-23 13:50:49 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def app_context():
|
|
|
|
with app.app_context():
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
@pytest.fixture(autouse=True, scope="session")
|
|
|
|
def setup_sample_data() -> Any:
|
2021-04-27 23:47:32 -04:00
|
|
|
# TODO(john-bodley): Determine a cleaner way of setting up the sample data without
|
2021-07-01 11:03:07 -04:00
|
|
|
# relying on `tests.integration_tests.test_app.app` leveraging an `app` fixture which is purposely
|
2021-04-27 23:47:32 -04:00
|
|
|
# scoped to the function level to ensure tests remain idempotent.
|
2020-08-10 16:20:19 -04:00
|
|
|
with app.app_context():
|
2020-08-27 12:49:18 -04:00
|
|
|
setup_presto_if_needed()
|
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
from superset.cli import load_test_users_run
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
load_test_users_run()
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
from superset import examples
|
2018-10-16 20:59:34 -04:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
examples.load_css_templates()
|
2019-11-20 10:47:06 -05:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
yield
|
2019-07-17 00:36:56 -04:00
|
|
|
|
2020-08-10 16:20:19 -04:00
|
|
|
with app.app_context():
|
|
|
|
engine = get_example_database().get_sqla_engine()
|
2020-08-27 12:49:18 -04:00
|
|
|
|
|
|
|
# drop sqlachemy tables
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
from sqlalchemy.ext import declarative
|
|
|
|
|
|
|
|
sqla_base = declarative.declarative_base()
|
|
|
|
# uses sorted_tables to drop in proper order without violating foreign constrains
|
|
|
|
for table in sqla_base.metadata.sorted_tables:
|
|
|
|
table.__table__.drop()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def drop_from_schema(engine: Engine, schema_name: str):
|
|
|
|
schemas = engine.execute(f"SHOW SCHEMAS").fetchall()
|
|
|
|
if schema_name not in [s[0] for s in schemas]:
|
|
|
|
# schema doesn't exist
|
|
|
|
return
|
|
|
|
tables_or_views = engine.execute(f"SHOW TABLES in {schema_name}").fetchall()
|
|
|
|
for tv in tables_or_views:
|
|
|
|
engine.execute(f"DROP TABLE IF EXISTS {schema_name}.{tv[0]}")
|
|
|
|
engine.execute(f"DROP VIEW IF EXISTS {schema_name}.{tv[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
def setup_presto_if_needed():
|
|
|
|
backend = app.config["SQLALCHEMY_EXAMPLES_URI"].split("://")[0]
|
2021-04-01 21:10:17 -04:00
|
|
|
database = get_example_database()
|
|
|
|
extra = database.get_extra()
|
|
|
|
|
2020-08-27 12:49:18 -04:00
|
|
|
if backend == "presto":
|
|
|
|
# decrease poll interval for tests
|
2021-04-01 21:10:17 -04:00
|
|
|
extra = {
|
|
|
|
**extra,
|
|
|
|
"engine_params": {
|
|
|
|
"connect_args": {"poll_interval": app.config["PRESTO_POLL_INTERVAL"]}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
# remove `poll_interval` from databases that do not support it
|
|
|
|
extra = {**extra, "engine_params": {}}
|
|
|
|
database.extra = json_dumps_w_dates(extra)
|
|
|
|
db.session.commit()
|
2020-08-27 12:49:18 -04:00
|
|
|
|
|
|
|
if backend in {"presto", "hive"}:
|
|
|
|
database = get_example_database()
|
|
|
|
engine = database.get_sqla_engine()
|
|
|
|
drop_from_schema(engine, CTAS_SCHEMA_NAME)
|
|
|
|
engine.execute(f"DROP SCHEMA IF EXISTS {CTAS_SCHEMA_NAME}")
|
|
|
|
engine.execute(f"CREATE SCHEMA {CTAS_SCHEMA_NAME}")
|
|
|
|
|
|
|
|
drop_from_schema(engine, ADMIN_SCHEMA_NAME)
|
|
|
|
engine.execute(f"DROP SCHEMA IF EXISTS {ADMIN_SCHEMA_NAME}")
|
|
|
|
engine.execute(f"CREATE SCHEMA {ADMIN_SCHEMA_NAME}")
|
2021-05-14 15:49:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
def with_feature_flags(**mock_feature_flags):
|
|
|
|
"""
|
2021-07-01 11:03:07 -04:00
|
|
|
Use this decorator to mock feature flags in tests.integration_tests.
|
2021-05-14 15:49:25 -04:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
class TestYourFeature(SupersetTestCase):
|
|
|
|
|
|
|
|
@with_feature_flags(YOUR_FEATURE=True)
|
|
|
|
def test_your_feature_enabled(self):
|
|
|
|
self.assertEqual(is_feature_enabled("YOUR_FEATURE"), True)
|
|
|
|
|
|
|
|
@with_feature_flags(YOUR_FEATURE=False)
|
|
|
|
def test_your_feature_disabled(self):
|
|
|
|
self.assertEqual(is_feature_enabled("YOUR_FEATURE"), False)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def mock_get_feature_flags():
|
|
|
|
feature_flags = feature_flag_manager._feature_flags or {}
|
|
|
|
return {**feature_flags, **mock_feature_flags}
|
|
|
|
|
|
|
|
def decorate(test_fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
with patch.object(
|
|
|
|
feature_flag_manager,
|
|
|
|
"get_feature_flags",
|
|
|
|
side_effect=mock_get_feature_flags,
|
|
|
|
):
|
|
|
|
test_fn(*args, **kwargs)
|
|
|
|
|
|
|
|
return functools.update_wrapper(wrapper, test_fn)
|
|
|
|
|
|
|
|
return decorate
|