mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
feat: API for semantic layers
This commit is contained in:
148
tests/unit_tests/commands/semantic_layer/create_test.py
Normal file
148
tests/unit_tests/commands/semantic_layer/create_test.py
Normal file
@@ -0,0 +1,148 @@
|
||||
# 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.
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from superset.commands.semantic_layer.create import CreateSemanticLayerCommand
|
||||
from superset.commands.semantic_layer.exceptions import (
|
||||
SemanticLayerCreateFailedError,
|
||||
SemanticLayerInvalidError,
|
||||
)
|
||||
|
||||
|
||||
def test_create_semantic_layer_success(mocker: MockerFixture) -> None:
|
||||
"""Test successful creation of a semantic layer."""
|
||||
new_model = MagicMock()
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.create.SemanticLayerDAO",
|
||||
)
|
||||
dao.validate_uniqueness.return_value = True
|
||||
dao.create.return_value = new_model
|
||||
|
||||
mock_cls = MagicMock()
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.create.registry",
|
||||
{"snowflake": mock_cls},
|
||||
)
|
||||
|
||||
data = {
|
||||
"name": "My Layer",
|
||||
"type": "snowflake",
|
||||
"configuration": {"account": "test"},
|
||||
}
|
||||
result = CreateSemanticLayerCommand(data).run()
|
||||
|
||||
assert result == new_model
|
||||
dao.create.assert_called_once_with(attributes=data)
|
||||
mock_cls.from_configuration.assert_called_once_with({"account": "test"})
|
||||
|
||||
|
||||
def test_create_semantic_layer_unknown_type(mocker: MockerFixture) -> None:
|
||||
"""Test that SemanticLayerInvalidError is raised for unknown type."""
|
||||
mocker.patch(
|
||||
"superset.commands.semantic_layer.create.SemanticLayerDAO",
|
||||
)
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.create.registry",
|
||||
{},
|
||||
clear=True,
|
||||
)
|
||||
|
||||
data = {
|
||||
"name": "My Layer",
|
||||
"type": "nonexistent",
|
||||
"configuration": {},
|
||||
}
|
||||
with pytest.raises(SemanticLayerInvalidError):
|
||||
CreateSemanticLayerCommand(data).run()
|
||||
|
||||
|
||||
def test_create_semantic_layer_duplicate_name(mocker: MockerFixture) -> None:
|
||||
"""Test that SemanticLayerInvalidError is raised for duplicate names."""
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.create.SemanticLayerDAO",
|
||||
)
|
||||
dao.validate_uniqueness.return_value = False
|
||||
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.create.registry",
|
||||
{"snowflake": MagicMock()},
|
||||
)
|
||||
|
||||
data = {
|
||||
"name": "Duplicate",
|
||||
"type": "snowflake",
|
||||
"configuration": {},
|
||||
}
|
||||
with pytest.raises(SemanticLayerInvalidError):
|
||||
CreateSemanticLayerCommand(data).run()
|
||||
|
||||
|
||||
def test_create_semantic_layer_invalid_configuration(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""Test that invalid configuration is caught by the @transaction decorator."""
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.create.SemanticLayerDAO",
|
||||
)
|
||||
dao.validate_uniqueness.return_value = True
|
||||
|
||||
mock_cls = MagicMock()
|
||||
mock_cls.from_configuration.side_effect = ValueError("bad config")
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.create.registry",
|
||||
{"snowflake": mock_cls},
|
||||
)
|
||||
|
||||
data = {
|
||||
"name": "My Layer",
|
||||
"type": "snowflake",
|
||||
"configuration": {"bad": "data"},
|
||||
}
|
||||
with pytest.raises(SemanticLayerCreateFailedError):
|
||||
CreateSemanticLayerCommand(data).run()
|
||||
|
||||
|
||||
def test_create_semantic_layer_copies_data(mocker: MockerFixture) -> None:
|
||||
"""Test that the command copies input data and does not mutate it."""
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.create.SemanticLayerDAO",
|
||||
)
|
||||
dao.validate_uniqueness.return_value = True
|
||||
dao.create.return_value = MagicMock()
|
||||
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.create.registry",
|
||||
{"snowflake": MagicMock()},
|
||||
)
|
||||
|
||||
original_data = {
|
||||
"name": "Original",
|
||||
"type": "snowflake",
|
||||
"configuration": {"account": "test"},
|
||||
}
|
||||
CreateSemanticLayerCommand(original_data).run()
|
||||
|
||||
assert original_data == {
|
||||
"name": "Original",
|
||||
"type": "snowflake",
|
||||
"configuration": {"account": "test"},
|
||||
}
|
||||
50
tests/unit_tests/commands/semantic_layer/delete_test.py
Normal file
50
tests/unit_tests/commands/semantic_layer/delete_test.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# 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.
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from superset.commands.semantic_layer.delete import DeleteSemanticLayerCommand
|
||||
from superset.commands.semantic_layer.exceptions import SemanticLayerNotFoundError
|
||||
|
||||
|
||||
def test_delete_semantic_layer_success(mocker: MockerFixture) -> None:
|
||||
"""Test successful deletion of a semantic layer."""
|
||||
mock_model = MagicMock()
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.delete.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
|
||||
DeleteSemanticLayerCommand("some-uuid").run()
|
||||
|
||||
dao.find_by_uuid.assert_called_once_with("some-uuid")
|
||||
dao.delete.assert_called_once_with([mock_model])
|
||||
|
||||
|
||||
def test_delete_semantic_layer_not_found(mocker: MockerFixture) -> None:
|
||||
"""Test that SemanticLayerNotFoundError is raised when model is missing."""
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.delete.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = None
|
||||
|
||||
with pytest.raises(SemanticLayerNotFoundError):
|
||||
DeleteSemanticLayerCommand("missing-uuid").run()
|
||||
@@ -16,6 +16,12 @@
|
||||
# under the License.
|
||||
|
||||
from superset.commands.semantic_layer.exceptions import (
|
||||
SemanticLayerCreateFailedError,
|
||||
SemanticLayerDeleteFailedError,
|
||||
SemanticLayerForbiddenError,
|
||||
SemanticLayerInvalidError,
|
||||
SemanticLayerNotFoundError,
|
||||
SemanticLayerUpdateFailedError,
|
||||
SemanticViewForbiddenError,
|
||||
SemanticViewInvalidError,
|
||||
SemanticViewNotFoundError,
|
||||
@@ -46,3 +52,40 @@ def test_semantic_view_update_failed_error() -> None:
|
||||
"""Test SemanticViewUpdateFailedError has correct message."""
|
||||
error = SemanticViewUpdateFailedError()
|
||||
assert str(error.message) == "Semantic view could not be updated."
|
||||
|
||||
|
||||
def test_semantic_layer_not_found_error() -> None:
|
||||
"""Test SemanticLayerNotFoundError has correct status and message."""
|
||||
error = SemanticLayerNotFoundError()
|
||||
assert error.status == 404
|
||||
assert str(error.message) == "Semantic layer does not exist"
|
||||
|
||||
|
||||
def test_semantic_layer_forbidden_error() -> None:
|
||||
"""Test SemanticLayerForbiddenError has correct message."""
|
||||
error = SemanticLayerForbiddenError()
|
||||
assert str(error.message) == "Changing this semantic layer is forbidden"
|
||||
|
||||
|
||||
def test_semantic_layer_invalid_error() -> None:
|
||||
"""Test SemanticLayerInvalidError has correct message."""
|
||||
error = SemanticLayerInvalidError()
|
||||
assert str(error.message) == "Semantic layer parameters are invalid."
|
||||
|
||||
|
||||
def test_semantic_layer_create_failed_error() -> None:
|
||||
"""Test SemanticLayerCreateFailedError has correct message."""
|
||||
error = SemanticLayerCreateFailedError()
|
||||
assert str(error.message) == "Semantic layer could not be created."
|
||||
|
||||
|
||||
def test_semantic_layer_update_failed_error() -> None:
|
||||
"""Test SemanticLayerUpdateFailedError has correct message."""
|
||||
error = SemanticLayerUpdateFailedError()
|
||||
assert str(error.message) == "Semantic layer could not be updated."
|
||||
|
||||
|
||||
def test_semantic_layer_delete_failed_error() -> None:
|
||||
"""Test SemanticLayerDeleteFailedError has correct message."""
|
||||
error = SemanticLayerDeleteFailedError()
|
||||
assert str(error.message) == "Semantic layer could not be deleted."
|
||||
|
||||
@@ -21,10 +21,15 @@ import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from superset.commands.semantic_layer.exceptions import (
|
||||
SemanticLayerInvalidError,
|
||||
SemanticLayerNotFoundError,
|
||||
SemanticViewForbiddenError,
|
||||
SemanticViewNotFoundError,
|
||||
)
|
||||
from superset.commands.semantic_layer.update import UpdateSemanticViewCommand
|
||||
from superset.commands.semantic_layer.update import (
|
||||
UpdateSemanticLayerCommand,
|
||||
UpdateSemanticViewCommand,
|
||||
)
|
||||
from superset.exceptions import SupersetSecurityException
|
||||
|
||||
|
||||
@@ -106,6 +111,116 @@ def test_update_semantic_view_copies_data(mocker: MockerFixture) -> None:
|
||||
assert original_data == {"description": "Original"}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# UpdateSemanticLayerCommand tests
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def test_update_semantic_layer_success(mocker: MockerFixture) -> None:
|
||||
"""Test successful update of a semantic layer."""
|
||||
mock_model = MagicMock()
|
||||
mock_model.type = "snowflake"
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
dao.update.return_value = mock_model
|
||||
|
||||
data = {"name": "Updated", "description": "New desc"}
|
||||
result = UpdateSemanticLayerCommand("some-uuid", data).run()
|
||||
|
||||
assert result == mock_model
|
||||
dao.find_by_uuid.assert_called_once_with("some-uuid")
|
||||
dao.update.assert_called_once_with(mock_model, attributes=data)
|
||||
|
||||
|
||||
def test_update_semantic_layer_not_found(mocker: MockerFixture) -> None:
|
||||
"""Test that SemanticLayerNotFoundError is raised when model is missing."""
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = None
|
||||
|
||||
with pytest.raises(SemanticLayerNotFoundError):
|
||||
UpdateSemanticLayerCommand("missing-uuid", {"name": "test"}).run()
|
||||
|
||||
|
||||
def test_update_semantic_layer_duplicate_name(mocker: MockerFixture) -> None:
|
||||
"""Test that SemanticLayerInvalidError is raised for duplicate names."""
|
||||
mock_model = MagicMock()
|
||||
mock_model.type = "snowflake"
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
dao.validate_update_uniqueness.return_value = False
|
||||
|
||||
with pytest.raises(SemanticLayerInvalidError):
|
||||
UpdateSemanticLayerCommand("some-uuid", {"name": "Duplicate"}).run()
|
||||
|
||||
|
||||
def test_update_semantic_layer_validates_configuration(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""Test that configuration is validated against the plugin."""
|
||||
mock_model = MagicMock()
|
||||
mock_model.type = "snowflake"
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
dao.update.return_value = mock_model
|
||||
|
||||
mock_cls = MagicMock()
|
||||
mocker.patch.dict(
|
||||
"superset.commands.semantic_layer.update.registry",
|
||||
{"snowflake": mock_cls},
|
||||
)
|
||||
|
||||
config = {"account": "test"}
|
||||
UpdateSemanticLayerCommand("some-uuid", {"configuration": config}).run()
|
||||
|
||||
mock_cls.from_configuration.assert_called_once_with(config)
|
||||
|
||||
|
||||
def test_update_semantic_layer_skips_name_check_when_no_name(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""Test that name uniqueness is not checked when name is not provided."""
|
||||
mock_model = MagicMock()
|
||||
mock_model.type = "snowflake"
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
dao.update.return_value = mock_model
|
||||
|
||||
UpdateSemanticLayerCommand("some-uuid", {"description": "Updated"}).run()
|
||||
|
||||
dao.validate_update_uniqueness.assert_not_called()
|
||||
|
||||
|
||||
def test_update_semantic_layer_copies_data(mocker: MockerFixture) -> None:
|
||||
"""Test that the command copies input data and does not mutate it."""
|
||||
mock_model = MagicMock()
|
||||
mock_model.type = "snowflake"
|
||||
|
||||
dao = mocker.patch(
|
||||
"superset.commands.semantic_layer.update.SemanticLayerDAO",
|
||||
)
|
||||
dao.find_by_uuid.return_value = mock_model
|
||||
dao.update.return_value = mock_model
|
||||
|
||||
original_data = {"description": "Original"}
|
||||
UpdateSemanticLayerCommand("some-uuid", original_data).run()
|
||||
|
||||
assert original_data == {"description": "Original"}
|
||||
|
||||
|
||||
def _make_view_model(
|
||||
uuid: str = "view-uuid-1",
|
||||
name: str = "my_view",
|
||||
|
||||
Reference in New Issue
Block a user