From 07c382f899f63b09a350f52a69983bd98308040a Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Mon, 23 Oct 2023 10:20:13 -0700 Subject: [PATCH] Add type enum `NodeType` and use it for declaring node type in `_validate_top_level_objects` --- dbt_semantic_interfaces/type_enums/__init__.py | 1 + dbt_semantic_interfaces/type_enums/node_type.py | 9 +++++++++ .../validations/unique_valid_name.py | 16 ++++++++++------ tests/validations/test_unique_valid_name.py | 12 +++++++----- 4 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 dbt_semantic_interfaces/type_enums/node_type.py diff --git a/dbt_semantic_interfaces/type_enums/__init__.py b/dbt_semantic_interfaces/type_enums/__init__.py index 20e0221a..954feee4 100644 --- a/dbt_semantic_interfaces/type_enums/__init__.py +++ b/dbt_semantic_interfaces/type_enums/__init__.py @@ -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, ) diff --git a/dbt_semantic_interfaces/type_enums/node_type.py b/dbt_semantic_interfaces/type_enums/node_type.py new file mode 100644 index 00000000..62de03e4 --- /dev/null +++ b/dbt_semantic_interfaces/type_enums/node_type.py @@ -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" diff --git a/dbt_semantic_interfaces/validations/unique_valid_name.py b/dbt_semantic_interfaces/validations/unique_valid_name.py index 6b321ae0..c2c98de9 100644 --- a/dbt_semantic_interfaces/validations/unique_valid_name.py +++ b/dbt_semantic_interfaces/validations/unique_valid_name.py @@ -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, @@ -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] = [] @@ -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: @@ -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 diff --git a/tests/validations/test_unique_valid_name.py b/tests/validations/test_unique_valid_name.py index 121d949d..12545e9d 100644 --- a/tests/validations/test_unique_valid_name.py +++ b/tests/validations/test_unique_valid_name.py @@ -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, ) @@ -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]()] @@ -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) @@ -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]()]