Skip to content

Commit

Permalink
chore: add comments to explain structure of test macros
Browse files Browse the repository at this point in the history
  • Loading branch information
cverhoef committed Jan 12, 2023
1 parent 6c84c01 commit 4ba5f81
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions macros/generic_tests/test_exists.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
{# Test fails if 0 records are returned. #}
{{ config(fail_calc = 'case when count(*) = 0 then 1 else 0 end') }}

{# Query the column in INFORMATION_SCHEMA to check if it exists. #}
select *
from "INFORMATION_SCHEMA"."COLUMNS"
where "INFORMATION_SCHEMA"."COLUMNS"."TABLE_SCHEMA" = '{{ model.schema }}'
and "INFORMATION_SCHEMA"."COLUMNS"."TABLE_NAME" = '{{ model.name }}'
and "INFORMATION_SCHEMA"."COLUMNS"."COLUMN_NAME" = replace('{{ column_name }}', '"', '')

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from "INFORMATION_SCHEMA"."COLUMNS"
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_field_length.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro test_field_length(model, column_name, length) %}

{# Get columns in the relation to check if the field exists. #}
{%- set columns = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -9,10 +10,12 @@

{# Only execute test when field exists. Otherwise execute a dummy test that always succeeds. #}
{% if column_name in column_names %}
{# Query the records that fail the test. #}
select {{ column_name }}
from {{ model }}
where len({{ column_name }}) <> {{ length }}

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from {{ model }}
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_format.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% macro test_format(model, column_name, data_type) %}

{# Test fails if the difference in number of null values before and after data type casting is different. #}
{{ config(fail_calc = 'coalesce("diff_count", 0)') }}

{# Get columns in the relation to check if the field exists. #}
{%- set columns = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -11,6 +13,7 @@

{# Only execute test when field exists. Otherwise execute a dummy test that always succeeds. #}
{% if column_name in column_names %}
{# Query to get the difference in number of null values. #}
{% set query %}
select
abs(count_before_casting - count_after_casting) as "diff_count"
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_not_null.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro test_not_null(model, column_name) %}

{# Get columns in the relation to check if the field exists. #}
{%- set columns = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -9,10 +10,12 @@

{# Only execute test when field exists. Otherwise execute a dummy test that always succeeds. #}
{% if column_name in column_names %}
{# Query the records that fail the test. #}
select {{ column_name }}
from {{ model }}
where {{ column_name }} is null

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from {{ model }}
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_one_column_not_null.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro test_one_column_not_null(model, columns) %}

{# Get columns in the relation to check if the fields exist. #}
{%- set columns_in_relation = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -23,10 +24,12 @@
{% endfor %}
{% set calculation = column_list | join('\n + ') %}

{# Query the records that fail the test. #}
select *
from {{ model }}
where {{ calculation }} <> 1

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from {{ model }}
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_unique.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro test_unique(model, column_name) %}

{# Get columns in the relation to check if the field exists. #}
{%- set columns = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -9,11 +10,13 @@

{# Only execute test when field exists. Otherwise execute a dummy test that always succeeds. #}
{% if column_name in column_names %}
{# Query the records that fail the test. #}
select {{ column_name }}
from {{ model }}
group by {{ column_name }}
having count(*) > 1

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from (
Expand Down
3 changes: 3 additions & 0 deletions macros/generic_tests/test_unique_combination_of_columns.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro test_unique_combination_of_columns(model, combination_of_columns) %}

{# Get columns in the relation to check if the fields exist. #}
{%- set columns_in_relation = adapter.get_columns_in_relation(model) -%}

{%- set column_names = [] -%}
Expand All @@ -24,11 +25,13 @@

{% set columns_csv = column_list | join(', ') %}

{# Query the records that fail the test. #}
select {{ columns_csv }}
from {{ model }}
group by {{ columns_csv }}
having count(*) > 1

{# Query to get the record count when executing the test. #}
{% set query %}
select count(*) as "test_record_count"
from (
Expand Down

0 comments on commit 4ba5f81

Please sign in to comment.