Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Seems like these unfortunately need to be duplicated for now. DBT assumes they are under tests/generic
  • Loading branch information
aalan3 committed May 30, 2024
1 parent f6656b4 commit 5e5b2ba
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
3 changes: 1 addition & 2 deletions daily_spellbook/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
20 changes: 20 additions & 0 deletions daily_spellbook/tests/generic/check_seed.sql
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -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 %}
27 changes: 27 additions & 0 deletions daily_spellbook/tests/generic/equal_rowcount_with_sources.sql
Original file line number Diff line number Diff line change
@@ -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 %}
13 changes: 13 additions & 0 deletions daily_spellbook/tests/generic/is_unique_filtered.sql
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 5e5b2ba

Please sign in to comment.