Skip to content

Commit

Permalink
Update tests to include more detail and use filters that work with so…
Browse files Browse the repository at this point in the history
…urce data
  • Loading branch information
courtneyholcomb committed Nov 15, 2024
1 parent 90c4794 commit 2d5361f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,22 @@ metric:
alias: bookings_at_start_of_month
---
metric:
name: bookings_offset_to_grain_twice
description: tests a nested offset_to_grain metric
name: bookings_offset_to_grain_twice_with_tiered_filters
description: tests a nested offset_to_grain metric with filters at each level
type: derived
type_params:
expr: bookings_qtd * 2
expr: bookings_this_month_wtd - bookings
metrics:
- name: bookings
- name: bookings_growth_since_start_of_month
offset_to_grain: quarter
alias: bookings_qtd
filter: "{{ TimeDimension('metric_time', 'month') }} >= '2020-01-01'"
offset_to_grain: week
alias: bookings_this_month_wtd
filter:
- "{{ TimeDimension('booking__ds', 'year') }} >= '2019-01-01'"
- "{{ TimeDimension('listing__created_at', 'day') }} >= '2020-01-02'"
filter:
- "{{ TimeDimension('metric_time', 'month') }} >= '2020-01-01'"
- "{{ Dimension('booking__is_instant') }}"
---
metric:
name: "bookings_month_start_compared_to_1_month_prior"
Expand Down Expand Up @@ -741,10 +747,10 @@ metric:
- name: bookings_offset_once
offset_window: 1 month
filter:
- "{{ TimeDimension('metric_time', 'month') }} >= '2019-01-01'"
- "{{ TimeDimension('metric_time', 'month') }} >= '2019-12-01'"
- "{{ Dimension('booking__is_instant') }}"
filter:
- "{{ TimeDimension('metric_time', 'year') }} >= '2020-01-01'"
- "{{ TimeDimension('metric_time', 'year') }} >= '2019-01-01'"
- "{{ Entity('listing') }} IS NOT NULL"
---
metric:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def test_derived_metric_that_defines_the_same_alias_in_different_components(


@pytest.mark.sql_engine_snapshot
def test_nested_offset_metric_with_tiered_filters(
def test_nested_offset_window_metric_with_tiered_filters(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
Expand All @@ -843,9 +843,9 @@ def test_nested_offset_metric_with_tiered_filters(
group_by_names=("metric_time__day",),
where_constraints=[
# `booking_ds` is the agg_time_dimension
PydanticWhereFilter(where_sql_template=("{{ TimeDimension('booking__ds', 'quarter') }} = '2021-01-01'")),
PydanticWhereFilter(where_sql_template=("{{ TimeDimension('booking__ds', 'quarter') }} >= '2020-01-01'")),
PydanticWhereFilter(
where_sql_template=("{{ TimeDimension('listing__created_at', 'day') }} = '2021-01-01'")
where_sql_template=("{{ TimeDimension('listing__created_at', 'day') }} = '2020-01-01'")
),
],
).query_spec
Expand All @@ -872,10 +872,11 @@ def test_nested_offset_to_grain_metric_with_tiered_filters(
"""Tests that filters at different tiers are applied appropriately for derived metrics with offset to grain."""
# TODO: test with Trino, hard-coded filters might fail
query_spec = query_parser.parse_and_validate_query(
metric_names=("bookings_offset_to_grain_twice",),
metric_names=("bookings_offset_to_grain_twice_with_tiered_filters",),
group_by_names=("metric_time__day",),
where_constraints=[
PydanticWhereFilter(where_sql_template=("{{ Entity('listing') }} IS NOT NULL")),
PydanticWhereFilter(where_sql_template=("{{ TimeDimension('metric_time', 'quarter') }} >= '2020-01-01'")),
],
).query_spec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_name: test_nested_offset_metric_with_tiered_filters
test_name: test_nested_offset_window_metric_with_tiered_filters
test_filename: test_derived_metric_rendering.py
docstring:
Tests that filters at different tiers are applied appropriately for derived metrics.
Expand All @@ -23,11 +23,16 @@ FROM (
FROM (
-- Time Spine
SELECT
subq_16.ds AS metric_time__day
DATE_TRUNC('quarter', subq_16.ds) AS booking__ds__quarter
, subq_16.ds AS metric_time__day
, DATE_TRUNC('year', subq_16.ds) AS metric_time__year
FROM ***************************.mf_time_spine subq_16
) subq_17
WHERE metric_time__year >= '2020-01-01'
WHERE (
booking__ds__quarter >= '2020-01-01'
) AND (
metric_time__year >= '2019-01-01'
)
) subq_15
INNER JOIN (
-- Compute Metrics via Expressions
Expand Down Expand Up @@ -365,7 +370,7 @@ FROM (
, DATE_TRUNC('month', subq_3.ds) AS metric_time__month
FROM ***************************.mf_time_spine subq_3
) subq_4
WHERE metric_time__month >= '2019-01-01'
WHERE metric_time__month >= '2019-12-01'
) subq_2
INNER JOIN (
-- Metric Time Dimension 'ds'
Expand Down Expand Up @@ -706,7 +711,7 @@ FROM (
ON
subq_5.listing = subq_8.listing
) subq_9
WHERE (((booking__ds__quarter = '2021-01-01') AND (listing__created_at__day = '2021-01-01')) AND (listing IS NOT NULL)) AND (booking__is_instant)
WHERE ((listing__created_at__day = '2020-01-01') AND (listing IS NOT NULL)) AND (booking__is_instant)
) subq_10
) subq_11
GROUP BY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_name: test_nested_offset_metric_with_tiered_filters
test_name: test_nested_offset_window_metric_with_tiered_filters
test_filename: test_derived_metric_rendering.py
docstring:
Tests that filters at different tiers are applied appropriately for derived metrics.
Expand All @@ -23,11 +23,16 @@ FROM (
FROM (
-- Time Spine
SELECT
ds AS metric_time__day
DATE_TRUNC('quarter', ds) AS booking__ds__quarter
, ds AS metric_time__day
, DATE_TRUNC('year', ds) AS metric_time__year
FROM ***************************.mf_time_spine subq_35
) subq_36
WHERE metric_time__year >= '2020-01-01'
WHERE (
booking__ds__quarter >= '2020-01-01'
) AND (
metric_time__year >= '2019-01-01'
)
) subq_34
INNER JOIN (
-- Compute Metrics via Expressions
Expand All @@ -47,15 +52,13 @@ FROM (
SELECT
DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day
, subq_24.metric_time__day AS metric_time__day
, subq_24.booking__ds__quarter AS booking__ds__quarter
, subq_24.listing AS listing
, subq_24.booking__is_instant AS booking__is_instant
, subq_24.bookings AS bookings
FROM (
-- Join to Time Spine Dataset
SELECT
subq_21.metric_time__day AS metric_time__day
, subq_20.booking__ds__quarter AS booking__ds__quarter
, subq_20.listing AS listing
, subq_20.booking__is_instant AS booking__is_instant
, subq_20.bookings AS bookings
Expand All @@ -70,14 +73,13 @@ FROM (
, DATE_TRUNC('month', ds) AS metric_time__month
FROM ***************************.mf_time_spine subq_22
) subq_23
WHERE metric_time__month >= '2019-01-01'
WHERE metric_time__month >= '2019-12-01'
) subq_21
INNER JOIN (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
SELECT
DATE_TRUNC('quarter', ds) AS booking__ds__quarter
, DATE_TRUNC('day', ds) AS metric_time__day
DATE_TRUNC('day', ds) AS metric_time__day
, listing_id AS listing
, is_instant AS booking__is_instant
, 1 AS bookings
Expand All @@ -91,7 +93,7 @@ FROM (
ON
subq_24.listing = listings_latest_src_28000.listing_id
) subq_28
WHERE (((booking__ds__quarter = '2021-01-01') AND (listing__created_at__day = '2021-01-01')) AND (listing IS NOT NULL)) AND (booking__is_instant)
WHERE ((listing__created_at__day = '2020-01-01') AND (listing IS NOT NULL)) AND (booking__is_instant)
GROUP BY
metric_time__day
) subq_32
Expand Down

0 comments on commit 2d5361f

Please sign in to comment.