Skip to content

Commit

Permalink
Add support for model versions
Browse files Browse the repository at this point in the history
  • Loading branch information
psousa50 committed Jul 30, 2023
1 parent 845a76e commit e52582b
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 19 deletions.
60 changes: 55 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Add the following to packages.yml
```yaml
packages:
- git: "https://github.com/EqualExperts/dbt-unit-testing"
revision: v0.2.9
revision: v0.3.2
```
[read the docs](https://docs.getdbt.com/docs/package-management) for more information on installing packages.
Expand Down Expand Up @@ -355,12 +355,12 @@ To be able to mock the models and sources in tests, in your dbt models you **mus
Alternatively, if you prefer to keep using the standard `ref` macro in the models, you can add these macros to your project:

```jinja
{% macro ref(model_name) %}
{{ return(dbt_unit_testing.ref(model_name)) }}
{% macro ref() %}
{{ return(dbt_unit_testing.ref(*varargs, **kwargs)) }}
{% endmacro %}
{% macro source(source, model_name) %}
{{ return(dbt_unit_testing.source(source, model_name)) }}
{% macro source() %}
{{ return(dbt_unit_testing.source(*varargs, **kwargs)) }}
{% endmacro %}
```

Expand All @@ -371,6 +371,56 @@ select {{ dbt_utils.star(builtins.ref('some_model')) }}
from {{ ref('some_model') }}
```

## Model versions

You can specify a model version on the `dbt_unit_testing.ref` macro, the same way you do on the dbt ref macro:

```jinja
{% call dbt_unit_testing.ref('some_model', version=3) %}
```

or

```jinja
{% call dbt_unit_testing.ref('some_model', v=3) %}
```

if you are overriding the ref and source macros in your project, please use the new way of doing it ([here](#requirement)). This is necessary for the version parameter to work:

```jinja
{% macro ref() %}
{{ return(dbt_unit_testing.ref(*varargs, **kwargs)) }}
{% endmacro %}
{% macro source() %}
{{ return(dbt_unit_testing.source(*varargs, **kwargs)) }}
{% endmacro %}
```

### Testing Model versions

You can test a specific model version by specifying the `version` parameter on the `dbt_unit_testing.test` macro:

```jinja
{% call dbt_unit_testing.test('some_model', 'should return 1', version=3) %}
{% call dbt_unit_testing.expect() %}
select 1
{% endcall %}
{% endcall %}
```

If `version` is not specified, the test will run against the latest version of the model.

It is also possible to mock a specific model version, again by specifying the `version` parameter on the `dbt_unit_testing.mock_ref` macro:

```jinja
{% call dbt_unit_testing.mock_ref('some_model', version=3) %}
select 1
{% endcall %}
```

If `version` is not specified, the latest version of the model will be mocked.

## Incremental models

You can write unit tests for incremental models. To enable this functionality, you should add the following code to your project:
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/macros/refs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% macro ref() %}
{{ return(dbt_unit_testing.ref(*varargs, **kwargs)) }}
{% endmacro %}

{% macro source() %}
{{ return(dbt_unit_testing.source(*varargs, **kwargs)) }}
{% endmacro %}
6 changes: 4 additions & 2 deletions integration-tests/macros/test_helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{{ dbt_unit_testing.ref_tested_model(model_name) }}
{% if execute %}
{% set mocks_and_expectations_json_str = caller() %}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_name, test_description, {}, mocks_and_expectations_json_str) %}
{% set model_node = {"package_name": model.package_name, "name": model_name} %}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_node, test_description, {}, mocks_and_expectations_json_str) %}
{% set test_report = dbt_unit_testing.build_test_report(test_configuration, test_queries) %}

{{ dbt_unit_testing.verbose("-------------------- " ~ test_configuration.model_name ~ " --------------------" ) }}
Expand All @@ -20,7 +21,8 @@
{{ dbt_unit_testing.ref_tested_model(model_name) }}
{% if execute %}
{% set mocks_and_expectations_json_str = caller() %}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_name, test_description, options, mocks_and_expectations_json_str) %}
{% set model_node = {"package_name": model.package_name, "name": model_name} %}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_node, test_description, options, mocks_and_expectations_json_str) %}

{% set model_query = test_queries["model_query"] %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
select
{{ dbt_utils.star(ref('model_in_database')) }},
{{ dbt_utils.star(source('dbt_unit_testing', 'sample_source_name')) }}
{{ dbt_utils.star(builtins.ref('model_in_database')) }},
{{ dbt_utils.star(builtins.source('dbt_unit_testing', 'sample_source_name')) }}
from {{ dbt_unit_testing.ref('model_in_database') }}
left join {{ dbt_unit_testing.source('dbt_unit_testing', 'sample_source_name') }} on false
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5") %}
select * from {{ dbt_unit_testing.ref('model_with_version') }} where a > 0
select * from {{ ref('model_with_version') }} where a > 0
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5") %}
select * from {{ dbt_unit_testing.ref('model_with_version', v=1) }} where a > 1
select * from {{ ref('model_with_version', v=1) }} where a > 1
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5") %}
select * from {{ dbt_unit_testing.ref('model_with_version', version=2) }} where a > 2
select * from {{ ref('model_with_version', version=2) }} where a > 2
{% endif %}

27 changes: 27 additions & 0 deletions integration-tests/tests/unit/model_with_version.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{
config(
tags=['unit-test', 'bigquery', 'snowflake', 'postgres', 'versioned', '1.5.4']
)
}}

{% call dbt_unit_testing.test('model_with_version', 'version1 ', version=1) %}
{% call dbt_unit_testing.expect() %}
select 1 as a
{% endcall %}
{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('model_with_version', 'version 2', v=2) %}
{% call dbt_unit_testing.expect() %}
select 2 as a
{% endcall %}
{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('model_with_version', 'latest version') %}
{% call dbt_unit_testing.expect() %}
select 2 as a
{% endcall %}
{% endcall %}
15 changes: 8 additions & 7 deletions macros/tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

{% if execute %}
{% set mocks_and_expectations_json_str = caller() %}
{% set test_configuration, test_queries = dbt_unit_testing.build_configuration_and_test_queries(model_name, test_description, options, mocks_and_expectations_json_str) %}
{% set model_version = kwargs["version"] | default(kwargs["v"]) | default(none) %}
{% set model_node = {"package_name": model.package_name, "name": model_name, "version": model_version} %}
{% 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) %}

{% if not test_report.succeeded %}
Expand All @@ -15,12 +17,12 @@
{% endif %}
{% endmacro %}

{% macro build_configuration_and_test_queries(model_name, test_description, options, mocks_and_expectations_json_str) %}
{% set node = {"package_name": model.package_name, "name": model_name} %}
{{ dbt_unit_testing.set_test_context("model_being_tested", dbt_unit_testing.ref_cte_name(node)) }}
{% macro build_configuration_and_test_queries(model_node, test_description, options, mocks_and_expectations_json_str) %}
{{ dbt_unit_testing.set_test_context("model_being_tested", dbt_unit_testing.ref_cte_name(model_node)) }}
{% set test_configuration = {
"model_name": model_name,
"model_name": model_node.model_name,
"description": test_description,
"model_node": model_node,
"options": dbt_unit_testing.merge_configs([options])}
%}
{{ dbt_unit_testing.set_test_context("options", test_configuration.options) }}
Expand Down Expand Up @@ -74,8 +76,7 @@

{% macro build_test_queries(test_configuration) %}
{% set expectations = test_configuration.expectations %}
{% set node = {"package_name": model.package_name, "name": test_configuration.model_name} %}
{% set model_node = dbt_unit_testing.model_node(node) %}
{% set model_node = dbt_unit_testing.model_node(test_configuration.model_node) %}
{%- set model_complete_sql = dbt_unit_testing.build_model_complete_sql(model_node, test_configuration.mocks, test_configuration.options) -%}
{% set columns = dbt_unit_testing.quote_and_join_columns(dbt_unit_testing.extract_columns_list(expectations.input_values)) %}

Expand Down
1 change: 1 addition & 0 deletions macros/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

{% macro model_node (node) %}
{% set graph_nodes = graph.nodes.values() |
selectattr('resource_type', 'in', ['model', 'snapshot', 'seed']) |
selectattr('package_name', 'equalto', node.package_name) |
selectattr('name', 'equalto', node.name) |
list %}
Expand Down

0 comments on commit e52582b

Please sign in to comment.