Skip to content

Commit

Permalink
Add tests for simple sub-daily time constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Aug 13, 2024
1 parent 9579aa6 commit 30d469d
Show file tree
Hide file tree
Showing 6 changed files with 835 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests_metricflow/integration/test_cases/itest_constraints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,37 @@ integration_test:
FROM {{ source_schema }}.fct_bookings b
LEFT OUTER JOIN {{ source_schema }}.dim_listings_latest l ON b.listing_id = l.listing_id
WHERE NOT b.is_instant AND (NOT l.is_lux OR l.is_lux IS NULL)
---
integration_test:
name: test_subdaily_time_constraint
description: Test a sub-daily time constraint on a no-metric query.
model: SIMPLE_MODEL
group_bys: ["metric_time__second"]
time_constraint: ["2020-01-01 00:00:02", "2020-01-01 00:00:08"]
check_query: |
SELECT
{{ render_date_trunc("ts", TimeGranularity.SECOND) }} AS metric_time__second
FROM {{ source_schema }}.mf_time_spine_second
WHERE {{ render_date_trunc("ts", TimeGranularity.SECOND) }} BETWEEN '2020-01-01 00:00:02' AND '2020-01-01 00:00:08'
GROUP BY {{ render_date_trunc("ts", TimeGranularity.SECOND) }}
---
integration_test:
name: test_subdaily_time_constraint_with_metric
description: Test a sub-daily time constraint on a metric query.
model: SIMPLE_MODEL
metrics: ["subdaily_join_to_time_spine_metric"]
group_bys: ["metric_time__hour"]
time_constraint: ["2020-01-01 02:00:00", "2020-01-03 05:00:00"]
check_query: |
SELECT
ts AS metric_time__hour
, archived_users AS subdaily_join_to_time_spine_metric
FROM {{ source_schema }}.mf_time_spine_hour
LEFT OUTER JOIN (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, SUM(1) AS archived_users
FROM {{ source_schema }}.dim_users
GROUP BY metric_time__hour
) subq_4 ON ts = subq_4.metric_time__hour
WHERE ts BETWEEN '2020-01-01 02:00:00' AND '2020-01-03 05:00:00'
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

from __future__ import annotations

import datetime as dt

import pytest
from _pytest.fixtures import FixtureRequest
from dbt_semantic_interfaces.references import EntityReference
from dbt_semantic_interfaces.type_enums.date_part import DatePart
from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity
from metricflow_semantics.filters.time_constraint import TimeRangeConstraint
from metricflow_semantics.specs.metric_spec import MetricSpec
from metricflow_semantics.specs.query_spec import MetricFlowQuerySpec
from metricflow_semantics.specs.time_dimension_spec import TimeDimensionSpec
Expand Down Expand Up @@ -327,3 +330,54 @@ def test_subdaily_join_to_time_spine_metric( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_time_constraint_without_metrics( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.SECOND),),
time_range_constraint=TimeRangeConstraint(
start_time=dt.datetime(2020, 1, 1, 0, 0, 2), end_time=dt.datetime(2020, 1, 1, 0, 0, 8)
),
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_time_constraint_with_metric( # 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("subdaily_join_to_time_spine_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
time_range_constraint=TimeRangeConstraint(
start_time=dt.datetime(2020, 1, 1, 2), end_time=dt.datetime(2020, 1, 1, 5)
),
)

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,
)
Loading

0 comments on commit 30d469d

Please sign in to comment.