Skip to content

Commit

Permalink
Write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Sep 26, 2024
1 parent bf14e8a commit defaf8d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions dbt_semantic_interfaces/transformations/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def _lowercase_semantic_model_elements(semantic_model: PydanticSemanticModel) ->
if semantic_model.dimensions:
for dimension in semantic_model.dimensions:
dimension.name = dimension.name.lower()
# add time spines here?

@staticmethod
def _lowercase_top_level_objects(model: PydanticSemanticManifest) -> None:
Expand Down
92 changes: 92 additions & 0 deletions tests/validations/test_time_spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
)
from dbt_semantic_interfaces.implementations.elements.entity import PydanticEntity
from dbt_semantic_interfaces.implementations.elements.measure import PydanticMeasure
from dbt_semantic_interfaces.implementations.metric import (
PydanticMetric,
PydanticMetricInputMeasure,
PydanticMetricTypeParams,
)
from dbt_semantic_interfaces.implementations.node_relation import PydanticNodeRelation
from dbt_semantic_interfaces.implementations.project_configuration import (
PydanticProjectConfiguration,
Expand All @@ -22,6 +27,7 @@
AggregationType,
DimensionType,
EntityType,
MetricType,
TimeGranularity,
)
from dbt_semantic_interfaces.validations.semantic_manifest_validator import (
Expand Down Expand Up @@ -182,3 +188,89 @@ def test_dimension_granularity_smaller_than_time_spine() -> None: # noqa: D
"configuring a time spine at or below the smallest time dimension granularity is recommended"
in issues.warnings[0].message
)


def test_time_spines_with_invalid_names() -> None: # noqa: D
semantic_manifest = PydanticSemanticManifest(
semantic_models=[
semantic_model_with_guaranteed_meta(
name="semantic_model",
measures=[
PydanticMeasure(
name="sum_measure", agg=AggregationType.SUM, agg_time_dimension="dim", create_metric=True
)
],
dimensions=[
PydanticDimension(
name="dim",
type=DimensionType.TIME,
type_params=PydanticDimensionTypeParams(time_granularity=TimeGranularity.SECOND),
)
],
entities=[PydanticEntity(name="entity", type=EntityType.PRIMARY)],
),
],
metrics=[
PydanticMetric(
name="metric",
description=None,
type=MetricType.SIMPLE,
type_params=PydanticMetricTypeParams(measure=PydanticMetricInputMeasure(name="sum_measure")),
),
],
project_configuration=PydanticProjectConfiguration(
time_spine_table_configurations=[],
time_spines=[
PydanticTimeSpine(
node_relation=PydanticNodeRelation(alias="time_spine", schema_name="my_fav_schema"),
primary_column=PydanticTimeSpinePrimaryColumn(name="ds", time_granularity=TimeGranularity.MONTH),
custom_granularities=[
PydanticTimeSpineCustomGranularityColumn(name="retail_year"),
],
),
PydanticTimeSpine(
node_relation=PydanticNodeRelation(alias="time_spine2", schema_name="my_fav_schema"),
primary_column=PydanticTimeSpinePrimaryColumn(name="ds", time_granularity=TimeGranularity.DAY),
custom_granularities=[
PydanticTimeSpineCustomGranularityColumn(name="retail_year"),
PydanticTimeSpineCustomGranularityColumn(name="quarter"), # todo
PydanticTimeSpineCustomGranularityColumn(name="semantic_model"),
PydanticTimeSpineCustomGranularityColumn(name="sum_measure"),
PydanticTimeSpineCustomGranularityColumn(name="dim"),
PydanticTimeSpineCustomGranularityColumn(name="entity"),
PydanticTimeSpineCustomGranularityColumn(name="metric"),
PydanticTimeSpineCustomGranularityColumn(name="mYfUnCuStOmGrAnUlArItY"),
],
),
],
),
)
issues = SemanticManifestValidator[PydanticSemanticManifest]().validate_semantic_manifest(semantic_manifest)
assert len(issues.warnings) == 1
warning_msg = "To avoid unexpected query errors, configuring a time spine at or below the smallest"
assert issues.warnings[0].message.startswith(warning_msg)

assert issues.has_blocking_issues
assert len(issues.errors) == 8
error_messages = {err.message for err in issues.errors}
for msg in [
(
"Custom granularity names must be unique, but found duplicate custom granularities with the names "
"{'retail_year'}."
),
"Can't use name `semantic_model` for a custom granularity when it was already used for a semantic_model.",
"Can't use name `sum_measure` for a custom granularity when it was already used for a measure.",
"Can't use name `dim` for a custom granularity when it was already used for a dimension.",
"Can't use name `entity` for a custom granularity when it was already used for a entity.",
"Can't use name `metric` for a custom granularity when it was already used for a metric.",
(
"Invalid name `quarter` - names cannot match reserved time granularity keywords (['NANOSECOND', "
"'MICROSECOND', 'MILLISECOND', 'SECOND', 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'])"
),
(
"Invalid name `mYfUnCuStOmGrAnUlArItY` - names may only contain lower case letters, numbers, and "
"underscores. Additionally, names must start with a lower case letter, cannot end with an underscore, "
"cannot contain dunders (double underscores, or __), and must be at least 2 characters long."
),
]:
assert msg in error_messages

0 comments on commit defaf8d

Please sign in to comment.