Skip to content

Commit

Permalink
Check query tests for custom granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Sep 20, 2024
1 parent 89f3a74 commit 6bdc926
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions tests_metricflow/integration/test_cases/itest_granularity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,155 @@ integration_test:
GROUP BY 2
) b
ON a.listing__ds__day = b.listing__ds__day
---
integration_test:
name: simple_metric_with_custom_granularity
description: A simple metric queried with a custom granularity
model: SIMPLE_MODEL
metrics: [ "bookings"]
group_bys: ["booking__ds__martian_day"]
check_query: |
SELECT
b.martian_day AS booking__ds__martian_day
, SUM(a.bookings) AS bookings
FROM (
SELECT
DATE_TRUNC('day', ds) AS booking__ds__day
, 1 AS bookings
FROM {{ source_schema }}.fct_bookings
) a
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine b
ON a.booking__ds__day = b.ds
GROUP BY b.martian_day
---
integration_test:
name: simple_metric_with_custom_granularity_and_join
description: A simple metric queried with a custom granularity
model: SIMPLE_MODEL
metrics: [ "bookings"]
group_bys: ["listing__ds__martian_day"]
check_query: |
SELECT
c.martian_day AS listing__ds__martian_day
, SUM(b.bookings) AS bookings
FROM (
SELECT
DATE_TRUNC('day', l.created_at) AS listing__ds__day
, 1 AS bookings
FROM {{ source_schema }}.fct_bookings a
LEFT OUTER JOIN {{ source_schema }}.dim_listings_latest l
ON a.listing_id = l.listing_id
) b
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine c
ON b.listing__ds__day = c.ds
GROUP BY c.martian_day
---
integration_test:
name: cumulative_metric_with_custom_granularity
description: A cumulative metric queried with a custom granularity
model: SIMPLE_MODEL
metrics: [ "trailing_2_months_revenue"]
group_bys: ["metric_time__martian_day"]
check_query: |
SELECT
metric_time__martian_day
, trailing_2_months_revenue
FROM (
SELECT
metric_time__martian_day
, AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue
FROM (
SELECT
subq_5.martian_day AS metric_time__martian_day
, ts.ds AS metric_time__day
, SUM(r.revenue) AS txn_revenue
FROM {{ source_schema }}.mf_time_spine ts
INNER JOIN {{ source_schema }}.fct_revenue r
ON (DATE_TRUNC('day', r.created_at) <= ts.ds) AND (DATE_TRUNC('day', r.created_at) > ts.ds - INTERVAL 2 month)
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_5
ON ts.ds = subq_5.ds
GROUP BY subq_5.martian_day, ts.ds
) ts
) subq_9
GROUP BY metric_time__martian_day, trailing_2_months_revenue
---
integration_test:
name: derived_offset_metric_with_custom_granularity
description: A derived metric queried with a custom granularity
model: SIMPLE_MODEL
metrics: [ "booking_fees_last_week_per_booker_this_week"]
group_bys: ["metric_time__martian_day"]
check_query: |
SELECT
metric_time__martian_day
, booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week
FROM (
SELECT
COALESCE(subq_8.metric_time__martian_day, subq_14.metric_time__martian_day) AS metric_time__martian_day
, MAX(subq_8.booking_value) AS booking_value
, MAX(subq_14.bookers) AS bookers
FROM (
SELECT
subq_5.martian_day AS metric_time__martian_day
, SUM(subq_4.booking_value) AS booking_value
FROM (
SELECT
ts.ds AS metric_time__day
, b.booking_value AS booking_value
FROM {{ source_schema }}.mf_time_spine ts
INNER JOIN {{ source_schema }}.fct_bookings b
ON ts.ds - INTERVAL 1 week = DATE_TRUNC('day', b.ds)
) subq_4
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_5
ON subq_4.metric_time__day = subq_5.ds
GROUP BY subq_5.martian_day
) subq_8
FULL OUTER JOIN (
SELECT
subq_11.martian_day AS metric_time__martian_day
, COUNT(DISTINCT b.guest_id) AS bookers
FROM {{ source_schema }}.fct_bookings b
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_11
ON DATE_TRUNC('day', b.ds) = subq_11.ds
GROUP BY subq_11.martian_day
) subq_14
ON subq_8.metric_time__martian_day = subq_14.metric_time__martian_day
GROUP BY COALESCE(subq_8.metric_time__martian_day, subq_14.metric_time__martian_day)
) subq_15
---
integration_test:
name: multiple_metrics_with_custom_granularity
description: A derived metric queried with a custom granularity
model: SIMPLE_MODEL
metrics: [ "bookings", "listings"]
group_bys: ["metric_time__martian_day", "listing__ds__month"]
check_query: |
SELECT
COALESCE(subq_10.metric_time__martian_day, subq_16.metric_time__martian_day) AS metric_time__martian_day
, COALESCE(subq_10.listing__ds__month, subq_16.listing__ds__month) AS listing__ds__month
, MAX(subq_10.bookings) AS bookings
, MAX(subq_16.listings) AS listings
FROM (
SELECT
ts.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', l.created_at) AS listing__ds__month
, SUM(1) AS bookings
FROM {{ source_schema }}.fct_bookings b
LEFT OUTER JOIN {{ source_schema }}.dim_listings_latest l ON b.listing_id = l.listing_id
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON DATE_TRUNC('day', b.ds) = ts.ds
GROUP BY ts.martian_day, DATE_TRUNC('month', l.created_at)
) subq_10
FULL OUTER JOIN (
SELECT
ts.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', l.created_at) AS listing__ds__month
, SUM(1) AS listings
FROM {{ source_schema }}.dim_listings_latest l
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON DATE_TRUNC('day', l.created_at) = ts.ds
GROUP BY ts.martian_day, DATE_TRUNC('month', l.created_at)
) subq_16
ON (subq_10.metric_time__martian_day = subq_16.metric_time__martian_day)
AND (subq_10.listing__ds__month = subq_16.listing__ds__month)
GROUP BY
COALESCE(subq_10.metric_time__martian_day, subq_16.metric_time__martian_day)
, COALESCE(subq_10.listing__ds__month, subq_16.listing__ds__month)

0 comments on commit 6bdc926

Please sign in to comment.