From 3996dda8b9fed77d64764713c4eaa81501f04191 Mon Sep 17 00:00:00 2001 From: Julio Garcia Date: Thu, 5 Sep 2024 08:00:49 +0200 Subject: [PATCH] Creating new generic tests for redshift adapter --- .../generic_test_sql/accepted_values.sql | 27 +++++++++++++++++++ .../macros/generic_test_sql/not_null.sql | 10 +++++++ .../macros/generic_test_sql/relationships.sql | 23 ++++++++++++++++ .../macros/generic_test_sql/unique.sql | 12 +++++++++ 4 files changed, 72 insertions(+) create mode 100644 dbt/include/redshift/macros/generic_test_sql/accepted_values.sql create mode 100644 dbt/include/redshift/macros/generic_test_sql/not_null.sql create mode 100644 dbt/include/redshift/macros/generic_test_sql/relationships.sql create mode 100644 dbt/include/redshift/macros/generic_test_sql/unique.sql diff --git a/dbt/include/redshift/macros/generic_test_sql/accepted_values.sql b/dbt/include/redshift/macros/generic_test_sql/accepted_values.sql new file mode 100644 index 000000000..0cf527a8f --- /dev/null +++ b/dbt/include/redshift/macros/generic_test_sql/accepted_values.sql @@ -0,0 +1,27 @@ +{% macro redshift__test_accepted_values(model, column_name, values, quote=True) %} + + with all_values as ( + + select + "{{ column_name }}" as value_field, + count(*) as n_records + + from {{ model }} + group by "{{ column_name }}" + + ) + + select * + from all_values + where value_field not in ( + {% for value in values -%} + {% if quote -%} + '{{ value }}' + {%- else -%} + {{ value }} + {%- endif -%} + {%- if not loop.last -%},{%- endif %} + {%- endfor %} + ) + +{% endmacro %} \ No newline at end of file diff --git a/dbt/include/redshift/macros/generic_test_sql/not_null.sql b/dbt/include/redshift/macros/generic_test_sql/not_null.sql new file mode 100644 index 000000000..88f2088a5 --- /dev/null +++ b/dbt/include/redshift/macros/generic_test_sql/not_null.sql @@ -0,0 +1,10 @@ +{% macro redshift__test_not_null(model, column_name) %} + +{% set column_name = '"' ~ column_name ~ '"'%} +{% set column_list = '*' if should_store_failures() else column_name %} + +select {{ column_list }} +from {{ model }} +where {{ column_name }} is null + +{% endmacro %} \ No newline at end of file diff --git a/dbt/include/redshift/macros/generic_test_sql/relationships.sql b/dbt/include/redshift/macros/generic_test_sql/relationships.sql new file mode 100644 index 000000000..a7592a01e --- /dev/null +++ b/dbt/include/redshift/macros/generic_test_sql/relationships.sql @@ -0,0 +1,23 @@ +{% macro redshift__test_relationships(model, column_name, to, field) %} + +with child as ( + select "{{ column_name }}" as from_field + from {{ model }} + where "{{ column_name }}" is not null +), + +parent as ( + select {{ field }} as to_field + from {{ to }} +) + +select + from_field + +from child +left join parent + on child.from_field = parent.to_field + +where parent.to_field is null + +{% endmacro %} \ No newline at end of file diff --git a/dbt/include/redshift/macros/generic_test_sql/unique.sql b/dbt/include/redshift/macros/generic_test_sql/unique.sql new file mode 100644 index 000000000..709b64201 --- /dev/null +++ b/dbt/include/redshift/macros/generic_test_sql/unique.sql @@ -0,0 +1,12 @@ +{% macro redshift__test_unique(model, column_name) %} + +select + "{{ column_name }}" as unique_field, + count(*) as n_records + +from {{ model }} +where "{{ column_name }}" is not null +group by "{{ column_name }}" +having count(*) > 1 + +{% endmacro %}