Skip to content

Commit 478cd62

Browse files
committed
Provide required missing context to resolve sql_header in unit tests
1 parent 300aa09 commit 478cd62

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Provide required missing context to resolve sql_header in unit tests
3+
time: 2025-03-27T00:27:11.364727+01:00
4+
custom:
5+
Author: Kayrnt
6+
Issue: "9775"

core/dbt/parser/unit_tests.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from dbt.artifacts.resources import ModelConfig, UnitTestConfig, UnitTestFormat
1111
from dbt.config import RuntimeConfig
1212
from dbt.context.context_config import ContextConfig
13-
from dbt.context.providers import generate_parse_exposure, get_rendered
13+
from dbt.context.providers import (
14+
generate_parse_exposure,
15+
generate_runtime_unit_test_context,
16+
get_rendered,
17+
)
1418
from dbt.contracts.files import FileHash, SchemaSourceFile
1519
from dbt.contracts.graph.manifest import Manifest
1620
from dbt.contracts.graph.model_config import UnitTestNodeConfig
@@ -99,13 +103,17 @@ def parse_unit_test_case(self, test_case: UnitTestDefinition):
99103
tested_node_unique_id=tested_node.unique_id,
100104
overrides=test_case.overrides,
101105
)
102-
103-
ctx = generate_parse_exposure(
104-
unit_test_node, # type: ignore
105-
self.root_project,
106-
self.manifest,
107-
test_case.package_name,
106+
# setting up the execution environment for a dbt unit test by creating a context with all the necessary functions and objects
107+
ctx = generate_runtime_unit_test_context(unit_test_node, self.root_project, self.manifest)
108+
ctx.update(
109+
generate_parse_exposure(
110+
unit_test_node, # type: ignore
111+
self.root_project,
112+
self.manifest,
113+
test_case.package_name,
114+
)
108115
)
116+
109117
get_rendered(unit_test_node.raw_code, ctx, unit_test_node, capture_macros=True)
110118
# unit_test_node now has a populated refs/sources
111119

core/dbt/task/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ def execute_unit_test(
237237
"Invalid materialization context generated, missing config: {}".format(context)
238238
)
239239

240+
# allows test code to access the configuration settings of the component it's testing
241+
if unit_test_node.tested_node_unique_id:
242+
context["tested_node_config"] = manifest.nodes[
243+
unit_test_node.tested_node_unique_id
244+
].config
245+
240246
# generate materialization macro
241247
macro_func = MacroGenerator(materialization_macro, context)
242248
try:

0 commit comments

Comments
 (0)