Skip to content

Commit

Permalink
Merge pull request #183 from dbt-labs/fix/alt-multi-ref
Browse files Browse the repository at this point in the history
fix multi ref tests - alt approach
  • Loading branch information
dave-connors-3 authored Aug 15, 2022
2 parents cbc788a + e681c59 commit 3ff7f50
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
3 changes: 3 additions & 0 deletions integration_tests/models/staging/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ models:
tests:
- unique
- not_null
- relationships:
to: ref('stg_model_2')
field: id
- name: "concat(coalesce('fake_column', ' '),'for_testing')"
tests:
- not_null
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/seeds/tests/test_fct_test_coverage.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
total_models,total_tests,tested_models,test_coverage_pct,staging_test_coverage_pct,intermediate_test_coverage_pct,marts_test_coverage_pct,other_test_coverage_pct,test_to_model_ratio
14,8,4,28.57,60.00,50.00,0.00,0.00,0.5714
14,9,4,28.57,60.00,50.00,0.00,0.00,0.6429
10 changes: 6 additions & 4 deletions macros/unpack/get_relationships.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{%- macro default__get_relationships(node_type) -%}

{%- if execute -%}

{%- if node_type == 'nodes' %}
{% set nodes_list = graph.nodes.values() %}
{%- elif node_type == 'exposures' -%}
Expand All @@ -25,7 +24,8 @@
{%- set values_line =
[
"cast('" ~ node.unique_id ~ "' as " ~ dbt_utils.type_string() ~ ")",
"cast(NULL as " ~ dbt_utils.type_string() ~ ")"
"cast(NULL as " ~ dbt_utils.type_string() ~ ")",
"FALSE",
]
%}

Expand All @@ -38,7 +38,8 @@
{%- set values_line =
[
"cast('" ~ node.unique_id ~ "' as " ~ dbt_utils.type_string() ~ ")",
"cast('" ~ parent ~ "' as " ~ dbt_utils.type_string() ~ ")"
"cast('" ~ parent ~ "' as " ~ dbt_utils.type_string() ~ ")",
"" ~ loop.last ~ ""
]
%}

Expand All @@ -55,7 +56,8 @@
values = values,
columns = [
'resource_id',
'direct_parent_id'
'direct_parent_id',
'is_primary_relationship'
]
)
) }}
Expand Down
27 changes: 17 additions & 10 deletions models/marts/core/int_direct_relationships.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,42 @@ all_graph_resources as (
direct_model_relationships as (
select
resource_id,
direct_parent_id
direct_parent_id,
is_primary_relationship
from {{ ref('stg_node_relationships')}}
),

direct_exposure_relationships as (
select
resource_id,
direct_parent_id
direct_parent_id,
is_primary_relationship
from {{ ref('stg_exposure_relationships')}}
),

direct_metrics_relationships as (
select
resource_id,
direct_parent_id
direct_parent_id,
is_primary_relationship
from {{ ref('stg_metric_relationships')}}
),

-- for all resources in the graph, find their direct parent
direct_relationships as (
select
all_graph_resources.*,
CASE
WHEN all_graph_resources.resource_type = 'source' THEN NULL
WHEN all_graph_resources.resource_type = 'exposure' THEN exposures.direct_parent_id
WHEN all_graph_resources.resource_type = 'metric' THEN metrics.direct_parent_id
WHEN all_graph_resources.resource_type IN ('model', 'snapshot', 'test') THEN models.direct_parent_id
ELSE NULL
END AS direct_parent_id
case
when all_graph_resources.resource_type = 'source' then null
when all_graph_resources.resource_type = 'exposure' then exposures.direct_parent_id
when all_graph_resources.resource_type = 'metric' then metrics.direct_parent_id
when all_graph_resources.resource_type in ('model', 'snapshot', 'test') then models.direct_parent_id
else null
end as direct_parent_id,
(
all_graph_resources.resource_type = 'test'
and models.is_primary_relationship
) as is_primary_test_relationship
from all_graph_resources
left join direct_model_relationships as models
on all_graph_resources.resource_id = models.resource_id
Expand Down
1 change: 1 addition & 0 deletions models/marts/structure/fct_test_directories.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ models_per_test as (
direct_parent_id as parent_model_id
from relationships
where resource_type = 'test'
and is_primary_test_relationship

),

Expand Down
1 change: 1 addition & 0 deletions models/marts/tests/intermediate/int_model_test_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ count_column_tests as (
left join relationships
on all_graph_resources.resource_id = relationships.resource_id
where all_graph_resources.resource_type = 'test'
and relationships.is_primary_test_relationship
group by 1,2
),

Expand Down

0 comments on commit 3ff7f50

Please sign in to comment.