Skip to content

Commit

Permalink
Add model() macro to support model object
Browse files Browse the repository at this point in the history
  • Loading branch information
psousa50 committed Apr 18, 2024
1 parent 0ca0081 commit b639244
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration-tests/models/model_with_model_object.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select '{{ dbt_unit_testing.model().resource_type }}' as resource_type
11 changes: 11 additions & 0 deletions integration-tests/tests/unit/model_with_model_object.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
tags=['unit-test', 'bigquery', 'snowflake', 'postgres']
)
}}

{% call dbt_unit_testing.test('model_with_model_object', 'sample test') %}
{% call dbt_unit_testing.expect() %}
select 'model' as resource_type
{% endcall %}
{% endcall %}
1 change: 1 addition & 0 deletions macros/tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{% set mocks_and_expectations_json_str = caller() %}
{% set model_version = kwargs["version"] | default(kwargs["v"]) | default(none) %}
{% set model_node = {"package_name": model.package_name, "name": model_name, "version": model_version} %}
{{ dbt_unit_testing.set_test_context("model_node_being_tested", model_node) }}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_node, test_description, options, mocks_and_expectations_json_str) %}
{% set test_report = dbt_unit_testing.build_test_report(test_configuration, test_queries) %}

Expand Down
11 changes: 11 additions & 0 deletions macros/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,14 @@
{{ return (dbt_unit_testing.split_and_pad_and_join(dbt_version, 5) >= dbt_unit_testing.split_and_pad_and_join(v, 5)) }}
{% endmacro %}

{%- macro model() -%}
{% if dbt_unit_testing.running_unit_test() %}
{% set model_node_being_tested = dbt_unit_testing.get_test_context("model_node_being_tested", "") %}
{% if model_node_being_tested == "" %}
{{ dbt_unit_testing.raise_error("Model node being tested not found.") }}
{% endif %}
{{ return(dbt_unit_testing.model_node(model_node_being_tested)) }}
{%- else -%}
{{ return(model) }}
{% endif %}
{%- endmacro -%}

0 comments on commit b639244

Please sign in to comment.