Skip to content

Commit

Permalink
Add type enum NodeType and use it for declaring node type in `_vali…
Browse files Browse the repository at this point in the history
…date_top_level_objects`
  • Loading branch information
QMalcolm committed Oct 23, 2023
1 parent 2e3dda0 commit 3fd724f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions dbt_semantic_interfaces/type_enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dbt_semantic_interfaces.type_enums.dimension_type import DimensionType # noqa:F401
from dbt_semantic_interfaces.type_enums.entity_type import EntityType # noqa:F401
from dbt_semantic_interfaces.type_enums.metric_type import MetricType # noqa:F401
from dbt_semantic_interfaces.type_enums.node_type import NodeType # noqa:F401
from dbt_semantic_interfaces.type_enums.time_granularity import ( # noqa:F401
TimeGranularity,
)
9 changes: 9 additions & 0 deletions dbt_semantic_interfaces/type_enums/node_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dbt_semantic_interfaces.enum_extension import ExtendedEnum


class NodeType(ExtendedEnum):
"""Currently supported node types."""

METRIC = "metric"
SAVED_QUERY = "saved_query"
SEMANTIC_MODEL = "semantic_model"
16 changes: 10 additions & 6 deletions dbt_semantic_interfaces/validations/unique_valid_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ElementReference,
SemanticModelElementReference,
)
from dbt_semantic_interfaces.type_enums import EntityType, TimeGranularity
from dbt_semantic_interfaces.type_enums import EntityType, NodeType, TimeGranularity
from dbt_semantic_interfaces.validations.validator_helpers import (
FileContext,
SemanticManifestValidationRule,
Expand Down Expand Up @@ -170,7 +170,7 @@ def _validate_semantic_model_elements(semantic_model: SemanticModel) -> List[Val
@validate_safely(whats_being_done="checking top level elements of a specific type have unique and valid names")
def _validate_top_level_objects_of_type(
objects: Union[List[SemanticModel], List[Metric], List[SavedQuery]],
object_type: str,
object_type: NodeType,
) -> List[ValidationIssue]:
"""Validates uniqeness and validaty of top level objects of singular type."""
issues: List[ValidationIssue] = []
Expand All @@ -180,7 +180,7 @@ def _validate_top_level_objects_of_type(
context = ValidationIssueContext(
file_context=FileContext.from_metadata(object.metadata),
object_name=object.name,
object_type=object_type,
object_type=object_type.value,
)
issues += UniqueAndValidNameRule.check_valid_name(name=object.name, context=context)
if object.name in object_names:
Expand All @@ -203,14 +203,18 @@ def _validate_top_level_objects(semantic_manifest: SemanticManifest) -> List[Val

issues.extend(
UniqueAndValidNameRule._validate_top_level_objects_of_type(
semantic_manifest.semantic_models, "semantic model"
semantic_manifest.semantic_models, NodeType.SEMANTIC_MODEL
)
)

issues.extend(UniqueAndValidNameRule._validate_top_level_objects_of_type(semantic_manifest.metrics, "metric"))
issues.extend(
UniqueAndValidNameRule._validate_top_level_objects_of_type(semantic_manifest.metrics, NodeType.METRIC)
)

issues.extend(
UniqueAndValidNameRule._validate_top_level_objects_of_type(semantic_manifest.saved_queries, "saved query")
UniqueAndValidNameRule._validate_top_level_objects_of_type(
semantic_manifest.saved_queries, NodeType.SAVED_QUERY
)
)

return issues
Expand Down
12 changes: 7 additions & 5 deletions tests/validations/test_unique_valid_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PydanticSemanticManifest,
)
from dbt_semantic_interfaces.test_utils import find_semantic_model_with
from dbt_semantic_interfaces.type_enums import NodeType
from dbt_semantic_interfaces.validations.semantic_manifest_validator import (
SemanticManifestValidator,
)
Expand Down Expand Up @@ -61,8 +62,8 @@ def test_duplicate_semantic_model_name( # noqa: D
model.semantic_models.append(duplicated_semantic_model)
with pytest.raises(
SemanticManifestValidationException,
match=rf"Can't use name `{duplicated_semantic_model.name}` for a semantic model when it was already used for "
"another semantic model",
match=rf"Can't use name `{duplicated_semantic_model.name}` for a {NodeType.SEMANTIC_MODEL} when it was "
f"already used for another {NodeType.SEMANTIC_MODEL}",
):
SemanticManifestValidator[PydanticSemanticManifest](
[UniqueAndValidNameRule[PydanticSemanticManifest]()]
Expand Down Expand Up @@ -98,7 +99,8 @@ def test_duplicate_metric_name( # noqa:D
model.metrics.append(duplicated_metric)
with pytest.raises(
SemanticManifestValidationException,
match=rf"Can't use name `{duplicated_metric.name}` for a metric when it was already used for another metric",
match=rf"Can't use name `{duplicated_metric.name}` for a {NodeType.METRIC} when it was already used for "
f"another {NodeType.METRIC}",
):
SemanticManifestValidator[PydanticSemanticManifest]([UniqueAndValidNameRule()]).checked_validations(model)

Expand Down Expand Up @@ -146,8 +148,8 @@ def test_duplicate_saved_query_name( # noqa: D
manifest.saved_queries.append(duplicated_saved_query)
with pytest.raises(
SemanticManifestValidationException,
match=rf"Can't use name `{duplicated_saved_query.name}` for a saved query when it was already used for "
"another saved query",
match=rf"Can't use name `{duplicated_saved_query.name}` for a {NodeType.SAVED_QUERY} when it was already used "
f"for another {NodeType.SAVED_QUERY}",
):
SemanticManifestValidator[PydanticSemanticManifest](
[UniqueAndValidNameRule[PydanticSemanticManifest]()]
Expand Down

0 comments on commit 3fd724f

Please sign in to comment.