mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
chore(sqla): refactor query utils (#21811)
Co-authored-by: Ville Brofeldt <ville.brofeldt@apple.com>
This commit is contained in:
committed by
Michael S. Molina
parent
4c21c7b038
commit
7c98e266ce
@@ -21,13 +21,15 @@ from typing import Any, Callable, Generator, Optional, TYPE_CHECKING
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask.ctx import AppContext
|
||||
from flask.testing import FlaskClient
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
from superset import db
|
||||
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
|
||||
from tests.integration_tests.test_app import app
|
||||
from tests.integration_tests.test_app import app, login
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from superset.connectors.sqla.models import Database
|
||||
@@ -42,6 +44,29 @@ def app_context():
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_client(app_context: AppContext):
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def login_as(test_client: "FlaskClient[Any]"):
|
||||
"""Fixture with app context and logged in admin user."""
|
||||
|
||||
def _login_as(username: str, password: str = "general"):
|
||||
login(test_client, username=username, password=password)
|
||||
|
||||
yield _login_as
|
||||
# no need to log out as both app_context and test_client are
|
||||
# function level fixtures anyway
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def login_as_admin(login_as: Callable[..., None]):
|
||||
yield login_as("admin")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="session")
|
||||
def setup_sample_data() -> Any:
|
||||
# TODO(john-bodley): Determine a cleaner way of setting up the sample data without
|
||||
@@ -180,3 +205,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