mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
add physical dataset
This commit is contained in:
@@ -995,12 +995,12 @@ def physical_query_context(physical_dataset) -> Dict[str, Any]:
|
||||
},
|
||||
},
|
||||
)
|
||||
def test_cache_default_timeout(login_as_admin, physical_query_context):
|
||||
def test_cache_default_timeout(test_client, login_as_admin, physical_query_context):
|
||||
rv = test_client.post(CHART_DATA_URI, json=physical_query_context)
|
||||
assert rv.json["result"][0]["cache_timeout"] == 1234
|
||||
|
||||
|
||||
def test_custom_cache_timeout(login_as_admin, physical_query_context):
|
||||
def test_custom_cache_timeout(test_client, login_as_admin, physical_query_context):
|
||||
physical_query_context["custom_cache_timeout"] = 5678
|
||||
rv = test_client.post(CHART_DATA_URI, json=physical_query_context)
|
||||
assert rv.json["result"][0]["cache_timeout"] == 5678
|
||||
@@ -1018,6 +1018,7 @@ def test_custom_cache_timeout(login_as_admin, physical_query_context):
|
||||
},
|
||||
)
|
||||
def test_data_cache_default_timeout(
|
||||
test_client,
|
||||
login_as_admin,
|
||||
physical_query_context,
|
||||
):
|
||||
|
||||
@@ -16,15 +16,18 @@
|
||||
# under the License.
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import functools
|
||||
from typing import Any, Callable, Generator, Optional, TYPE_CHECKING
|
||||
import os
|
||||
from typing import Any, Callable, Optional, TYPE_CHECKING
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask.ctx import AppContext
|
||||
from flask_appbuilder.security.sqla import models as ab_models
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
from superset import db
|
||||
from superset import db, security_manager
|
||||
from superset.extensions import feature_flag_manager
|
||||
from superset.utils.core import json_dumps_w_dates
|
||||
from superset.utils.database import get_example_database, remove_database
|
||||
@@ -70,8 +73,6 @@ def setup_sample_data() -> Any:
|
||||
yield
|
||||
|
||||
with app.app_context():
|
||||
engine = get_example_database().get_sqla_engine()
|
||||
|
||||
# drop sqlachemy tables
|
||||
|
||||
db.session.commit()
|
||||
@@ -101,6 +102,50 @@ def login_as_admin(login_as: Callable[..., None]):
|
||||
yield login_as("admin")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_user(app_context: AppContext):
|
||||
def _create_user(username: str, role: str = "Admin", password: str = "general"):
|
||||
security_manager.add_user(
|
||||
username,
|
||||
"firstname",
|
||||
"lastname",
|
||||
"email@exaple.com",
|
||||
security_manager.find_role(role),
|
||||
password,
|
||||
)
|
||||
return security_manager.find_user(username)
|
||||
|
||||
return _create_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def get_user(app_context: AppContext):
|
||||
def _get_user(username: str) -> ab_models.User:
|
||||
return (
|
||||
db.session.query(security_manager.user_model)
|
||||
.filter_by(username=username)
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
return _get_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def get_or_create_user(get_user, create_user) -> ab_models.User:
|
||||
@contextlib.contextmanager
|
||||
def _get_user(username: str) -> ab_models.User:
|
||||
user = get_user(username)
|
||||
if not user:
|
||||
# if user is created by test, remove it after done
|
||||
user = create_user(username)
|
||||
yield user
|
||||
db.session.delete(user)
|
||||
else:
|
||||
yield user
|
||||
|
||||
return _get_user
|
||||
|
||||
|
||||
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]:
|
||||
@@ -206,3 +251,75 @@ def with_feature_flags(**mock_feature_flags):
|
||||
return functools.update_wrapper(wrapper, test_fn)
|
||||
|
||||
return decorate
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def physical_dataset():
|
||||
from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
|
||||
from superset.connectors.sqla.utils import get_identifier_quoter
|
||||
|
||||
example_database = get_example_database()
|
||||
|
||||
with example_database.get_sqla_engine_with_context() as engine:
|
||||
quoter = get_identifier_quoter(engine.name)
|
||||
# sqlite can only execute one statement at a time
|
||||
engine.execute(
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS physical_dataset(
|
||||
col1 INTEGER,
|
||||
col2 VARCHAR(255),
|
||||
col3 DECIMAL(4,2),
|
||||
col4 VARCHAR(255),
|
||||
col5 TIMESTAMP DEFAULT '1970-01-01 00:00:01',
|
||||
col6 TIMESTAMP DEFAULT '1970-01-01 00:00:01',
|
||||
{quoter('time column with spaces')} TIMESTAMP DEFAULT '1970-01-01 00:00:01'
|
||||
);
|
||||
"""
|
||||
)
|
||||
engine.execute(
|
||||
"""
|
||||
INSERT INTO physical_dataset values
|
||||
(0, 'a', 1.0, NULL, '2000-01-01 00:00:00', '2002-01-03 00:00:00', '2002-01-03 00:00:00'),
|
||||
(1, 'b', 1.1, NULL, '2000-01-02 00:00:00', '2002-02-04 00:00:00', '2002-02-04 00:00:00'),
|
||||
(2, 'c', 1.2, NULL, '2000-01-03 00:00:00', '2002-03-07 00:00:00', '2002-03-07 00:00:00'),
|
||||
(3, 'd', 1.3, NULL, '2000-01-04 00:00:00', '2002-04-12 00:00:00', '2002-04-12 00:00:00'),
|
||||
(4, 'e', 1.4, NULL, '2000-01-05 00:00:00', '2002-05-11 00:00:00', '2002-05-11 00:00:00'),
|
||||
(5, 'f', 1.5, NULL, '2000-01-06 00:00:00', '2002-06-13 00:00:00', '2002-06-13 00:00:00'),
|
||||
(6, 'g', 1.6, NULL, '2000-01-07 00:00:00', '2002-07-15 00:00:00', '2002-07-15 00:00:00'),
|
||||
(7, 'h', 1.7, NULL, '2000-01-08 00:00:00', '2002-08-18 00:00:00', '2002-08-18 00:00:00'),
|
||||
(8, 'i', 1.8, NULL, '2000-01-09 00:00:00', '2002-09-20 00:00:00', '2002-09-20 00:00:00'),
|
||||
(9, 'j', 1.9, NULL, '2000-01-10 00:00:00', '2002-10-22 00:00:00', '2002-10-22 00:00:00');
|
||||
"""
|
||||
)
|
||||
|
||||
dataset = SqlaTable(
|
||||
table_name="physical_dataset",
|
||||
database=example_database,
|
||||
)
|
||||
TableColumn(column_name="col1", type="INTEGER", table=dataset)
|
||||
TableColumn(column_name="col2", type="VARCHAR(255)", table=dataset)
|
||||
TableColumn(column_name="col3", type="DECIMAL(4,2)", table=dataset)
|
||||
TableColumn(column_name="col4", type="VARCHAR(255)", table=dataset)
|
||||
TableColumn(column_name="col5", type="TIMESTAMP", is_dttm=True, table=dataset)
|
||||
TableColumn(column_name="col6", type="TIMESTAMP", is_dttm=True, table=dataset)
|
||||
TableColumn(
|
||||
column_name="time column with spaces",
|
||||
type="TIMESTAMP",
|
||||
is_dttm=True,
|
||||
table=dataset,
|
||||
)
|
||||
SqlMetric(metric_name="count", expression="count(*)", table=dataset)
|
||||
db.session.merge(dataset)
|
||||
db.session.commit()
|
||||
|
||||
yield dataset
|
||||
|
||||
engine.execute(
|
||||
"""
|
||||
DROP TABLE physical_dataset;
|
||||
"""
|
||||
)
|
||||
dataset = db.session.query(SqlaTable).filter_by(table_name="physical_dataset").all()
|
||||
for ds in dataset:
|
||||
db.session.delete(ds)
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user