From 74e8c60f995542ce0ac4aff3331a6ccddfc33e71 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Fri, 28 Jun 2024 08:43:02 -0700 Subject: [PATCH] Set `default_granularity` in `SetDefaultGranularityRule` (#301) ### Description Actually set the `default_granularity` during transformation using `SetDefaultGranularityRule`. This was an oversight from a previous PR. I added a test this time to ensure it works. ### Checklist - [x] I have read [the contributing guide](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md) and understand what's expected of me - [x] I have signed the [CLA](https://docs.getdbt.com/docs/contributor-license-agreements) - [x] This PR includes tests, or tests are not required/relevant for this PR - [ ] I have run `changie new` to [create a changelog entry](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md#adding-a-changelog-entry) --- .../transformations/default_granularity.py | 2 ++ pyproject.toml | 2 +- .../test_configurable_transform_rules.py | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/dbt_semantic_interfaces/transformations/default_granularity.py b/dbt_semantic_interfaces/transformations/default_granularity.py index 6e5f9af6..0e6b75fd 100644 --- a/dbt_semantic_interfaces/transformations/default_granularity.py +++ b/dbt_semantic_interfaces/transformations/default_granularity.py @@ -45,4 +45,6 @@ def transform_model(semantic_manifest: PydanticSemanticManifest) -> PydanticSema ): default_granularity = dimension.type_params.time_granularity + metric.default_granularity = default_granularity + return semantic_manifest diff --git a/pyproject.toml b/pyproject.toml index 30394519..76903e1d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "dbt-semantic-interfaces" -version = "0.6.2.dev1" +version = "0.6.2.dev2" description = 'The shared semantic layer definitions that dbt-core and MetricFlow use' readme = "README.md" requires-python = ">=3.8" diff --git a/tests/transformations/test_configurable_transform_rules.py b/tests/transformations/test_configurable_transform_rules.py index 573d0d71..bdd5fefc 100644 --- a/tests/transformations/test_configurable_transform_rules.py +++ b/tests/transformations/test_configurable_transform_rules.py @@ -1,12 +1,18 @@ +from typing import Dict + from dbt_semantic_interfaces.implementations.semantic_manifest import ( PydanticSemanticManifest, ) +from dbt_semantic_interfaces.transformations.default_granularity import ( + SetDefaultGranularityRule, +) from dbt_semantic_interfaces.transformations.semantic_manifest_transformer import ( PydanticSemanticManifestTransformer, ) from dbt_semantic_interfaces.transformations.transform_rule import ( SemanticManifestTransformRule, ) +from dbt_semantic_interfaces.type_enums import TimeGranularity class SliceNamesRule(SemanticManifestTransformRule): @@ -32,3 +38,30 @@ def test_can_configure_model_transform_rules( # noqa: D rules = [SliceNamesRule()] transformed_model = PydanticSemanticManifestTransformer.transform(pre_model, ordered_rule_sequences=(rules,)) assert all(len(x.name) == 3 for x in transformed_model.semantic_models) + + +def test_set_default_granularity_rule( # noqa: D + simple_semantic_manifest__with_primary_transforms: PydanticSemanticManifest, +) -> None: + pre_model = simple_semantic_manifest__with_primary_transforms + + metric_exists_without_default_granularity = False + configured_default_granularities: Dict[str, TimeGranularity] = {} + for metric in pre_model.metrics: + if metric.default_granularity: + configured_default_granularities[metric.name] = metric.default_granularity + metric_exists_without_default_granularity = True + + assert ( + pre_model.metrics and metric_exists_without_default_granularity + ), "If there are no metrics without a configured default_granularity, this tests nothing." + + rules = [SetDefaultGranularityRule()] + transformed_model = PydanticSemanticManifestTransformer.transform(pre_model, ordered_rule_sequences=(rules,)) + + for metric in transformed_model.metrics: + assert metric.default_granularity, f"No default_granularity set in transformation for metric '{metric.name}'" + if metric.name in configured_default_granularities: + assert ( + metric.default_granularity == configured_default_granularities[metric.name] + ), f"Default granularity was unexpected changed during transformation for metric '{metric.name}"