forked from AxelThevenot/dbt-assertions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_assertions.sql
51 lines (41 loc) · 1.87 KB
/
generic_assertions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{% test generic_assertions(model, column='exceptions', exclude_list=none, include_list=none, re_assert=False) %}
{#-
Generates a test SELECT expression to get rows based on exceptions.
By default, each row with exception(s) will be returned.
You can change this behaviour specifying an exclude_list or include_list (not both).
Args:
column (optional[str]): Column to read the assertions from.
exclude_list (optional[list[str]]): Assertions to exclude in the filter.
include_list (optional[list[str]]): Assertions to include in the filter.
re_assert (optional[bool]): to set to `true` if your assertion field
is not calculated in your table.
Returns:
str: An SELECT expression to return rows with exceptions.
-#}
WITH
final AS (
SELECT
*
{%- if re_assert and execute %}
{%- set model_parts = (model | replace('`', '')).split('.') %}
{%- set database = model_parts[0] %}
{%- set schema = model_parts[1] %}
{%- set alias = model_parts[2] %}
{#- Filter the graph to find the node for the specified model -#}
{%- set node = (
graph.nodes.values()
| selectattr('resource_type', 'equalto', 'model')
| selectattr('database' , 'equalto', database)
| selectattr('schema' , 'equalto', schema)
| selectattr('alias' , 'equalto', alias)
) | first -%}
,
{{ dbt_assertions.assertions(column=column, _node=node) | indent(12) }}
{%- endif %}
FROM {{ model }}
)
SELECT
*
FROM `final`
WHERE {{ dbt_assertions.assertions_filter(column, exclude_list, include_list, reverse=true) }}
{% endtest %}