Skip to content

Commit

Permalink
Bugfix/37 fix skip ephemeral models (#38)
Browse files Browse the repository at this point in the history
* fix: #37 fix skip ephemeral models

* updated readme file for store_test_results macro behavior

* added description for macro __get_test_model_materialization

* fix null table_name
  • Loading branch information
il-ngocanh authored Oct 21, 2024
1 parent b4d19a0 commit 94d96fa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Since the version 1.4+, all models and metrics will be enabled by default.
#### store_test_results ([source](https://github.com/infinitelambda/dq-tools/blob/main/macros/artifacts/test/store_test_results.sql))

This macro is used to parse `results` variable at the `on-run-end` context to achieve the test result nodes, and save them to the `DQ_ISSUE_LOG` table.
If the model is materialized as `ephemeral`, this macro will insert the null value for aggregated fields related to tested model.

Usage:

Expand Down
7 changes: 7 additions & 0 deletions integration_tests/models/general/test_ephemeral_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
materialized='ephemeral'
)
}}

select 1 as col
9 changes: 9 additions & 0 deletions integration_tests/models/general/test_ephemeral_model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2

models:
- name: test_ephemeral_model
columns:
- name: col
data_tests:
- unique
- not_null
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro __get_test_model_materialization(model_name) %}

{% if execute %}

{% for item in graph.nodes.values() | selectattr("name", "equalto", model_name) %}

{% set materialized = item.config.materialized %}

{{ return(materialized) }}

{% endfor %}

{% endif %}

{{ return('') }}

{% endmacro %}
11 changes: 7 additions & 4 deletions macros/artifacts/test/utilities/general/__select_test_result.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{%- set test_type = dq_tools.__get_test_type(result.node) -%}
{%- set testing_model = dq_tools.__get_test_model(result.node) -%}
{%- set testing_model_relation = dq_tools.__get_relation(testing_model) -%}
{%- set materialization = dq_tools.__get_test_model_materialization(testing_model.name) -%}
/* {{ testing_model }} */

select '{{ result.node.unique_id }}' as test_unique_id
Expand All @@ -18,22 +19,24 @@
,'{{ dq_tools.__get_kpi_categorize(result.node) }}' as test_kpi_category_config
,'{{ dq_tools.__get_dq_issue_type(result.node) }}' as dq_issue_type
,'{{ result.status }}' as test_result
,'{{ testing_model_relation }}' as table_name
,{% if materialization != 'ephemeral' %}
'{{ testing_model_relation }}'
{% else %}''{% endif %} as table_name
,'{{ dq_tools.__get_where_subquery(
testing_model,
result.node.config,
sql_escape=true) }}' as table_query
,'{{ dq_tools.__get_to_relation(result.node) }}' as ref_table
,'{{ dq_tools.__get_column_name(result.node) | escape }}' as column_name
,{% if test_type == 'generic' %}
,{% if test_type == 'generic' and materialization != 'ephemeral' %}
{{ adapter.get_columns_in_relation(testing_model_relation) | length }}
{% else %}null{% endif %} as no_of_table_columns
,'{{ dq_tools.__get_to_column_name(result.node) | escape }}' as ref_column
,{% if test_type == 'generic' %}(
,{% if test_type == 'generic' and materialization != 'ephemeral' %}(
select count(*)
from {{ testing_model_relation }}
){% else %}null{% endif %} as no_of_records
,{% if test_type == 'generic' %}(
,{% if test_type == 'generic' and materialization != 'ephemeral' %}(
select count(*)
from {{ dq_tools.__get_where_subquery(testing_model, result.node.config) }}
){% else %}null{% endif %} as no_of_records_scanned
Expand Down
6 changes: 5 additions & 1 deletion macros/artifacts/test/utilities/general/_general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ macros:
But if there is a config.where presenting it will become the subquery:
```
(select * from {database}.{schema}.{table} where {where}) as dbt_subquery
```
```
- name: __get_test_model_materialization
description: |
Return the materialization type of the model being tested (eg: table, view, ephemeral etc).

0 comments on commit 94d96fa

Please sign in to comment.