Skip to content

Commit 3ff7f50

Browse files
Merge pull request #183 from dbt-labs/fix/alt-multi-ref
fix multi ref tests - alt approach
2 parents cbc788a + e681c59 commit 3ff7f50

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

integration_tests/models/staging/staging.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ models:
77
tests:
88
- unique
99
- not_null
10+
- relationships:
11+
to: ref('stg_model_2')
12+
field: id
1013
- name: "concat(coalesce('fake_column', ' '),'for_testing')"
1114
tests:
1215
- not_null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
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
2-
14,8,4,28.57,60.00,50.00,0.00,0.00,0.5714
2+
14,9,4,28.57,60.00,50.00,0.00,0.00,0.6429

macros/unpack/get_relationships.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
{%- macro default__get_relationships(node_type) -%}
66

77
{%- if execute -%}
8-
98
{%- if node_type == 'nodes' %}
109
{% set nodes_list = graph.nodes.values() %}
1110
{%- elif node_type == 'exposures' -%}
@@ -25,7 +24,8 @@
2524
{%- set values_line =
2625
[
2726
"cast('" ~ node.unique_id ~ "' as " ~ dbt_utils.type_string() ~ ")",
28-
"cast(NULL as " ~ dbt_utils.type_string() ~ ")"
27+
"cast(NULL as " ~ dbt_utils.type_string() ~ ")",
28+
"FALSE",
2929
]
3030
%}
3131

@@ -38,7 +38,8 @@
3838
{%- set values_line =
3939
[
4040
"cast('" ~ node.unique_id ~ "' as " ~ dbt_utils.type_string() ~ ")",
41-
"cast('" ~ parent ~ "' as " ~ dbt_utils.type_string() ~ ")"
41+
"cast('" ~ parent ~ "' as " ~ dbt_utils.type_string() ~ ")",
42+
"" ~ loop.last ~ ""
4243
]
4344
%}
4445

@@ -55,7 +56,8 @@
5556
values = values,
5657
columns = [
5758
'resource_id',
58-
'direct_parent_id'
59+
'direct_parent_id',
60+
'is_primary_relationship'
5961
]
6062
)
6163
) }}

models/marts/core/int_direct_relationships.sql

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,42 @@ all_graph_resources as (
1818
direct_model_relationships as (
1919
select
2020
resource_id,
21-
direct_parent_id
21+
direct_parent_id,
22+
is_primary_relationship
2223
from {{ ref('stg_node_relationships')}}
2324
),
2425

2526
direct_exposure_relationships as (
2627
select
2728
resource_id,
28-
direct_parent_id
29+
direct_parent_id,
30+
is_primary_relationship
2931
from {{ ref('stg_exposure_relationships')}}
3032
),
3133

3234
direct_metrics_relationships as (
3335
select
3436
resource_id,
35-
direct_parent_id
37+
direct_parent_id,
38+
is_primary_relationship
3639
from {{ ref('stg_metric_relationships')}}
3740
),
3841

3942
-- for all resources in the graph, find their direct parent
4043
direct_relationships as (
4144
select
4245
all_graph_resources.*,
43-
CASE
44-
WHEN all_graph_resources.resource_type = 'source' THEN NULL
45-
WHEN all_graph_resources.resource_type = 'exposure' THEN exposures.direct_parent_id
46-
WHEN all_graph_resources.resource_type = 'metric' THEN metrics.direct_parent_id
47-
WHEN all_graph_resources.resource_type IN ('model', 'snapshot', 'test') THEN models.direct_parent_id
48-
ELSE NULL
49-
END AS direct_parent_id
46+
case
47+
when all_graph_resources.resource_type = 'source' then null
48+
when all_graph_resources.resource_type = 'exposure' then exposures.direct_parent_id
49+
when all_graph_resources.resource_type = 'metric' then metrics.direct_parent_id
50+
when all_graph_resources.resource_type in ('model', 'snapshot', 'test') then models.direct_parent_id
51+
else null
52+
end as direct_parent_id,
53+
(
54+
all_graph_resources.resource_type = 'test'
55+
and models.is_primary_relationship
56+
) as is_primary_test_relationship
5057
from all_graph_resources
5158
left join direct_model_relationships as models
5259
on all_graph_resources.resource_id = models.resource_id

models/marts/structure/fct_test_directories.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ models_per_test as (
2121
direct_parent_id as parent_model_id
2222
from relationships
2323
where resource_type = 'test'
24+
and is_primary_test_relationship
2425

2526
),
2627

models/marts/tests/intermediate/int_model_test_summary.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ count_column_tests as (
1919
left join relationships
2020
on all_graph_resources.resource_id = relationships.resource_id
2121
where all_graph_resources.resource_type = 'test'
22+
and relationships.is_primary_test_relationship
2223
group by 1,2
2324
),
2425

0 commit comments

Comments
 (0)