2021-12-14 21:33:52 -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.
|
|
|
|
# pylint: disable=import-outside-toplevel, unused-argument, unused-import, invalid-name
|
|
|
|
|
2022-03-24 13:41:22 -04:00
|
|
|
import copy
|
2021-12-14 21:33:52 -05:00
|
|
|
import json
|
2023-02-06 11:17:08 -05:00
|
|
|
import re
|
2021-12-14 21:33:52 -05:00
|
|
|
import uuid
|
2023-06-01 15:01:10 -04:00
|
|
|
from typing import Any
|
2023-02-06 11:17:08 -05:00
|
|
|
from unittest.mock import Mock, patch
|
2021-12-14 21:33:52 -05:00
|
|
|
|
2023-02-06 11:17:08 -05:00
|
|
|
import pytest
|
|
|
|
from flask import current_app
|
2023-03-15 11:31:09 -04:00
|
|
|
from pytest_mock import MockFixture
|
2021-12-14 21:33:52 -05:00
|
|
|
from sqlalchemy.orm.session import Session
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
from superset import db
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.exceptions import (
|
2023-03-15 11:31:09 -04:00
|
|
|
DatasetForbiddenDataURI,
|
|
|
|
ImportFailedError,
|
|
|
|
)
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import validate_data_uri
|
2023-02-06 11:17:08 -05:00
|
|
|
|
2021-12-22 12:41:31 -05:00
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
def test_import_dataset(mocker: MockFixture, session: Session) -> None:
|
2021-12-14 21:33:52 -05:00
|
|
|
"""
|
|
|
|
Test importing a dataset.
|
|
|
|
"""
|
2023-03-15 11:31:09 -04:00
|
|
|
from superset import security_manager
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2023-02-06 11:17:08 -05:00
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
2021-12-14 21:33:52 -05:00
|
|
|
from superset.models.core import Database
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2021-12-14 21:33:52 -05:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2021-12-14 21:33:52 -05:00
|
|
|
|
|
|
|
dataset_uuid = uuid.uuid4()
|
|
|
|
config = {
|
|
|
|
"table_name": "my_table",
|
|
|
|
"main_dttm_col": "ds",
|
|
|
|
"description": "This is the description",
|
|
|
|
"default_endpoint": None,
|
|
|
|
"offset": -8,
|
|
|
|
"cache_timeout": 3600,
|
|
|
|
"schema": "my_schema",
|
|
|
|
"sql": None,
|
|
|
|
"params": {
|
|
|
|
"remote_id": 64,
|
|
|
|
"database_name": "examples",
|
|
|
|
"import_time": 1606677834,
|
|
|
|
},
|
2022-03-29 13:03:09 -04:00
|
|
|
"template_params": {
|
|
|
|
"answer": "42",
|
|
|
|
},
|
2021-12-14 21:33:52 -05:00
|
|
|
"filter_select_enabled": True,
|
|
|
|
"fetch_values_predicate": "foo IN (1, 2)",
|
2021-12-22 12:41:31 -05:00
|
|
|
"extra": {"warning_markdown": "*WARNING*"},
|
2021-12-14 21:33:52 -05:00
|
|
|
"uuid": dataset_uuid,
|
|
|
|
"metrics": [
|
|
|
|
{
|
|
|
|
"metric_name": "cnt",
|
|
|
|
"verbose_name": None,
|
|
|
|
"metric_type": None,
|
|
|
|
"expression": "COUNT(*)",
|
|
|
|
"description": None,
|
|
|
|
"d3format": None,
|
2021-12-22 13:39:29 -05:00
|
|
|
"extra": {"warning_markdown": None},
|
2021-12-14 21:33:52 -05:00
|
|
|
"warning_text": None,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"column_name": "profit",
|
|
|
|
"verbose_name": None,
|
|
|
|
"is_dttm": None,
|
|
|
|
"is_active": None,
|
|
|
|
"type": "INTEGER",
|
|
|
|
"groupby": None,
|
|
|
|
"filterable": None,
|
|
|
|
"expression": "revenue-expenses",
|
|
|
|
"description": None,
|
|
|
|
"python_date_format": None,
|
2022-03-29 13:03:09 -04:00
|
|
|
"extra": {
|
|
|
|
"certified_by": "User",
|
|
|
|
},
|
2021-12-14 21:33:52 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"database_uuid": database.uuid,
|
|
|
|
"database_id": database.id,
|
|
|
|
}
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
sqla_table = import_dataset(config)
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.table_name == "my_table"
|
|
|
|
assert sqla_table.main_dttm_col == "ds"
|
|
|
|
assert sqla_table.description == "This is the description"
|
|
|
|
assert sqla_table.default_endpoint is None
|
|
|
|
assert sqla_table.offset == -8
|
|
|
|
assert sqla_table.cache_timeout == 3600
|
|
|
|
assert sqla_table.schema == "my_schema"
|
|
|
|
assert sqla_table.sql is None
|
|
|
|
assert sqla_table.params == json.dumps(
|
|
|
|
{"remote_id": 64, "database_name": "examples", "import_time": 1606677834}
|
|
|
|
)
|
|
|
|
assert sqla_table.template_params == json.dumps({"answer": "42"})
|
|
|
|
assert sqla_table.filter_select_enabled is True
|
|
|
|
assert sqla_table.fetch_values_predicate == "foo IN (1, 2)"
|
|
|
|
assert sqla_table.extra == '{"warning_markdown": "*WARNING*"}'
|
|
|
|
assert sqla_table.uuid == dataset_uuid
|
|
|
|
assert len(sqla_table.metrics) == 1
|
|
|
|
assert sqla_table.metrics[0].metric_name == "cnt"
|
|
|
|
assert sqla_table.metrics[0].verbose_name is None
|
|
|
|
assert sqla_table.metrics[0].metric_type is None
|
|
|
|
assert sqla_table.metrics[0].expression == "COUNT(*)"
|
|
|
|
assert sqla_table.metrics[0].description is None
|
|
|
|
assert sqla_table.metrics[0].d3format is None
|
2021-12-22 13:39:29 -05:00
|
|
|
assert sqla_table.metrics[0].extra == '{"warning_markdown": null}'
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.metrics[0].warning_text is None
|
|
|
|
assert len(sqla_table.columns) == 1
|
|
|
|
assert sqla_table.columns[0].column_name == "profit"
|
|
|
|
assert sqla_table.columns[0].verbose_name is None
|
2022-07-18 18:21:38 -04:00
|
|
|
assert sqla_table.columns[0].is_dttm is False
|
|
|
|
assert sqla_table.columns[0].is_active is True
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.columns[0].type == "INTEGER"
|
2022-07-18 18:21:38 -04:00
|
|
|
assert sqla_table.columns[0].groupby is True
|
|
|
|
assert sqla_table.columns[0].filterable is True
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.columns[0].expression == "revenue-expenses"
|
|
|
|
assert sqla_table.columns[0].description is None
|
|
|
|
assert sqla_table.columns[0].python_date_format is None
|
2022-07-16 03:00:39 -04:00
|
|
|
assert sqla_table.columns[0].extra == '{"certified_by": "User"}'
|
|
|
|
assert sqla_table.database.uuid == database.uuid
|
|
|
|
assert sqla_table.database.id == database.id
|
|
|
|
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
def test_import_dataset_duplicate_column(mocker: MockFixture, session: Session) -> None:
|
2022-07-16 03:00:39 -04:00
|
|
|
"""
|
|
|
|
Test importing a dataset with a column that already exists.
|
|
|
|
"""
|
2023-03-15 11:31:09 -04:00
|
|
|
from superset import security_manager
|
2022-07-16 03:00:39 -04:00
|
|
|
from superset.columns.models import Column as NewColumn
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2022-07-16 03:00:39 -04:00
|
|
|
from superset.connectors.sqla.models import SqlaTable, TableColumn
|
|
|
|
from superset.models.core import Database
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2022-07-16 03:00:39 -04:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
dataset_uuid = uuid.uuid4()
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2022-07-16 03:00:39 -04:00
|
|
|
|
|
|
|
dataset = SqlaTable(
|
|
|
|
uuid=dataset_uuid, table_name="existing_dataset", database_id=database.id
|
|
|
|
)
|
|
|
|
column = TableColumn(column_name="existing_column")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(dataset)
|
|
|
|
db.session.add(column)
|
|
|
|
db.session.flush()
|
2022-07-16 03:00:39 -04:00
|
|
|
|
|
|
|
config = {
|
|
|
|
"table_name": dataset.table_name,
|
|
|
|
"main_dttm_col": "ds",
|
|
|
|
"description": "This is the description",
|
|
|
|
"default_endpoint": None,
|
|
|
|
"offset": -8,
|
|
|
|
"cache_timeout": 3600,
|
|
|
|
"schema": "my_schema",
|
|
|
|
"sql": None,
|
|
|
|
"params": {
|
|
|
|
"remote_id": 64,
|
|
|
|
"database_name": "examples",
|
|
|
|
"import_time": 1606677834,
|
|
|
|
},
|
|
|
|
"template_params": {
|
|
|
|
"answer": "42",
|
|
|
|
},
|
|
|
|
"filter_select_enabled": True,
|
|
|
|
"fetch_values_predicate": "foo IN (1, 2)",
|
|
|
|
"extra": {"warning_markdown": "*WARNING*"},
|
|
|
|
"uuid": dataset_uuid,
|
|
|
|
"metrics": [
|
|
|
|
{
|
|
|
|
"metric_name": "cnt",
|
|
|
|
"verbose_name": None,
|
|
|
|
"metric_type": None,
|
|
|
|
"expression": "COUNT(*)",
|
|
|
|
"description": None,
|
|
|
|
"d3format": None,
|
|
|
|
"extra": {"warning_markdown": None},
|
|
|
|
"warning_text": None,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"column_name": column.column_name,
|
|
|
|
"verbose_name": None,
|
|
|
|
"is_dttm": None,
|
|
|
|
"is_active": None,
|
|
|
|
"type": "INTEGER",
|
|
|
|
"groupby": None,
|
|
|
|
"filterable": None,
|
|
|
|
"expression": "revenue-expenses",
|
|
|
|
"description": None,
|
|
|
|
"python_date_format": None,
|
|
|
|
"extra": {
|
|
|
|
"certified_by": "User",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"database_uuid": database.uuid,
|
|
|
|
"database_id": database.id,
|
|
|
|
}
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
sqla_table = import_dataset(config, overwrite=True)
|
2022-07-16 03:00:39 -04:00
|
|
|
assert sqla_table.table_name == dataset.table_name
|
|
|
|
assert sqla_table.main_dttm_col == "ds"
|
|
|
|
assert sqla_table.description == "This is the description"
|
|
|
|
assert sqla_table.default_endpoint is None
|
|
|
|
assert sqla_table.offset == -8
|
|
|
|
assert sqla_table.cache_timeout == 3600
|
|
|
|
assert sqla_table.schema == "my_schema"
|
|
|
|
assert sqla_table.sql is None
|
|
|
|
assert sqla_table.params == json.dumps(
|
|
|
|
{"remote_id": 64, "database_name": "examples", "import_time": 1606677834}
|
|
|
|
)
|
|
|
|
assert sqla_table.template_params == json.dumps({"answer": "42"})
|
|
|
|
assert sqla_table.filter_select_enabled is True
|
|
|
|
assert sqla_table.fetch_values_predicate == "foo IN (1, 2)"
|
|
|
|
assert sqla_table.extra == '{"warning_markdown": "*WARNING*"}'
|
|
|
|
assert sqla_table.uuid == dataset_uuid
|
|
|
|
assert len(sqla_table.metrics) == 1
|
|
|
|
assert sqla_table.metrics[0].metric_name == "cnt"
|
|
|
|
assert sqla_table.metrics[0].verbose_name is None
|
|
|
|
assert sqla_table.metrics[0].metric_type is None
|
|
|
|
assert sqla_table.metrics[0].expression == "COUNT(*)"
|
|
|
|
assert sqla_table.metrics[0].description is None
|
|
|
|
assert sqla_table.metrics[0].d3format is None
|
|
|
|
assert sqla_table.metrics[0].extra == '{"warning_markdown": null}'
|
|
|
|
assert sqla_table.metrics[0].warning_text is None
|
|
|
|
assert len(sqla_table.columns) == 1
|
|
|
|
assert sqla_table.columns[0].column_name == column.column_name
|
|
|
|
assert sqla_table.columns[0].verbose_name is None
|
|
|
|
assert sqla_table.columns[0].is_dttm is False
|
|
|
|
assert sqla_table.columns[0].is_active is True
|
|
|
|
assert sqla_table.columns[0].type == "INTEGER"
|
|
|
|
assert sqla_table.columns[0].groupby is True
|
|
|
|
assert sqla_table.columns[0].filterable is True
|
|
|
|
assert sqla_table.columns[0].expression == "revenue-expenses"
|
|
|
|
assert sqla_table.columns[0].description is None
|
|
|
|
assert sqla_table.columns[0].python_date_format is None
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.columns[0].extra == '{"certified_by": "User"}'
|
|
|
|
assert sqla_table.database.uuid == database.uuid
|
|
|
|
assert sqla_table.database.id == database.id
|
|
|
|
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
def test_import_column_extra_is_string(mocker: MockFixture, session: Session) -> None:
|
2021-12-14 21:33:52 -05:00
|
|
|
"""
|
|
|
|
Test importing a dataset when the column extra is a string.
|
|
|
|
"""
|
2023-03-15 11:31:09 -04:00
|
|
|
from superset import security_manager
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2021-12-14 21:33:52 -05:00
|
|
|
from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
|
2022-02-24 14:02:01 -05:00
|
|
|
from superset.datasets.schemas import ImportV1DatasetSchema
|
2021-12-14 21:33:52 -05:00
|
|
|
from superset.models.core import Database
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2021-12-14 21:33:52 -05:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2021-12-14 21:33:52 -05:00
|
|
|
|
|
|
|
dataset_uuid = uuid.uuid4()
|
2023-06-01 15:01:10 -04:00
|
|
|
yaml_config: dict[str, Any] = {
|
2021-12-22 12:41:31 -05:00
|
|
|
"version": "1.0.0",
|
2021-12-14 21:33:52 -05:00
|
|
|
"table_name": "my_table",
|
|
|
|
"main_dttm_col": "ds",
|
|
|
|
"description": "This is the description",
|
|
|
|
"default_endpoint": None,
|
|
|
|
"offset": -8,
|
|
|
|
"cache_timeout": 3600,
|
|
|
|
"schema": "my_schema",
|
|
|
|
"sql": None,
|
|
|
|
"params": {
|
|
|
|
"remote_id": 64,
|
|
|
|
"database_name": "examples",
|
|
|
|
"import_time": 1606677834,
|
|
|
|
},
|
2022-03-29 13:03:09 -04:00
|
|
|
"template_params": {
|
|
|
|
"answer": "42",
|
|
|
|
},
|
2021-12-14 21:33:52 -05:00
|
|
|
"filter_select_enabled": True,
|
|
|
|
"fetch_values_predicate": "foo IN (1, 2)",
|
|
|
|
"extra": '{"warning_markdown": "*WARNING*"}',
|
|
|
|
"uuid": dataset_uuid,
|
2021-12-22 13:39:29 -05:00
|
|
|
"metrics": [
|
|
|
|
{
|
|
|
|
"metric_name": "cnt",
|
|
|
|
"verbose_name": None,
|
|
|
|
"metric_type": None,
|
|
|
|
"expression": "COUNT(*)",
|
|
|
|
"description": None,
|
|
|
|
"d3format": None,
|
|
|
|
"extra": '{"warning_markdown": null}',
|
|
|
|
"warning_text": None,
|
|
|
|
}
|
|
|
|
],
|
2021-12-14 21:33:52 -05:00
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"column_name": "profit",
|
|
|
|
"verbose_name": None,
|
2021-12-22 12:41:31 -05:00
|
|
|
"is_dttm": False,
|
|
|
|
"is_active": True,
|
2021-12-14 21:33:52 -05:00
|
|
|
"type": "INTEGER",
|
2021-12-22 12:41:31 -05:00
|
|
|
"groupby": False,
|
|
|
|
"filterable": False,
|
2021-12-14 21:33:52 -05:00
|
|
|
"expression": "revenue-expenses",
|
|
|
|
"description": None,
|
|
|
|
"python_date_format": None,
|
|
|
|
"extra": '{"certified_by": "User"}',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"database_uuid": database.uuid,
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:41:22 -04:00
|
|
|
# the Marshmallow schema should convert strings to objects
|
2021-12-22 12:41:31 -05:00
|
|
|
schema = ImportV1DatasetSchema()
|
|
|
|
dataset_config = schema.load(yaml_config)
|
|
|
|
dataset_config["database_id"] = database.id
|
2024-02-13 12:20:15 -05:00
|
|
|
sqla_table = import_dataset(dataset_config)
|
2021-12-22 12:41:31 -05:00
|
|
|
|
2021-12-22 13:39:29 -05:00
|
|
|
assert sqla_table.metrics[0].extra == '{"warning_markdown": null}'
|
2021-12-14 21:33:52 -05:00
|
|
|
assert sqla_table.columns[0].extra == '{"certified_by": "User"}'
|
2021-12-22 12:41:31 -05:00
|
|
|
assert sqla_table.extra == '{"warning_markdown": "*WARNING*"}'
|
2022-03-24 13:41:22 -04:00
|
|
|
|
|
|
|
|
2023-07-13 19:19:00 -04:00
|
|
|
def test_import_dataset_extra_empty_string(
|
|
|
|
mocker: MockFixture, session: Session
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Test importing a dataset when the extra field is an empty string.
|
|
|
|
"""
|
|
|
|
from superset import security_manager
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2023-07-13 19:19:00 -04:00
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
|
|
|
from superset.datasets.schemas import ImportV1DatasetSchema
|
|
|
|
from superset.models.core import Database
|
|
|
|
|
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2023-07-13 19:19:00 -04:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2023-07-13 19:19:00 -04:00
|
|
|
|
|
|
|
dataset_uuid = uuid.uuid4()
|
|
|
|
yaml_config: dict[str, Any] = {
|
|
|
|
"version": "1.0.0",
|
|
|
|
"table_name": "my_table",
|
|
|
|
"main_dttm_col": "ds",
|
|
|
|
"schema": "my_schema",
|
|
|
|
"sql": None,
|
|
|
|
"params": {
|
|
|
|
"remote_id": 64,
|
|
|
|
"database_name": "examples",
|
|
|
|
"import_time": 1606677834,
|
|
|
|
},
|
|
|
|
"extra": " ",
|
|
|
|
"uuid": dataset_uuid,
|
|
|
|
"metrics": [
|
|
|
|
{
|
|
|
|
"metric_name": "cnt",
|
|
|
|
"expression": "COUNT(*)",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"column_name": "profit",
|
|
|
|
"is_dttm": False,
|
|
|
|
"is_active": True,
|
|
|
|
"type": "INTEGER",
|
|
|
|
"groupby": False,
|
|
|
|
"filterable": False,
|
|
|
|
"expression": "revenue-expenses",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"database_uuid": database.uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
schema = ImportV1DatasetSchema()
|
|
|
|
dataset_config = schema.load(yaml_config)
|
|
|
|
dataset_config["database_id"] = database.id
|
2024-02-13 12:20:15 -05:00
|
|
|
sqla_table = import_dataset(dataset_config)
|
2023-07-13 19:19:00 -04:00
|
|
|
|
|
|
|
assert sqla_table.extra == None
|
|
|
|
|
|
|
|
|
2023-11-22 14:55:54 -05:00
|
|
|
@patch("superset.commands.dataset.importers.v1.utils.request")
|
2023-03-15 11:31:09 -04:00
|
|
|
def test_import_column_allowed_data_url(
|
|
|
|
request: Mock,
|
|
|
|
mocker: MockFixture,
|
|
|
|
session: Session,
|
|
|
|
) -> None:
|
2023-02-06 11:17:08 -05:00
|
|
|
"""
|
|
|
|
Test importing a dataset when using data key to fetch data from a URL.
|
|
|
|
"""
|
|
|
|
import io
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
from superset import security_manager
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2023-02-06 11:17:08 -05:00
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
|
|
|
from superset.datasets.schemas import ImportV1DatasetSchema
|
|
|
|
from superset.models.core import Database
|
|
|
|
|
|
|
|
request.urlopen.return_value = io.StringIO("col1\nvalue1\nvalue2\n")
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2023-02-06 11:17:08 -05:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2023-02-06 11:17:08 -05:00
|
|
|
|
|
|
|
dataset_uuid = uuid.uuid4()
|
2023-06-01 15:01:10 -04:00
|
|
|
yaml_config: dict[str, Any] = {
|
2023-02-06 11:17:08 -05:00
|
|
|
"version": "1.0.0",
|
|
|
|
"table_name": "my_table",
|
|
|
|
"main_dttm_col": "ds",
|
|
|
|
"description": "This is the description",
|
|
|
|
"default_endpoint": None,
|
|
|
|
"offset": -8,
|
|
|
|
"cache_timeout": 3600,
|
|
|
|
"schema": None,
|
|
|
|
"sql": None,
|
|
|
|
"params": {
|
|
|
|
"remote_id": 64,
|
|
|
|
"database_name": "examples",
|
|
|
|
"import_time": 1606677834,
|
|
|
|
},
|
|
|
|
"template_params": None,
|
|
|
|
"filter_select_enabled": True,
|
|
|
|
"fetch_values_predicate": None,
|
|
|
|
"extra": None,
|
|
|
|
"uuid": dataset_uuid,
|
|
|
|
"metrics": [],
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"column_name": "col1",
|
|
|
|
"verbose_name": None,
|
|
|
|
"is_dttm": False,
|
|
|
|
"is_active": True,
|
|
|
|
"type": "TEXT",
|
|
|
|
"groupby": False,
|
|
|
|
"filterable": False,
|
|
|
|
"expression": None,
|
|
|
|
"description": None,
|
|
|
|
"python_date_format": None,
|
|
|
|
"extra": None,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"database_uuid": database.uuid,
|
|
|
|
"data": "https://some-external-url.com/data.csv",
|
|
|
|
}
|
|
|
|
|
|
|
|
# the Marshmallow schema should convert strings to objects
|
|
|
|
schema = ImportV1DatasetSchema()
|
|
|
|
dataset_config = schema.load(yaml_config)
|
|
|
|
dataset_config["database_id"] = database.id
|
2024-02-13 12:20:15 -05:00
|
|
|
_ = import_dataset(dataset_config, force_data=True)
|
|
|
|
assert [("value1",), ("value2",)] == db.session.execute(
|
2023-02-06 11:17:08 -05:00
|
|
|
"SELECT * FROM my_table"
|
|
|
|
).fetchall()
|
|
|
|
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
def test_import_dataset_managed_externally(
|
|
|
|
mocker: MockFixture,
|
|
|
|
session: Session,
|
|
|
|
) -> None:
|
2022-03-24 13:41:22 -04:00
|
|
|
"""
|
|
|
|
Test importing a dataset that is managed externally.
|
|
|
|
"""
|
2023-03-15 11:31:09 -04:00
|
|
|
from superset import security_manager
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dataset.importers.v1.utils import import_dataset
|
2023-02-06 11:17:08 -05:00
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
2022-03-24 13:41:22 -04:00
|
|
|
from superset.models.core import Database
|
|
|
|
from tests.integration_tests.fixtures.importexport import dataset_config
|
|
|
|
|
2023-03-15 11:31:09 -04:00
|
|
|
mocker.patch.object(security_manager, "can_access", return_value=True)
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
engine = db.session.get_bind()
|
2022-03-24 13:41:22 -04:00
|
|
|
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
|
|
|
|
|
|
|
|
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://")
|
2024-02-13 12:20:15 -05:00
|
|
|
db.session.add(database)
|
|
|
|
db.session.flush()
|
2022-03-24 13:41:22 -04:00
|
|
|
|
|
|
|
config = copy.deepcopy(dataset_config)
|
|
|
|
config["is_managed_externally"] = True
|
|
|
|
config["external_url"] = "https://example.org/my_table"
|
|
|
|
config["database_id"] = database.id
|
|
|
|
|
2024-02-13 12:20:15 -05:00
|
|
|
sqla_table = import_dataset(config)
|
2022-03-24 13:41:22 -04:00
|
|
|
assert sqla_table.is_managed_externally is True
|
|
|
|
assert sqla_table.external_url == "https://example.org/my_table"
|
2023-02-06 11:17:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"allowed_urls, data_uri, expected, exception_class",
|
|
|
|
[
|
|
|
|
([r".*"], "https://some-url/data.csv", True, None),
|
|
|
|
(
|
|
|
|
[r"^https://.+\.domain1\.com\/?.*", r"^https://.+\.domain2\.com\/?.*"],
|
|
|
|
"https://host1.domain1.com/data.csv",
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[r"^https://.+\.domain1\.com\/?.*", r"^https://.+\.domain2\.com\/?.*"],
|
|
|
|
"https://host2.domain1.com/data.csv",
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[r"^https://.+\.domain1\.com\/?.*", r"^https://.+\.domain2\.com\/?.*"],
|
|
|
|
"https://host1.domain2.com/data.csv",
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[r"^https://.+\.domain1\.com\/?.*", r"^https://.+\.domain2\.com\/?.*"],
|
|
|
|
"https://host1.domain3.com/data.csv",
|
|
|
|
False,
|
|
|
|
DatasetForbiddenDataURI,
|
|
|
|
),
|
|
|
|
([], "https://host1.domain3.com/data.csv", False, DatasetForbiddenDataURI),
|
|
|
|
(["*"], "https://host1.domain3.com/data.csv", False, re.error),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_validate_data_uri(allowed_urls, data_uri, expected, exception_class):
|
|
|
|
current_app.config["DATASET_IMPORT_ALLOWED_DATA_URLS"] = allowed_urls
|
|
|
|
if expected:
|
|
|
|
validate_data_uri(data_uri)
|
|
|
|
else:
|
|
|
|
with pytest.raises(exception_class):
|
|
|
|
validate_data_uri(data_uri)
|