-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
base microbatch support + tests (#300)
- Loading branch information
1 parent
1edeb74
commit 629f22f
Showing
6 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Default microbatch strategy implementation and base tests | ||
time: 2024-09-11T13:54:04.231977-04:00 | ||
custom: | ||
Author: michelleark | ||
Issue: "302" |
91 changes: 91 additions & 0 deletions
91
dbt-tests-adapter/dbt/tests/adapter/incremental/test_incremental_microbatch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
from pprint import pformat | ||
from unittest import mock | ||
|
||
import pytest | ||
from freezegun import freeze_time | ||
|
||
from dbt.tests.util import relation_from_name, run_dbt | ||
|
||
_input_model_sql = """ | ||
{{ config(materialized='table', event_time='event_time') }} | ||
select 1 as id, TIMESTAMP '2020-01-01 00:00:00-0' as event_time | ||
union all | ||
select 2 as id, TIMESTAMP '2020-01-02 00:00:00-0' as event_time | ||
union all | ||
select 3 as id, TIMESTAMP '2020-01-03 00:00:00-0' as event_time | ||
""" | ||
|
||
_microbatch_model_sql = """ | ||
{{ config(materialized='incremental', incremental_strategy='microbatch', unique_key='id', event_time='event_time', batch_size='day') }} | ||
select * from {{ ref('input_model') }} | ||
""" | ||
|
||
|
||
class BaseMicrobatch: | ||
@pytest.fixture(scope="class") | ||
def microbatch_model_sql(self) -> str: | ||
""" | ||
This is the SQL that defines the microbatch model, including any {{ config(..) }} | ||
""" | ||
return _microbatch_model_sql | ||
|
||
@pytest.fixture(scope="class") | ||
def input_model_sql(self) -> str: | ||
""" | ||
This is the SQL that defines the input model to the microbatch model, including any {{ config(..) }}. | ||
event_time is a required configuration of this input | ||
""" | ||
return _input_model_sql | ||
|
||
@pytest.fixture(scope="class") | ||
def insert_two_rows_sql(self, project) -> str: | ||
test_schema_relation = project.adapter.Relation.create( | ||
database=project.database, schema=project.test_schema | ||
) | ||
return f"insert into {test_schema_relation}.input_model (id, event_time) values (4, TIMESTAMP '2020-01-04 00:00:00-0'), (5, TIMESTAMP '2020-01-05 00:00:00-0')" | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self, microbatch_model_sql, input_model_sql): | ||
return { | ||
"input_model.sql": input_model_sql, | ||
"microbatch_model.sql": microbatch_model_sql, | ||
} | ||
|
||
def assert_row_count(self, project, relation_name: str, expected_row_count: int): | ||
relation = relation_from_name(project.adapter, relation_name) | ||
result = project.run_sql(f"select * from {relation}", fetch="all") | ||
|
||
assert len(result) == expected_row_count, f"{relation_name}:{pformat(result)}" | ||
|
||
@mock.patch.dict(os.environ, {"DBT_EXPERIMENTAL_MICROBATCH": "True"}) | ||
def test_run_with_event_time(self, project, insert_two_rows_sql): | ||
# initial run -- backfills all data | ||
with freeze_time("2020-01-03 13:57:00"): | ||
run_dbt(["run"]) | ||
self.assert_row_count(project, "microbatch_model", 3) | ||
|
||
# our partition grain is "day" so running the same day without new data should produce the same results | ||
with freeze_time("2020-01-03 14:57:00"): | ||
run_dbt(["run"]) | ||
self.assert_row_count(project, "microbatch_model", 3) | ||
|
||
# add next two days of data | ||
project.run_sql(insert_two_rows_sql) | ||
|
||
self.assert_row_count(project, "input_model", 5) | ||
|
||
# re-run without changing current time => no insert | ||
with freeze_time("2020-01-03 14:57:00"): | ||
run_dbt(["run", "--select", "microbatch_model"]) | ||
self.assert_row_count(project, "microbatch_model", 3) | ||
|
||
# re-run by advancing time by one day changing current time => insert 1 row | ||
with freeze_time("2020-01-04 14:57:00"): | ||
run_dbt(["run", "--select", "microbatch_model"]) | ||
self.assert_row_count(project, "microbatch_model", 4) | ||
|
||
# re-run by advancing time by one more day changing current time => insert 1 more row | ||
with freeze_time("2020-01-05 14:57:00"): | ||
run_dbt(["run", "--select", "microbatch_model"]) | ||
self.assert_row_count(project, "microbatch_model", 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = "1.5.0" | ||
version = "1.6.0a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters