-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from GeneralMills/feature/save-test-results
Feature/save test results
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,14 @@ | ||
{%- macro get_full_model_name(node_id) -%} | ||
{%- set node_list = [] -%} | ||
{%- if node_id.split('.')[0] == 'model' -%} | ||
{%- set node_list = graph.nodes.values() | selectattr("resource_type", "equalto", "model") -%} | ||
{%- elif node_id.split('.')[0] == 'source' -%} | ||
{% set node_list = graph.sources.values() -%} | ||
{%- endif -%} | ||
|
||
{%- for node in node_list -%} | ||
{%- if node.unique_id == node_id -%} | ||
`{{ node.database }}.{{ node.schema }}.{{ node.name }}` | ||
{%- endif -%} | ||
{%- endfor -%} | ||
{%- endmacro -%} |
This file contains 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,56 @@ | ||
{% macro save_test_results(results) %} | ||
|
||
{%- set test_results = [] -%} | ||
|
||
{%- for result in results -%} | ||
{%- if result.node.resource_type == 'test' -%} | ||
{%- do test_results.append(result) -%} | ||
{%- endif -%} | ||
{%- endfor -%} | ||
|
||
{%- set results_tbl -%} | ||
`{{ target.database }}.{{ gmi_common_dbt_utils.generate_schema_name('processed') }}.dbt_test_results` | ||
{%- endset -%} | ||
|
||
{{ log('Centralizing test data in ' + results_tbl, info = true) if execute }} | ||
|
||
create table if not exists {{ results_tbl }} ( | ||
test_id string, | ||
test_name string, | ||
project_name string, | ||
target_db string, | ||
dbt_run_env string, | ||
test_severity string, | ||
test_result string, | ||
test_models string, | ||
execution_time_seconds string, | ||
dbt_cloud_run_id string, | ||
create_update_ts timestamp | ||
) | ||
cluster by dbt_cloud_run_id | ||
; | ||
|
||
{% if test_results|length > 0 %} | ||
insert into {{ results_tbl }} ( | ||
{% for result in test_results %} | ||
select | ||
'{{ result.node.unique_id }}' as test_id, | ||
'{{ result.node.name }}' as test_name, | ||
'{{ project_name }}' as project_name, | ||
'{{ target.database }}' as target_db, | ||
'{{ env_var("DBT_RUN_ENV") }}' as dbt_run_env, | ||
'{{ result.node.config.severity }}' as test_severity, | ||
'{{ result.status }}' as test_result, | ||
'{% for node_id in result.node.depends_on.nodes -%} | ||
{{ gmi_common_dbt_utils.get_full_model_name(node_id) }} | ||
{%- if not loop.last -%},{%- endif -%} | ||
{%- endfor %}' as test_models, | ||
'{{ result.execution_time }}' as execution_time_seconds, | ||
'{{ env_var("DBT_CLOUD_RUN_ID", invocation_id) }}' as dbt_cloud_run_id, | ||
current_timestamp() as create_update_ts | ||
{{ 'union all' if not loop.last }} | ||
{% endfor %} | ||
); | ||
{% endif %} | ||
|
||
{% endmacro %} |