Skip to content

Commit

Permalink
Write query parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jul 9, 2024
1 parent 54d19f6 commit 906fd0a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -781,4 +781,4 @@ metric:
denominator:
name: listings
filter: "{{ Metric('views', ['listing']) }} > 10"
default_granularity: week
default_granularity: week
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from metricflow_semantics.test_helpers.example_project_configuration import (
EXAMPLE_PROJECT_CONFIGURATION_YAML_CONFIG_FILE,
)
from metricflow_semantics.test_helpers.metric_time_dimension import MTD
from metricflow_semantics.test_helpers.metric_time_dimension import MTD, MTD_SPEC_DAY, MTD_SPEC_MONTH, MTD_SPEC_YEAR
from metricflow_semantics.test_helpers.snapshot_helpers import assert_object_snapshot_equal

logger = logging.getLogger(__name__)
Expand All @@ -53,6 +53,10 @@
expr: "1"
agg: sum
create_metric: true
- name: monthly_bookings
expr: "1"
agg: sum
agg_time_dimension: ds_month
dimensions:
- name: is_instant
Expand All @@ -61,13 +65,44 @@
type: time
type_params:
time_granularity: day
- name: ds_month
type: time
type_params:
time_granularity: month
primary_entity: booking
entities:
- name: listing
type: foreign
expr: listing_id
---
metric:
name: instant_bookings
description: instant bookings
type: simple
type_params:
measure: bookings
filter: "{{ Dimension('booking__is_instant') }}"
default_granularity: year
---
metric:
name: month_bookings
description: monthly bookings
type: simple
type_params:
measure: monthly_bookings
---
metric:
name: instant_plus_months_bookings
description: instant plus month bookings
type: derived
type_params:
expr: instant_bookings + month_bookings
metrics:
- name: instant_bookings
- name: month_bookings
"""
)

Expand Down Expand Up @@ -626,3 +661,27 @@ def test_invalid_group_by_metric(bookings_query_parser: MetricFlowQueryParser) -
bookings_query_parser.parse_and_validate_query(
metric_names=("bookings",), where_constraint_str="{{ Metric('listings', ['garbage']) }} > 1"
)


def test_default_granularity(bookings_query_parser: MetricFlowQueryParser) -> None:
"""Tests different scenarios using default granularity."""
# Metric with default_granularity set
query_spec = bookings_query_parser.parse_and_validate_query(
metric_names=("instant_bookings",), group_by_names=("metric_time",)
).query_spec
assert len(query_spec.time_dimension_specs) == 1
assert query_spec.time_dimension_specs[0] == MTD_SPEC_YEAR

# Metric without default_granularity set
query_spec = bookings_query_parser.parse_and_validate_query(
metric_names=("month_bookings",), group_by_names=("metric_time",)
).query_spec
assert len(query_spec.time_dimension_specs) == 1
assert query_spec.time_dimension_specs[0] == MTD_SPEC_MONTH

# Derived metric with different agg_time_dimensions and no default granularity set
query_spec = bookings_query_parser.parse_and_validate_query(
metric_names=("instant_plus_months_bookings",), group_by_names=("metric_time",)
).query_spec
assert len(query_spec.time_dimension_specs) == 1
assert query_spec.time_dimension_specs[0] == MTD_SPEC_MONTH
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,3 @@ def test_offset_window_with_date_part( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_offset_window_with_date_part( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="bookings_growth_2_weeks"),),
time_dimension_specs=(
DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.DAY, date_part=DatePart.DOW),
),
)

render_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


# Tests to write:
# Simple metric with default granularity
# Derived metric with default granularity
# Defaults to agg_time_dimension
# Default when day isn't avail
# Default for derived metric with diff agg time grains

0 comments on commit 906fd0a

Please sign in to comment.