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.
|
2018-08-06 18:30:13 -04:00
|
|
|
"""Fixtures for test_datasource.py"""
|
2024-04-24 20:19:53 -04:00
|
|
|
|
2023-06-01 15:01:10 -04:00
|
|
|
from collections.abc import Generator
|
|
|
|
from typing import Any
|
2021-08-02 15:45:55 -04:00
|
|
|
|
2022-06-13 20:30:13 -04:00
|
|
|
import pytest
|
|
|
|
from sqlalchemy import Column, create_engine, Date, Integer, MetaData, String, Table
|
2022-07-18 18:21:38 -04:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2022-06-13 20:30:13 -04:00
|
|
|
|
2024-04-24 20:19:53 -04:00
|
|
|
from superset.columns.models import Column as Sl_Column # noqa: F401
|
2022-06-13 20:30:13 -04:00
|
|
|
from superset.connectors.sqla.models import SqlaTable, TableColumn
|
|
|
|
from superset.extensions import db
|
|
|
|
from superset.models.core import Database
|
2024-04-24 20:19:53 -04:00
|
|
|
from superset.tables.models import Table as Sl_Table # noqa: F401
|
2022-01-16 01:32:50 -05:00
|
|
|
from superset.utils.core import get_example_default_schema
|
2024-04-24 20:19:53 -04:00
|
|
|
from superset.utils.database import get_example_database # noqa: F401
|
2022-06-13 20:30:13 -04:00
|
|
|
from tests.integration_tests.test_app import app
|
2021-11-04 14:09:08 -04:00
|
|
|
|
2021-08-02 15:45:55 -04:00
|
|
|
|
2023-06-01 15:01:10 -04:00
|
|
|
def get_datasource_post() -> dict[str, Any]:
|
2021-11-04 14:09:08 -04:00
|
|
|
schema = get_example_default_schema()
|
|
|
|
|
2021-08-02 15:45:55 -04:00
|
|
|
return {
|
|
|
|
"id": None,
|
|
|
|
"column_formats": {"ratio": ".2%"},
|
|
|
|
"database": {"id": 1},
|
|
|
|
"description": "Adding a DESCRip",
|
|
|
|
"default_endpoint": "",
|
|
|
|
"filter_select_enabled": True,
|
2021-11-04 14:09:08 -04:00
|
|
|
"name": f"{schema}.birth_names" if schema else "birth_names",
|
2021-08-02 15:45:55 -04:00
|
|
|
"table_name": "birth_names",
|
|
|
|
"datasource_name": "birth_names",
|
|
|
|
"type": "table",
|
2021-11-04 14:09:08 -04:00
|
|
|
"schema": schema,
|
2021-08-02 15:45:55 -04:00
|
|
|
"offset": 66,
|
|
|
|
"cache_timeout": 55,
|
|
|
|
"sql": "",
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"id": 504,
|
|
|
|
"column_name": "ds",
|
|
|
|
"verbose_name": "",
|
|
|
|
"description": None,
|
|
|
|
"expression": "",
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": True,
|
|
|
|
"type": "DATETIME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 505,
|
|
|
|
"column_name": "gender",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": "",
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": False,
|
|
|
|
"type": "VARCHAR(16)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 506,
|
|
|
|
"column_name": "name",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": None,
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": None,
|
|
|
|
"type": "VARCHAR(255)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 508,
|
|
|
|
"column_name": "state",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": None,
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": None,
|
|
|
|
"type": "VARCHAR(10)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 509,
|
|
|
|
"column_name": "num_boys",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": None,
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": None,
|
|
|
|
"type": "BIGINT(20)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 510,
|
|
|
|
"column_name": "num_girls",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": "",
|
|
|
|
"filterable": False,
|
|
|
|
"groupby": False,
|
|
|
|
"is_dttm": False,
|
|
|
|
"type": "BIGINT(20)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 532,
|
|
|
|
"column_name": "num",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": None,
|
|
|
|
"filterable": True,
|
|
|
|
"groupby": True,
|
|
|
|
"is_dttm": None,
|
|
|
|
"type": "BIGINT(20)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 522,
|
|
|
|
"column_name": "num_california",
|
|
|
|
"verbose_name": None,
|
|
|
|
"description": None,
|
|
|
|
"expression": "CASE WHEN state = 'CA' THEN num ELSE 0 END",
|
|
|
|
"filterable": False,
|
|
|
|
"groupby": False,
|
|
|
|
"is_dttm": False,
|
|
|
|
"type": "NUMBER",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"metrics": [
|
|
|
|
{
|
|
|
|
"id": 824,
|
|
|
|
"metric_name": "sum__num",
|
|
|
|
"verbose_name": "Babies",
|
|
|
|
"description": "",
|
|
|
|
"expression": "SUM(num)",
|
|
|
|
"warning_text": "",
|
|
|
|
"d3format": "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 836,
|
|
|
|
"metric_name": "count",
|
|
|
|
"verbose_name": "",
|
|
|
|
"description": None,
|
|
|
|
"expression": "count(1)",
|
|
|
|
"warning_text": None,
|
|
|
|
"d3format": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 843,
|
|
|
|
"metric_name": "ratio",
|
|
|
|
"verbose_name": "Ratio Boys/Girls",
|
|
|
|
"description": "This represents the ratio of boys/girls",
|
|
|
|
"expression": "sum(num_boys) / sum(num_girls)",
|
|
|
|
"warning_text": "no warning",
|
|
|
|
"d3format": ".2%",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2022-06-13 20:30:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2024-04-24 16:46:35 -04:00
|
|
|
@pytest.mark.usefixtures("app_conntext")
|
2022-06-13 20:30:13 -04:00
|
|
|
def load_dataset_with_columns() -> Generator[SqlaTable, None, None]:
|
2024-04-24 16:46:35 -04:00
|
|
|
engine = create_engine(app.config["SQLALCHEMY_DATABASE_URI"], echo=True)
|
|
|
|
meta = MetaData()
|
2022-06-13 20:30:13 -04:00
|
|
|
|
2024-04-24 16:46:35 -04:00
|
|
|
students = Table(
|
|
|
|
"students",
|
|
|
|
meta,
|
|
|
|
Column("id", Integer, primary_key=True),
|
|
|
|
Column("name", String(255)),
|
|
|
|
Column("lastname", String(255)),
|
|
|
|
Column("ds", Date),
|
|
|
|
)
|
|
|
|
meta.create_all(engine)
|
2022-06-13 20:30:13 -04:00
|
|
|
|
2024-04-24 16:46:35 -04:00
|
|
|
students.insert().values(name="George", ds="2021-01-01")
|
2022-06-13 20:30:13 -04:00
|
|
|
|
2024-04-24 16:46:35 -04:00
|
|
|
dataset = SqlaTable(
|
|
|
|
database_id=db.session.query(Database).first().id, table_name="students"
|
|
|
|
)
|
|
|
|
column = TableColumn(table_id=dataset.id, column_name="name")
|
|
|
|
dataset.columns = [column]
|
|
|
|
db.session.add(dataset)
|
|
|
|
db.session.commit()
|
|
|
|
yield dataset
|
2022-06-13 20:30:13 -04:00
|
|
|
|
2024-04-24 16:46:35 -04:00
|
|
|
# cleanup
|
|
|
|
if (students_table := meta.tables.get("students")) is not None:
|
|
|
|
base = declarative_base()
|
|
|
|
# needed for sqlite
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.commit()
|
2024-04-24 16:46:35 -04:00
|
|
|
base.metadata.drop_all(engine, [students_table], checkfirst=True)
|
|
|
|
db.session.delete(dataset)
|
|
|
|
db.session.delete(column)
|
|
|
|
db.session.commit()
|