-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In schema tests, pass rendered items (#2149) #2220
Merged
beckjake
merged 8 commits into
dev/octavius-catto
from
feature/schema-tests-pass-rendered
Mar 25, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d20c301
split test types into data/schema tests
31025a0
remove wrapped_sql, move data test wrapping into the node runner
dc65118
Add metadata to tests, add a native env
96cfc49
rev hologram to 0.0.7
e9a053c
Update core/dbt/node_runners.py
beckjake 434edce
remove extra methods
1268c11
require jinja 2.11.x
38bcc2b
override NativeSandboxTemplate.render(), remove the quotes hack
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import itertools | ||
import os | ||
from collections import defaultdict | ||
from typing import List, Dict | ||
from typing import List, Dict, Any | ||
|
||
import dbt.utils | ||
import dbt.include | ||
|
@@ -11,11 +11,17 @@ | |
from dbt.linker import Linker | ||
|
||
from dbt.context.providers import generate_runtime_model | ||
from dbt.contracts.graph.manifest import Manifest | ||
import dbt.contracts.project | ||
import dbt.exceptions | ||
import dbt.flags | ||
import dbt.config | ||
from dbt.contracts.graph.compiled import InjectedCTE, COMPILED_TYPES | ||
from dbt.contracts.graph.compiled import ( | ||
InjectedCTE, | ||
COMPILED_TYPES, | ||
NonSourceCompiledNode, | ||
CompiledSchemaTestNode, | ||
) | ||
from dbt.contracts.graph.parsed import ParsedNode | ||
|
||
from dbt.logger import GLOBAL_LOGGER as logger | ||
|
@@ -24,12 +30,12 @@ | |
|
||
|
||
def _compiled_type_for(model: ParsedNode): | ||
if model.resource_type not in COMPILED_TYPES: | ||
if type(model) not in COMPILED_TYPES: | ||
raise dbt.exceptions.InternalException( | ||
'Asked to compile {} node, but it has no compiled form' | ||
.format(model.resource_type) | ||
.format(type(model)) | ||
) | ||
return COMPILED_TYPES[model.resource_type] | ||
return COMPILED_TYPES[type(model)] | ||
|
||
|
||
def print_compile_stats(stats): | ||
|
@@ -130,6 +136,22 @@ def initialize(self): | |
dbt.clients.system.make_directory(self.config.target_path) | ||
dbt.clients.system.make_directory(self.config.modules_path) | ||
|
||
def _create_node_context( | ||
self, | ||
node: NonSourceCompiledNode, | ||
manifest: Manifest, | ||
extra_context: Dict[str, Any], | ||
) -> Dict[str, Any]: | ||
context = generate_runtime_model( | ||
node, self.config, manifest | ||
) | ||
context.update(extra_context) | ||
if isinstance(node, CompiledSchemaTestNode): | ||
# for test nodes, add a special keyword args value to the context | ||
dbt.clients.jinja.add_rendered_test_kwargs(context, node) | ||
|
||
return context | ||
|
||
def compile_node(self, node, manifest, extra_context=None): | ||
if extra_context is None: | ||
extra_context = {} | ||
|
@@ -146,10 +168,9 @@ def compile_node(self, node, manifest, extra_context=None): | |
}) | ||
compiled_node = _compiled_type_for(node).from_dict(data) | ||
|
||
context = generate_runtime_model( | ||
compiled_node, self.config, manifest | ||
context = self._create_node_context( | ||
compiled_node, manifest, extra_context | ||
) | ||
context.update(extra_context) | ||
|
||
compiled_node.compiled_sql = dbt.clients.jinja.get_rendered( | ||
node.raw_sql, | ||
|
@@ -160,33 +181,6 @@ def compile_node(self, node, manifest, extra_context=None): | |
|
||
injected_node, _ = prepend_ctes(compiled_node, manifest) | ||
|
||
should_wrap = {NodeType.Test, NodeType.Operation} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ye shall not be missed |
||
if injected_node.resource_type in should_wrap: | ||
# data tests get wrapped in count(*) | ||
# TODO : move this somewhere more reasonable | ||
if 'data' in injected_node.tags and \ | ||
injected_node.resource_type == NodeType.Test: | ||
injected_node.wrapped_sql = ( | ||
"select count(*) as errors " | ||
"from (\n{test_sql}\n) sbq").format( | ||
test_sql=injected_node.injected_sql) | ||
else: | ||
# don't wrap schema tests or analyses. | ||
injected_node.wrapped_sql = injected_node.injected_sql | ||
|
||
elif injected_node.resource_type == NodeType.Snapshot: | ||
# unfortunately we do everything automagically for | ||
# snapshots. in the future it'd be nice to generate | ||
# the SQL at the parser level. | ||
pass | ||
|
||
elif(injected_node.resource_type == NodeType.Model and | ||
injected_node.get_materialization() == 'ephemeral'): | ||
pass | ||
|
||
else: | ||
injected_node.wrapped_sql = None | ||
|
||
return injected_node | ||
|
||
def write_graph_file(self, linker, manifest): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have the ability to reach into the
context
and call these functions directly? I think the approach shown here is a-ok for now, but I'd like to remove this jinja hackery in the future if we can!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could, but it would be quite tricky to handle things like
ref(var('foo'))
because you have to recursively descend into the argumentsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, interesting - that's fair. Thanks!