-
Notifications
You must be signed in to change notification settings - Fork 130
adding generate_unit_test_template macro
#251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dbeatty10
merged 9 commits into
dbt-labs:main
from
bruno-szdl:feature/add_generate_unit_test_template_macro
Apr 3, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
27a25e6
adding generate_unit_test_template_macro
8d04c51
added a relation exists check
83996c1
adding arg to controle inline/multiline columns
809991d
Removing duplicated `get_resource_from_unique_id` macro
dbeatty10 ee54da8
Add newline to end of file
dbeatty10 5e8ec30
Add a simple incremental model for testing
dbeatty10 bbad2e2
CI tests
dbeatty10 3a660ea
Use dispatch, etc.
dbeatty10 1425cd2
Usage documentation for `generate_unit_test_template` macro
dbeatty10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| {% macro generate_unit_test_template(model_name, inline_columns=false) %} | ||
|
|
||
| {%- set ns = namespace(depends_on_list = []) -%} | ||
|
|
||
| -- getting inputs and materialization info | ||
| {%- for node in graph.nodes.values() | ||
| | selectattr("resource_type", "equalto", "model") | ||
| | selectattr("name", "equalto", model_name) -%} | ||
| {%- set ns.depends_on_list = ns.depends_on_list + node.depends_on.nodes -%} | ||
| {%- set ns.this_materialization = node.config['materialized'] -%} | ||
| {%- endfor -%} | ||
|
|
||
| -- getting the input columns | ||
| {%- set ns.input_columns_list = [] -%} | ||
| {%- for item in ns.depends_on_list -%} | ||
| {%- set input_columns_list = [] -%} | ||
| {%- set item_dict = get_resource_from_unique_id(item) -%} | ||
| {%- if item_dict.resource_type == 'source' %} | ||
| {%- set columns = adapter.get_columns_in_relation(source(item_dict.source_name, item_dict.identifier)) -%} | ||
| {%- else -%} | ||
| {%- set columns = adapter.get_columns_in_relation(ref(item_dict.alias)) -%} | ||
| {%- endif -%} | ||
| {%- for column in columns -%} | ||
| {{ input_columns_list.append(column.name) }} | ||
| {%- endfor -%} | ||
| {{ ns.input_columns_list.append(input_columns_list) }} | ||
| {%- endfor -%} | ||
|
|
||
| -- getting 'this' columns | ||
| {% set relation_exists = load_relation(ref(model_name)) is not none %} | ||
| {% if relation_exists %} | ||
| {%- set ns.expected_columns_list = [] -%} | ||
| {%- set columns = adapter.get_columns_in_relation(ref(model_name)) -%} | ||
| {%- for column in columns -%} | ||
| {{ ns.expected_columns_list.append(column.name) }} | ||
| {%- endfor -%} | ||
| {% endif %} | ||
|
|
||
| {%- set unit_test_yaml_template -%} | ||
| unit_tests: | ||
| - name: unit_test_{{ model_name }} | ||
| model: {{ model_name }} | ||
| {% if ns.this_materialization == 'incremental' %} | ||
| overrides: | ||
| macros: | ||
| is_incremental: true | ||
| {% endif %} | ||
| given: | ||
| {%- for i in range(ns.depends_on_list|length) -%} | ||
| {%- set item_dict = get_resource_from_unique_id(ns.depends_on_list[i]) -%} | ||
| {% if item_dict.resource_type == 'source' %} | ||
| - input: source("{{item_dict.source_name}}", "{{item_dict.identifier}}") | ||
| rows: | ||
| {%- else %} | ||
| - input: ref("{{item_dict.alias}}") | ||
| rows: | ||
| {%- endif -%} | ||
| {%- if inline_columns -%} | ||
| {%- set ns.column_string = '- {' -%} | ||
| {%- for column_name in ns.input_columns_list[i] -%} | ||
| {%- if not loop.last -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': , ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': }' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- else -%} | ||
| {%- set ns.column_string = '' -%} | ||
| {%- for column_name in ns.input_columns_list[i] -%} | ||
| {%- if loop.first -%} | ||
| {%- set ns.column_string = ns.column_string ~ '- ' ~ column_name ~ ': ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ '\n ' ~ column_name ~ ': ' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- endif %} | ||
| {{ns.column_string}} | ||
| {% endfor %} | ||
|
|
||
| {%- if ns.this_materialization == 'incremental' %} | ||
| - input: this | ||
| rows: | ||
| {%- if relation_exists -%} | ||
| {%- if inline_columns -%} | ||
| {%- set ns.column_string = '- {' -%} | ||
| {%- for column_name in ns.expected_columns_list -%} | ||
| {%- if not loop.last -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': , ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': }' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- else -%} | ||
| {%- set ns.column_string = '' -%} | ||
| {%- for column_name in ns.expected_columns_list -%} | ||
| {%- if loop.first -%} | ||
| {%- set ns.column_string = ns.column_string ~ '- ' ~ column_name ~ ': ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ '\n ' ~ column_name ~ ': ' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- endif %} | ||
| {{ns.column_string}} | ||
| {%- endif %} | ||
| {% endif %} | ||
|
|
||
| expect: | ||
| rows: | ||
| {%- if relation_exists -%} | ||
| {%- if inline_columns -%} | ||
| {%- set ns.column_string = '- {' -%} | ||
| {%- for column_name in ns.expected_columns_list -%} | ||
| {%- if not loop.last -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': , ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ column_name ~ ': }' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- else -%} | ||
| {%- set ns.column_string = '' -%} | ||
| {%- for column_name in ns.expected_columns_list -%} | ||
| {%- if loop.first -%} | ||
| {%- set ns.column_string = ns.column_string ~ '- ' ~ column_name ~ ': ' -%} | ||
| {%- else -%} | ||
| {%- set ns.column_string = ns.column_string ~ '\n ' ~ column_name ~ ': ' -%} | ||
| {%- endif -%} | ||
| {% endfor %} | ||
| {%- endif %} | ||
| {{ns.column_string}} | ||
| {%- endif -%} | ||
|
|
||
| {%- endset -%} | ||
|
|
||
| {{ print(unit_test_yaml_template) }} | ||
|
|
||
| {% endmacro %} | ||
|
|
||
| {# retrieve entire resource dictionary based on unique id #} | ||
| {% macro get_resource_from_unique_id(resource_unique_id) %} | ||
| {% set resource_type = resource_unique_id.split('.')[0] %} | ||
| {% if resource_type == 'source' %} | ||
| {% set resource = graph.sources[resource_unique_id] %} | ||
| {% elif resource_type == 'exposure' %} | ||
| {% set resource = graph.exposure[resource_unique_id] %} | ||
| {% elif resource_type == 'metric' %} | ||
| {% set resource = graph.metrics[resource_unique_id] %} | ||
| {% else %} | ||
| {% set resource = graph.nodes[resource_unique_id] %} | ||
| {% endif %} | ||
| {{ return(resource) }} | ||
| {% endmacro %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.