Skip to content

Commit

Permalink
added tests for parsing custom grains in where filters
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDee committed Nov 5, 2024
1 parent f2ce289 commit 9f29106
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/example_project_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
primary_column:
name: ds_day
time_granularity: day
- node_relation:
schema_name: stuffs
alias: week_time_spine
primary_column:
name: ds
time_granularity: week
custom_granularities:
- name: martian_week
"""
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ project_configuration:
primary_column:
name: ds_day
time_granularity: day
- node_relation:
alias: mf_time_spine
schema_name: stufffs
primary_column:
name: ds
time_granularity: day
custom_granularities:
- name: martian_day
29 changes: 29 additions & 0 deletions tests/parsing/test_saved_query_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ def test_saved_query_group_by() -> None:
)


def test_saved_query_group_by_with_custom_grain() -> None:
"""Test for parsing group_bys in a saved query."""
yaml_contents = textwrap.dedent(
"""\
saved_query:
name: test_saved_query_group_bys
query_params:
metrics:
- test_metric_a
group_by:
- TimeDimension('test_entity__metric_time', 'martian_week')
- Dimension('test_entity__metric_time__martian_week')
"""
)
file = YamlConfigFile(filepath="test_dir/inline_for_test", contents=yaml_contents)

build_result = parse_yaml_files_to_semantic_manifest(files=[file, EXAMPLE_PROJECT_CONFIGURATION_YAML_CONFIG_FILE])

assert len(build_result.semantic_manifest.saved_queries) == 1
saved_query = build_result.semantic_manifest.saved_queries[0]
assert len(saved_query.query_params.group_by) == 2
print(saved_query.query_params.group_by)
assert {
"TimeDimension('test_entity__metric_time', 'martian_week')",
"Dimension('test_entity__metric_time__martian_week')",
} == set(saved_query.query_params.group_by)


def test_saved_query_where() -> None:
"""Test for parsing where clause in a saved query."""
where = "Dimension(test_entity__test_dimension) == true"
Expand Down
16 changes: 16 additions & 0 deletions tests/parsing/test_where_filter_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ def test_dimension_date_part() -> None: # noqa
time_granularity_name=TimeGranularity.WEEK.value,
),
),
(
"{{ TimeDimension('metric_time__martian_week') }} > '2023-01-01'",
TimeDimensionCallParameterSet(
time_dimension_reference=TimeDimensionReference("metric_time"),
entity_path=(),
time_granularity_name="martian_week",
),
),
(
"{{ TimeDimension('metric_time', time_granularity_name='martian_week') }} > '2023-01-01'",
TimeDimensionCallParameterSet(
time_dimension_reference=TimeDimensionReference("metric_time"),
entity_path=(),
time_granularity_name="martian_week",
),
),
],
)
def test_time_dimension_grain( # noqa
Expand Down
2 changes: 2 additions & 0 deletions tests/validations/test_where_filters_are_parseable.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_metric_where_filter_validations_invalid_granularity( # noqa: D
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'cool') }}"),
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'month') }}"),
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'MONTH') }}"),
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'martian_day') }}"),
]
)
validator = SemanticManifestValidator[PydanticSemanticManifest]([WhereFiltersAreParseable()])
Expand Down Expand Up @@ -195,6 +196,7 @@ def test_saved_query_with_happy_filter( # noqa: D
where=PydanticWhereFilterIntersection(
where_filters=[
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'hour') }}"),
PydanticWhereFilter(where_sql_template="{{ TimeDimension('metric_time', 'martian_day') }}"),
]
),
),
Expand Down

0 comments on commit 9f29106

Please sign in to comment.