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