Add test macros (#261)

This commit is contained in:
Ryan-Loofy 2023-10-18 15:03:08 -04:00 committed by GitHub
parent 512281a711
commit 424fbfcf09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

35
macros/tests/udfs.sql Normal file
View File

@ -0,0 +1,35 @@
{% macro base_test_udf(model, udf, args, assertions) %}
{#
Generates a test for a UDF.
#}
{% if execute %}
{%- set context -%}
SET LIVEQUERY_CONTEXT = '{"userId":"{{ var("STUDIO_TEST_USER_ID") }}"}';
{%- endset -%}
{%- do run_query(context) -%}
{%- endif -%}
{%- set call -%}
{{ target.database }}.{{ udf }}({{ args }})
{%- endset -%}
,
test AS
(
SELECT
'{{ udf }}' AS test_name
,[{{ args }}] as parameters
,{{ call }} AS result
)
{% for assertion in assertions %}
SELECT
test_name,
parameters,
result,
$${{ assertion }}$$ AS assertion,
$${{ context ~ "\n" }}SELECT {{ call ~ "\n" }};$$ AS sql
FROM test
WHERE NOT {{ assertion }}
{%- if not loop.last -%}
UNION ALL
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

View File

@ -0,0 +1,12 @@
{% test test_udf(model, column_name, args, assertions) %}
{#
This is a generic test for UDFs.
The udfs are deployed using ephemeral models, so we need to
use the ephemeral model name to get the udf name.
#}
{%- set schema = model | replace("__dbt__cte__", "") -%}
{%- set schema = schema.split("__") | first -%}
{%- set udf = schema ~ "." ~ column_name -%}
{{ base_test_udf(model, udf, args, assertions) }}
{% endtest %}