Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default_granularity in SetDefaultGranularityRule #301

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
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}"
Loading