-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_microbatch_compiled_code.py
More file actions
152 lines (133 loc) · 4.56 KB
/
test_microbatch_compiled_code.py
File metadata and controls
152 lines (133 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from contextlib import contextmanager
import pytest
from dbt_project import DbtProject
def _microbatch_source_model_sql() -> str:
return """
{{ config(event_time='order_date') }}
{% set event_time_data_type = 'datetime2' if target.type == 'sqlserver' else 'timestamp' %}
select
1 as order_id,
1 as customer_id,
42 as amount,
cast('2024-01-01 00:00:00' as {{ event_time_data_type }}) as order_date
union all
select
2 as order_id,
2 as customer_id,
84 as amount,
cast('2025-01-01 00:00:00' as {{ event_time_data_type }}) as order_date
"""
def _microbatch_model_sql(source_model_name: str) -> str:
return """
{% set model_config = {
"materialized": "incremental",
"incremental_strategy": "microbatch",
"event_time": "order_date",
"batch_size": "year",
"begin": "2024-01-01"
} %}
{% if target.type != "duckdb" %}
{% do model_config.update({"unique_key": "order_id"}) %}
{% endif %}
{{ config(**model_config) }}
select
order_id,
customer_id,
amount,
order_date
from {{ ref('__MICROBATCH_SOURCE_MODEL__') }}
""".replace(
"__MICROBATCH_SOURCE_MODEL__", source_model_name
)
@contextmanager
def _with_microbatch_test_models(dbt_project: DbtProject, model_suffix: str):
source_model_name = f"mb_src_{model_suffix}"
target_model_name = f"mb_tgt_{model_suffix}"
source_model_path = dbt_project.tmp_models_dir_path.joinpath(
f"{source_model_name}.sql"
)
target_model_path = dbt_project.tmp_models_dir_path.joinpath(
f"{target_model_name}.sql"
)
source_model_path.write_text(_microbatch_source_model_sql())
target_model_path.write_text(_microbatch_model_sql(source_model_name))
relative_source_model_path = source_model_path.relative_to(
dbt_project.project_dir_path
)
relative_target_model_path = target_model_path.relative_to(
dbt_project.project_dir_path
)
try:
yield relative_source_model_path, relative_target_model_path, target_model_name
finally:
if source_model_path.exists():
source_model_path.unlink()
if target_model_path.exists():
target_model_path.unlink()
def _run_microbatch_model_and_get_latest_success_result(
dbt_project: DbtProject, model_suffix: str
):
with _with_microbatch_test_models(dbt_project, model_suffix) as (
source_model_path,
model_path,
target_model_name,
):
dbt_project.dbt_runner.run(select=f"{source_model_path} {model_path}")
unique_id = f"model.elementary_tests.{target_model_name}"
run_results = dbt_project.read_table(
"dbt_run_results",
where=f"unique_id = '{unique_id}' and status = 'success'",
order_by="generated_at desc",
limit=1,
)
return run_results
@contextmanager
def _with_microbatch_macro_file(dbt_project: DbtProject, macro_name: str):
macro_path = dbt_project.project_dir_path / "macros" / "microbatch.sql"
macro_sql = """
{% macro __MACRO_NAME__(arg_dict) %}
{{ return(elementary.get_incremental_microbatch_sql(arg_dict)) }}
{% endmacro %}
""".replace(
"__MACRO_NAME__", macro_name
)
if macro_path.exists():
raise FileExistsError(f"Expected no macro file at {macro_path}")
macro_path.write_text(macro_sql)
try:
yield
finally:
if macro_path.exists():
macro_path.unlink()
@pytest.mark.skip_targets(
["spark", "vertica", "bigquery", "athena", "clickhouse", "dremio"]
)
@pytest.mark.skip_for_dbt_fusion
@pytest.mark.parametrize(
"macro_name,expected_compiled_code,model_suffix",
[
("get_incremental_microbatch_sql", True, "with_override"),
("get_incremental_microbatch_sql_not_used", False, "without_override"),
],
ids=["with_override", "without_override"],
)
def test_microbatch_run_results_compiled_code_behavior(
dbt_project: DbtProject,
macro_name: str,
expected_compiled_code: bool,
model_suffix: str,
):
dbt_project.dbt_runner.vars["disable_run_results"] = False
with _with_microbatch_macro_file(dbt_project, macro_name):
run_results = _run_microbatch_model_and_get_latest_success_result(
dbt_project, model_suffix
)
assert run_results, "Expected a successful run result row for microbatch model"
if expected_compiled_code:
assert run_results[0][
"compiled_code"
], "Expected compiled_code to be populated when override macro is present"
else:
assert not run_results[0][
"compiled_code"
], "Expected compiled_code to stay empty when override macro is absent"