Skip to content

Commit

Permalink
Merge branch 'EqualExperts:master' into quoted-column-identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhwdeca authored Aug 4, 2023
2 parents 6d76b45 + c997099 commit 05d0e5b
Show file tree
Hide file tree
Showing 26 changed files with 303 additions and 64 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
1 change: 1 addition & 0 deletions integration-tests/models/model_b_references_subpackage.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from {{ dbt_unit_testing.ref('dbt_unit_testing_sub_package', 'sub_package_model_a') }} where a >=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5.0") %}
select * from {{ ref('model_with_version') }} where a > 0
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5.0") %}
select * from {{ ref('model_with_version', v=1) }} where a > 1
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% if dbt_unit_testing.version_bigger_or_equal_to("1.5.0") %}
select * from {{ ref('model_with_version', version=2) }} where a > 2
{% endif %}

1 change: 1 addition & 0 deletions integration-tests/models/model_with_version_v1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as a
1 change: 1 addition & 0 deletions integration-tests/models/model_with_version_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 2 as a
8 changes: 8 additions & 0 deletions integration-tests/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2

models:
- name: model_with_version

versions:
- v: 1
- v: 2
6 changes: 3 additions & 3 deletions integration-tests/tests/unit/mock_input_styles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}}

{% call dbt_unit_testing.test('model_b_references_a', 'csv input') %}
{% call dbt_unit_testing.mock_ref ('model_a',{"input_format": "CSV"}) %}
{% call dbt_unit_testing.mock_ref ('model_a', options={"input_format": "CSV"}) %}
a,b
0,'a'
1,'b'
Expand All @@ -19,7 +19,7 @@
UNION ALL

{% call dbt_unit_testing.test('model_b_references_a', 'csv input with type cast on columns') %}
{% call dbt_unit_testing.mock_ref ('model_a',{"input_format": "csv"}) %}
{% call dbt_unit_testing.mock_ref ('model_a', options={"input_format": "csv"}) %}
a::numeric,b
0,'a'
1,'b'
Expand All @@ -33,7 +33,7 @@ UNION ALL
UNION ALL

{% call dbt_unit_testing.test('model_b_references_a', 'csv input with different separator') %}
{% call dbt_unit_testing.mock_ref ('model_a',{"input_format": "csv","column_separator": "|"}) %}
{% call dbt_unit_testing.mock_ref ('model_a', options={"input_format": "csv","column_separator": "|"}) %}
a | b
0 | 'a'
1 | 'b'
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/unit/model_31.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
UNION ALL

{% call dbt_unit_testing.test('model_31', "unless we need to fetch missing columns") %}
{% call dbt_unit_testing.mock_ref ('model_21', {"include_missing_columns": true}) %}
{% call dbt_unit_testing.mock_ref ('model_21', options={"include_missing_columns": true}) %}
select 1 as id
{% endcall %}
{% call dbt_unit_testing.expect() %}
Expand Down
17 changes: 17 additions & 0 deletions integration-tests/tests/unit/model_b_references_subpackage.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{
config(
tags=['unit-test', 'bigquery', 'snowflake', 'postgres']
)
}}

{% call dbt_unit_testing.test('model_b_references_subpackage', 'sample test') %}
{% call dbt_unit_testing.mock_ref ('dbt_unit_testing_sub_package', 'sub_package_model_a') %}
select 0 as a, 'a' as b
UNION ALL
select 1 as a, 'b' as b
{% endcall %}
{% call dbt_unit_testing.expect() %}
select 1 as a, 'b' as b
{% endcall %}
{% endcall %}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
UNION ALL

{% call dbt_unit_testing.test('model_f_references_a_non_nullable_field', 'sample test passes if we include extra columns') %}
{% call dbt_unit_testing.mock_ref ('model_a', {"include_missing_columns": true}) %}
{% call dbt_unit_testing.mock_ref ('model_a', options={"include_missing_columns": true}) %}
select 0 as a
UNION ALL
select 1 as a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{
config(
tags=['unit-test', 'bigquery', 'snowflake', 'postgres', 'versioned', '1.5.4']
)
}}

