From 5e5b2ba4a1a0ae150e6f6bca36678f1f693aa09b Mon Sep 17 00:00:00 2001 From: Alan Ghobadi Date: Thu, 30 May 2024 17:34:36 +0200 Subject: [PATCH] Fix tests Seems like these unfortunately need to be duplicated for now. DBT assumes they are under tests/generic --- daily_spellbook/dbt_project.yml | 3 +-- daily_spellbook/tests/generic/check_seed.sql | 20 ++++++++++++++ .../compare_column_values_to_seed_values.sql | 22 +++++++++++++++ .../generic/equal_rowcount_with_sources.sql | 27 +++++++++++++++++++ .../tests/generic/is_unique_filtered.sql | 13 +++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 daily_spellbook/tests/generic/check_seed.sql create mode 100644 daily_spellbook/tests/generic/compare_column_values_to_seed_values.sql create mode 100644 daily_spellbook/tests/generic/equal_rowcount_with_sources.sql create mode 100644 daily_spellbook/tests/generic/is_unique_filtered.sql diff --git a/daily_spellbook/dbt_project.yml b/daily_spellbook/dbt_project.yml index f7c9e57eee9..a9e98710080 100644 --- a/daily_spellbook/dbt_project.yml +++ b/daily_spellbook/dbt_project.yml @@ -23,8 +23,7 @@ vars: model-paths: ["models", "../sources"] analysis-paths: ["analyses"] # ../tests/* should be added to a separate shared folder -# TODO: adding just ../tests causes a bunch of warnings so remove this once we figure out better granularity -test-paths: ["tests", "../tests"] # "../tests/generic", "../tests/macros", "../tests/integration-tests", "../tests/transfers"] +test-paths: ["tests"] seed-paths: ["seeds"] macro-paths: ["../macros"] snapshot-paths: ["snapshots"] diff --git a/daily_spellbook/tests/generic/check_seed.sql b/daily_spellbook/tests/generic/check_seed.sql new file mode 100644 index 00000000000..b1af4ffb53e --- /dev/null +++ b/daily_spellbook/tests/generic/check_seed.sql @@ -0,0 +1,20 @@ +-- this tests checks a model for every row in a seed file. +-- you need to specify the matching columns and the columns to check for equality. +-- filter: dictionary filter of column:value that is applied to the seed file +-- actual implementation in macros/test-helpers/check_seed.sql +{% test check_seed(model, seed_file, match_columns=[], check_columns=[], filter=None) %} + {# + --jinja comment + -- potential dynamic approach, but requires db access -- ci setup to allow in future? + -- {%- set unique_columns = config.get('unique_key') -%} + -- {%- set seed_check_columns = dbt_utils.get_filtered_columns_in_relation(from=seed_file, except=unique_columns) -%} + -- {%- set seed_matching_columns = dbt_utils.get_filtered_columns_in_relation(from=seed_file, except=seed_check_columns) -%} + --jinja comment + #} + {{ config(severity = 'error') }} + {%- set seed_check_columns = check_columns -%} + {%- set seed_matching_columns = match_columns -%} + {%- set seed = seed_file -%} + {{ check_seed_macro(model,seed,seed_matching_columns,seed_check_columns,filter) }} + +{% endtest %} diff --git a/daily_spellbook/tests/generic/compare_column_values_to_seed_values.sql b/daily_spellbook/tests/generic/compare_column_values_to_seed_values.sql new file mode 100644 index 00000000000..1073925bf62 --- /dev/null +++ b/daily_spellbook/tests/generic/compare_column_values_to_seed_values.sql @@ -0,0 +1,22 @@ +{% test compare_column_values_to_seed_values(model, column_name, seed_file_location) %} + + with unit_test as + ( + select + seed.test_description, + case + when m.{{ column_name }} = seed.{{ column_name }} + then True + else False + end as generic_column_test + from {{ model }} m + join {{ seed_file_location }} seed + on m.tx_hash = seed.tx_hash + and m.block_number = seed.block_number + ) + + select test_description + from unit_test + where generic_column_test = False + +{% endtest %} diff --git a/daily_spellbook/tests/generic/equal_rowcount_with_sources.sql b/daily_spellbook/tests/generic/equal_rowcount_with_sources.sql new file mode 100644 index 00000000000..71be9489536 --- /dev/null +++ b/daily_spellbook/tests/generic/equal_rowcount_with_sources.sql @@ -0,0 +1,27 @@ +{% test equal_rowcount_with_sources(model, evt_sources=[]) %} + + WITH + model_count as ( + select count(*) as count_a from {{ model }} + ) + ,sources_count as ( + select sum(count_b) as count_b + from ( + {% for source in evt_sources %} + select count(*) as count_b + from {{ source }} + where evt_block_time <= (select max(block_time) from {{ model }}) + {% if not loop.last %} UNION ALL {% endif %} + {% endfor %} + ) b + ) + + ,unit_test as ( + select count_a, count_b, abs(count_a - count_b) as diff_count + from model_count + full outer join sources_count + on 1=1 + ) + + select * from unit_test where diff_count > 0 +{% endtest %} diff --git a/daily_spellbook/tests/generic/is_unique_filtered.sql b/daily_spellbook/tests/generic/is_unique_filtered.sql new file mode 100644 index 00000000000..da19556226c --- /dev/null +++ b/daily_spellbook/tests/generic/is_unique_filtered.sql @@ -0,0 +1,13 @@ +{% test is_unique_filtered(model, column_name) %} + +select + {{ column_name }} as unique_field, + count(*) as n_records + +from {{ model }} +where {{ column_name }} is not null + and block_date >= NOW() - interval '2' day +group by {{ column_name }} +having count(*) > 1 + +{% endtest %}