From 6c55e0b43ff606ad6b24a5526dbc96f2fce25ed2 Mon Sep 17 00:00:00 2001 From: Pedro Sousa Date: Tue, 30 May 2023 16:29:57 +0100 Subject: [PATCH] Use regular dbt 'is_incremental' and 'this', when testing incremental models --- integration-tests/models/incremental_model.sql | 4 ++-- integration-tests/tests/unit/incremental_model.sql | 2 +- macros/sql_builders.sql | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/integration-tests/models/incremental_model.sql b/integration-tests/models/incremental_model.sql index df5326c..8f3ec68 100644 --- a/integration-tests/models/incremental_model.sql +++ b/integration-tests/models/incremental_model.sql @@ -2,6 +2,6 @@ select c from {{ dbt_unit_testing.ref('model_for_incremental') }} -{% if dbt_unit_testing.is_incremental() %} - where c > (select max(c) from {{ dbt_unit_testing.this() }}) +{% if is_incremental() %} + where c > (select max(c) from {{ this }}) {% endif %} diff --git a/integration-tests/tests/unit/incremental_model.sql b/integration-tests/tests/unit/incremental_model.sql index d0b3e24..d139422 100644 --- a/integration-tests/tests/unit/incremental_model.sql +++ b/integration-tests/tests/unit/incremental_model.sql @@ -28,7 +28,7 @@ UNION ALL -{% call dbt_unit_testing.test('incremental_model', 'incremental test', options={"is_incremental": "True"}) %} +{% call dbt_unit_testing.test('incremental_model', 'incremental test', options={"run_as_incremental": "True"}) %} {% call dbt_unit_testing.mock_ref ('model_for_incremental') %} select 10 as c UNION ALL diff --git a/macros/sql_builders.sql b/macros/sql_builders.sql index 112702a..1dbc4d8 100644 --- a/macros/sql_builders.sql +++ b/macros/sql_builders.sql @@ -39,6 +39,15 @@ {{ return(model_complete_sql) }} {% endmacro %} +{% macro replace_this_with_model_name(rendered_node) %} + {% set this_name = this | string %} + {% set model_name = dbt_unit_testing.get_test_context('model_name') %} + {% if model_name is defined %} + {{ return (rendered_node.replace(this_name, model_name)) }} + {% endif %} + {{ return (rendered_node) }} +{% endmacro %} + {% macro cte_name(node) %} {% if node.resource_type in ('source') %} {{ return (dbt_unit_testing.source_cte_name(node.source_name, node.name)) }}