{% call dbt_unit_testing.test('model_references_model_with_version', 'latest version') %}
{% call dbt_unit_testing.mock_ref ('model_with_version') %}
select 0 as a
UNION ALL
select 1234 as a
{% endcall %}
{% call dbt_unit_testing.expect() %}
select 1234 as a
{% endcall %}
{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('model_references_model_with_version_1', 'version 1') %}
{% call dbt_unit_testing.mock_ref ('model_with_version', v=1) %}
select 1 as a
UNION ALL
select 1234 as a
{% endcall %}
{% call dbt_unit_testing.expect() %}
select 1234 as a
{% endcall %}
{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('model_references_model_with_version_2', 'version 2') %}
{% call dbt_unit_testing.mock_ref ('model_with_version', version=2) %}
select 2 as a
UNION ALL
select 1234 as a
{% endcall %}
{% call dbt_unit_testing.expect() %}
select 1234 as a
{% endcall %}
{% endcall %}

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', 'version 1', 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 %}
23 changes: 20 additions & 3 deletions macros/mock_builders.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{% macro mock_ref(model_name, options={}) %}
{% macro mock_ref(project_or_package, model_name, options={}) %}
{% if model_name is mapping %}
{% set options = model_name %}
{% set model_name = project_or_package %}
{% set project_or_package = model.package_name %}
{{ dbt_unit_testing.print_warning("Use keyword 'options' when passing options to mock_ref" ~ " (in " ~ model_name ~ ")") }}
{% else %}
{% set project_or_package, model_name = dbt_unit_testing.setup_project_and_model_name(project_or_package, model_name) %}
{% endif %}
{% if model_name is undefined %}
{{ dbt_unit_testing.raise_error('model_name must be provided for mock_ref') }}
{% endif %}
{% set node_version = kwargs["version"] | default(kwargs["v"]) | default(none) %}
{% set mock = {
"type": 'mock',
"resource_type": 'model',
"name": model_name,
"package_name": project_or_package,
"version": node_version,
"options": options,
"input_values": caller(),
}
Expand All @@ -11,8 +25,11 @@
{% endmacro %}

{% macro mock_source(source_name, table_name, options={}) %}
{% if not table_name %}
{{ dbt_unit_testing.raise_error('Table name must be provided for source') }}
{% if source_name is undefined %}
{{ dbt_unit_testing.raise_error('source_name must be provided for mock_source') }}
{% endif %}
{% if table_name is undefined %}
{{ dbt_unit_testing.raise_error('table_name must be provided for mock_source') }}
{% endif %}
{% set mock = {
"type": 'mock',
Expand Down
16 changes: 10 additions & 6 deletions macros/output.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
{% set cells = [] %}
{% for cell_value in row %}
{% set col_index = loop.index0 %}
{% set padded = dbt_unit_testing.pad(cell_value, columns_info[col_index].max_length, pad_right=cell_value is string) %}
{% if columns_info[col_index].has_differences %}
{% do cells.append("{RED}" ~ padded ~ "{RESET}") %}
{% else %}
{% do cells.append(padded) %}
{% endif %}
{% set padded = dbt_unit_testing.pad(cell_value, columns_info[col_index].max_length, pad_right=cell_value is string) %}
{% if columns_info[col_index].has_differences %}
{% do cells.append("{RED}" ~ padded ~ "{RESET}") %}
{% else %}
{% do cells.append(padded) %}
{% endif %}
{% endfor %}
{{ dbt_unit_testing.println("| " ~ cells | join(" | ") ~ " |")}}
{% endfor %}
Expand All @@ -83,3 +83,7 @@
{% macro println(s) %}
{% do log(dbt_unit_testing.parse_colors(s ~ "{RESET}"), info=true) %}
{% endmacro %}

{% macro print_warning(s) %}
{% do log(dbt_unit_testing.parse_colors("{YELLOW}" ~ s ~ "{RESET}"), info=true) %}
{% endmacro %}
20 changes: 16 additions & 4 deletions macros/overrides.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{% macro ref(model_name) %}
{% macro ref(project_or_package, model_name) %}
{% set project_or_package, model_name = dbt_unit_testing.setup_project_and_model_name(project_or_package, model_name) %}
{% if dbt_unit_testing.running_unit_test() %}
{{ return (dbt_unit_testing.ref_cte_name(model_name)) }}
{% set node_version = kwargs["version"] | default (kwargs["v"]) %}
{% set node = {"package_name": project_or_package, "name": model_name, "version": node_version} %}
{{ return (dbt_unit_testing.ref_cte_name(node)) }}
{% else %}
{{ return (builtins.ref(model_name)) }}
{{ return (builtins.ref(project_or_package, model_name, **kwargs)) }}
{% endif %}
{% endmacro %}

{% macro source(source, table_name) %}
{% if dbt_unit_testing.running_unit_test() %}
{{ return (dbt_unit_testing.source_cte_name(source, table_name)) }}
{{ return (dbt_unit_testing.source_cte_name({"source_name": source, "name": table_name})) }}
{% else %}
{{ return (builtins.source(source, table_name)) }}
{% endif %}
Expand All @@ -28,3 +31,12 @@
{% macro running_unit_test() %}
{{ return ('unit-test' in config.get('tags', {})) }}
{% endmacro %}

{% macro setup_project_and_model_name(project_or_package, model_name) %}
{% set updated_project_or_package = project_or_package if model_name is defined else model.package_name %}
{% set updated_model_name = model_name if model_name is defined else project_or_package %}
{{ return((updated_project_or_package, updated_model_name)) }}
{% endmacro %}



Loading

0 comments on commit 05d0e5b

Please sign in to comment.