Skip to content

Commit

Permalink
Actually set default_granularity during transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 28, 2024
1 parent 0d7d4fe commit 78be77f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions tests/transformations/test_configurable_transform_rules.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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}"

0 comments on commit 78be77f

Please sign in to comment.