mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
feat: semantic layer extension (#37815)
This commit is contained in:
2321
tests/unit_tests/semantic_layers/api_test.py
Normal file
2321
tests/unit_tests/semantic_layers/api_test.py
Normal file
File diff suppressed because it is too large
Load Diff
85
tests/unit_tests/semantic_layers/dao_test.py
Normal file
85
tests/unit_tests/semantic_layers/dao_test.py
Normal file
@@ -0,0 +1,85 @@
|
||||
# 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.
|
||||
|
||||
"""Tests for SemanticViewDAO."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from collections.abc import Iterator
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def session_with_semantic_view(session: Session) -> Iterator[Session]:
|
||||
"""Create an in-memory DB with a SemanticLayer and one SemanticView."""
|
||||
from superset.semantic_layers.models import SemanticLayer, SemanticView
|
||||
|
||||
engine = session.get_bind()
|
||||
SemanticView.metadata.create_all(engine) # pylint: disable=no-member
|
||||
|
||||
layer = SemanticLayer(
|
||||
uuid=uuid.uuid4(),
|
||||
name="test_layer",
|
||||
type="test",
|
||||
configuration="{}",
|
||||
)
|
||||
session.add(layer)
|
||||
session.flush()
|
||||
|
||||
view = SemanticView(
|
||||
id=1,
|
||||
uuid=uuid.uuid4(),
|
||||
name="test_view",
|
||||
semantic_layer_uuid=layer.uuid,
|
||||
configuration="{}",
|
||||
)
|
||||
session.add(view)
|
||||
session.flush()
|
||||
|
||||
return session
|
||||
|
||||
|
||||
def test_find_by_id_uses_integer_id_column(
|
||||
session_with_semantic_view: Session,
|
||||
) -> None:
|
||||
"""
|
||||
SemanticViewDAO.find_by_id must look up by the integer ``id`` column, not
|
||||
by ``uuid``.
|
||||
|
||||
Regression test: SemanticViewDAO previously set ``id_column_name = "uuid"``,
|
||||
which caused find_by_id(pk) to filter on the UUID column using an integer
|
||||
value, always returning None and making every PUT request return 404.
|
||||
"""
|
||||
from superset.daos.semantic_layer import SemanticViewDAO
|
||||
from superset.semantic_layers.models import SemanticView
|
||||
|
||||
view = session_with_semantic_view.query(SemanticView).one()
|
||||
|
||||
# Sanity check: the view has an auto-assigned integer id
|
||||
assert isinstance(view.id, int)
|
||||
|
||||
result = SemanticViewDAO.find_by_id(view.id)
|
||||
|
||||
assert result is not None, (
|
||||
"find_by_id returned None for a valid integer id — "
|
||||
"id_column_name is likely set to 'uuid' instead of 'id'"
|
||||
)
|
||||
assert result.id == view.id
|
||||
assert result.name == "test_view"
|
||||
103
tests/unit_tests/semantic_layers/decorators_test.py
Normal file
103
tests/unit_tests/semantic_layers/decorators_test.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# 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 __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_semantic_layer_stub_raises() -> None:
|
||||
"""The stub decorator raises NotImplementedError before initialization."""
|
||||
import importlib
|
||||
|
||||
import superset_core.semantic_layers.decorators as mod
|
||||
|
||||
# Reload to get the original stub (injection may have replaced it)
|
||||
importlib.reload(mod)
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
mod.semantic_layer(id="test", name="Test")
|
||||
|
||||
|
||||
def test_inject_semantic_layer_host_context() -> None:
|
||||
"""The injected decorator registers a class in host context."""
|
||||
from superset.core.api.core_api_injection import (
|
||||
inject_semantic_layer_implementations,
|
||||
)
|
||||
from superset.semantic_layers.registry import registry
|
||||
|
||||
# Clear registry for test isolation
|
||||
registry.clear()
|
||||
|
||||
inject_semantic_layer_implementations()
|
||||
|
||||
import superset_core.semantic_layers.decorators as mod
|
||||
|
||||
# Host context: no extension context active, so no prefix
|
||||
with patch(
|
||||
"superset.extensions.context.get_current_extension_context",
|
||||
return_value=None,
|
||||
):
|
||||
|
||||
@mod.semantic_layer(id="test_layer", name="Test Layer", description="A test")
|
||||
class FakeLayer:
|
||||
pass
|
||||
|
||||
assert "test_layer" in registry
|
||||
assert registry["test_layer"] is FakeLayer
|
||||
assert FakeLayer.name == "Test Layer" # type: ignore[attr-defined]
|
||||
assert FakeLayer.description == "A test" # type: ignore[attr-defined]
|
||||
|
||||
# Cleanup
|
||||
registry.pop("test_layer", None)
|
||||
|
||||
|
||||
def test_inject_semantic_layer_extension_context() -> None:
|
||||
"""The injected decorator prefixes ID in extension context."""
|
||||
from superset.core.api.core_api_injection import (
|
||||
inject_semantic_layer_implementations,
|
||||
)
|
||||
from superset.semantic_layers.registry import registry
|
||||
|
||||
registry.clear()
|
||||
|
||||
mock_context = MagicMock()
|
||||
mock_context.manifest.publisher = "acme"
|
||||
mock_context.manifest.name = "analytics"
|
||||
|
||||
inject_semantic_layer_implementations()
|
||||
|
||||
import superset_core.semantic_layers.decorators as mod
|
||||
|
||||
# Extension context is checked at decorator call time via module lookup
|
||||
with patch(
|
||||
"superset.extensions.context.get_current_extension_context",
|
||||
return_value=mock_context,
|
||||
):
|
||||
|
||||
@mod.semantic_layer(id="ext_layer", name="Extension Layer")
|
||||
class ExtLayer:
|
||||
pass
|
||||
|
||||
expected_id = "extensions.acme.analytics.ext_layer"
|
||||
assert expected_id in registry
|
||||
assert registry[expected_id] is ExtLayer
|
||||
|
||||
# Cleanup
|
||||
registry.pop(expected_id, None)
|
||||
52
tests/unit_tests/semantic_layers/labels_test.py
Normal file
52
tests/unit_tests/semantic_layers/labels_test.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# 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 patch
|
||||
|
||||
from superset.semantic_layers import labels
|
||||
|
||||
|
||||
def test_labels_feature_flag_off() -> None:
|
||||
with patch(
|
||||
"superset.feature_flag_manager.is_feature_enabled",
|
||||
return_value=False,
|
||||
):
|
||||
assert labels.dataset_label() == "Dataset"
|
||||
assert labels.dataset_label_lower() == "dataset"
|
||||
assert labels.datasets_label() == "Datasets"
|
||||
assert labels.datasets_label_lower() == "datasets"
|
||||
assert labels.database_label() == "Database"
|
||||
assert labels.database_label_lower() == "database"
|
||||
assert labels.databases_label() == "Databases"
|
||||
assert labels.databases_label_lower() == "databases"
|
||||
assert labels.database_connections_menu_label() == "Database Connections"
|
||||
|
||||
|
||||
def test_labels_feature_flag_on() -> None:
|
||||
with patch(
|
||||
"superset.feature_flag_manager.is_feature_enabled",
|
||||
return_value=True,
|
||||
):
|
||||
assert labels.dataset_label() == "Datasource"
|
||||
assert labels.dataset_label_lower() == "datasource"
|
||||
assert labels.datasets_label() == "Datasources"
|
||||
assert labels.datasets_label_lower() == "datasources"
|
||||
assert labels.database_label() == "Data connection"
|
||||
assert labels.database_label_lower() == "data connection"
|
||||
assert labels.databases_label() == "Data connections"
|
||||
assert labels.databases_label_lower() == "data connections"
|
||||
assert labels.database_connections_menu_label() == "Data Connections"
|
||||
2743
tests/unit_tests/semantic_layers/mapper_test.py
Normal file
2743
tests/unit_tests/semantic_layers/mapper_test.py
Normal file
File diff suppressed because it is too large
Load Diff
1267
tests/unit_tests/semantic_layers/models_test.py
Normal file
1267
tests/unit_tests/semantic_layers/models_test.py
Normal file
File diff suppressed because it is too large
Load Diff
296
tests/unit_tests/semantic_layers/schemas_test.py
Normal file
296
tests/unit_tests/semantic_layers/schemas_test.py
Normal file
@@ -0,0 +1,296 @@
|
||||
# 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.
|
||||
|
||||
import pytest
|
||||
from marshmallow import ValidationError
|
||||
|
||||
from superset.semantic_layers.schemas import (
|
||||
SemanticLayerPostSchema,
|
||||
SemanticLayerPutSchema,
|
||||
SemanticViewPostSchema,
|
||||
SemanticViewPutSchema,
|
||||
)
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_both_fields() -> None:
|
||||
"""Test loading both description and cache_timeout."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({"description": "A description", "cache_timeout": 300})
|
||||
assert result == {"description": "A description", "cache_timeout": 300}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_description_only() -> None:
|
||||
"""Test loading with only description."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({"description": "Just a description"})
|
||||
assert result == {"description": "Just a description"}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_cache_timeout_only() -> None:
|
||||
"""Test loading with only cache_timeout."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({"cache_timeout": 600})
|
||||
assert result == {"cache_timeout": 600}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_empty() -> None:
|
||||
"""Test loading empty payload."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({})
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_null_description() -> None:
|
||||
"""Test that description accepts None."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({"description": None})
|
||||
assert result == {"description": None}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_null_cache_timeout() -> None:
|
||||
"""Test that cache_timeout accepts None."""
|
||||
schema = SemanticViewPutSchema()
|
||||
result = schema.load({"cache_timeout": None})
|
||||
assert result == {"cache_timeout": None}
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_invalid_cache_timeout() -> None:
|
||||
"""Test that non-integer cache_timeout raises ValidationError."""
|
||||
schema = SemanticViewPutSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"cache_timeout": "not_a_number"})
|
||||
assert "cache_timeout" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_semantic_view_put_schema_unknown_field() -> None:
|
||||
"""Test that unknown fields raise ValidationError."""
|
||||
schema = SemanticViewPutSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"unknown_field": "value"})
|
||||
assert "unknown_field" in exc_info.value.messages
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# SemanticLayerPostSchema tests
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def test_post_schema_all_fields() -> None:
|
||||
"""Test loading all fields."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "My Layer",
|
||||
"description": "A layer",
|
||||
"type": "snowflake",
|
||||
"configuration": {"account": "test"},
|
||||
"cache_timeout": 300,
|
||||
}
|
||||
)
|
||||
assert result["name"] == "My Layer"
|
||||
assert result["type"] == "snowflake"
|
||||
assert result["configuration"] == {"account": "test"}
|
||||
assert result["cache_timeout"] == 300
|
||||
|
||||
|
||||
def test_post_schema_required_fields_only() -> None:
|
||||
"""Test loading with only required fields."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "My Layer",
|
||||
"type": "snowflake",
|
||||
"configuration": {"account": "test"},
|
||||
}
|
||||
)
|
||||
assert result["name"] == "My Layer"
|
||||
assert "description" not in result
|
||||
assert "cache_timeout" not in result
|
||||
|
||||
|
||||
def test_post_schema_missing_name() -> None:
|
||||
"""Test that missing name raises ValidationError."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"type": "snowflake", "configuration": {}})
|
||||
assert "name" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_post_schema_missing_type() -> None:
|
||||
"""Test that missing type raises ValidationError."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"name": "My Layer", "configuration": {}})
|
||||
assert "type" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_post_schema_missing_configuration() -> None:
|
||||
"""Test that missing configuration raises ValidationError."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"name": "My Layer", "type": "snowflake"})
|
||||
assert "configuration" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_post_schema_null_description() -> None:
|
||||
"""Test that description accepts None."""
|
||||
schema = SemanticLayerPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "My Layer",
|
||||
"type": "snowflake",
|
||||
"configuration": {},
|
||||
"description": None,
|
||||
}
|
||||
)
|
||||
assert result["description"] is None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# SemanticLayerPutSchema tests
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def test_put_schema_all_fields() -> None:
|
||||
"""Test loading all fields."""
|
||||
schema = SemanticLayerPutSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "Updated",
|
||||
"description": "New desc",
|
||||
"configuration": {"account": "new"},
|
||||
"cache_timeout": 600,
|
||||
}
|
||||
)
|
||||
assert result["name"] == "Updated"
|
||||
assert result["configuration"] == {"account": "new"}
|
||||
|
||||
|
||||
def test_put_schema_empty() -> None:
|
||||
"""Test loading empty payload."""
|
||||
schema = SemanticLayerPutSchema()
|
||||
result = schema.load({})
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_put_schema_name_only() -> None:
|
||||
"""Test loading with only name."""
|
||||
schema = SemanticLayerPutSchema()
|
||||
result = schema.load({"name": "New Name"})
|
||||
assert result == {"name": "New Name"}
|
||||
|
||||
|
||||
def test_put_schema_configuration_only() -> None:
|
||||
"""Test loading with only configuration."""
|
||||
schema = SemanticLayerPutSchema()
|
||||
result = schema.load({"configuration": {"key": "value"}})
|
||||
assert result == {"configuration": {"key": "value"}}
|
||||
|
||||
|
||||
def test_put_schema_unknown_field() -> None:
|
||||
"""Test that unknown fields raise ValidationError."""
|
||||
schema = SemanticLayerPutSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"unknown_field": "value"})
|
||||
assert "unknown_field" in exc_info.value.messages
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# SemanticViewPostSchema tests
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_all_fields() -> None:
|
||||
"""Test loading all SemanticViewPostSchema fields."""
|
||||
schema = SemanticViewPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"configuration": {"table": "orders"},
|
||||
"description": "View over orders",
|
||||
"cache_timeout": 120,
|
||||
}
|
||||
)
|
||||
assert result == {
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"configuration": {"table": "orders"},
|
||||
"description": "View over orders",
|
||||
"cache_timeout": 120,
|
||||
}
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_required_fields_with_default_configuration() -> None:
|
||||
"""Test required fields and configuration load_default behavior."""
|
||||
schema = SemanticViewPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
}
|
||||
)
|
||||
assert result == {
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"configuration": {},
|
||||
}
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_missing_name() -> None:
|
||||
"""Test missing name validation."""
|
||||
schema = SemanticViewPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"semantic_layer_uuid": "abc", "configuration": {}})
|
||||
assert "name" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_missing_semantic_layer_uuid() -> None:
|
||||
"""Test missing semantic_layer_uuid validation."""
|
||||
schema = SemanticViewPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load({"name": "Orders View", "configuration": {}})
|
||||
assert "semantic_layer_uuid" in exc_info.value.messages
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_null_optional_fields() -> None:
|
||||
"""Test optional nullable fields accept None."""
|
||||
schema = SemanticViewPostSchema()
|
||||
result = schema.load(
|
||||
{
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"description": None,
|
||||
"cache_timeout": None,
|
||||
}
|
||||
)
|
||||
assert result["description"] is None
|
||||
assert result["cache_timeout"] is None
|
||||
|
||||
|
||||
def test_semantic_view_post_schema_unknown_field() -> None:
|
||||
"""Test unknown field validation for SemanticViewPostSchema."""
|
||||
schema = SemanticViewPostSchema()
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load(
|
||||
{
|
||||
"name": "Orders View",
|
||||
"semantic_layer_uuid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"unknown_field": "value",
|
||||
}
|
||||
)
|
||||
assert "unknown_field" in exc_info.value.messages
|
||||
Reference in New Issue
Block a user