Skip to content
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

Fix the tests (#2317) #2318

Merged
merged 3 commits into from Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions core/dbt/clients/jinja.py
Expand Up @@ -105,22 +105,6 @@ class NativeSandboxEnvironment(MacroFuzzEnvironment):
class NativeSandboxTemplate(jinja2.nativetypes.NativeTemplate): # mypy: ignore
environment_class = NativeSandboxEnvironment

def render(self, *args, **kwargs):
"""Render the template to produce a native Python type. If the
result is a single node, its value is returned. Otherwise, the
nodes are concatenated as strings. If the result can be parsed
with :func:`ast.literal_eval`, the parsed value is returned.
Otherwise, the string is returned.
"""
vars = dict(*args, **kwargs)
try:
return jinja2.nativetypes.native_concat(
self.root_render_func(self.new_context(vars)),
preserve_quotes=True
)
except Exception:
return self.environment.handle_exception()


NativeSandboxEnvironment.template_class = NativeSandboxTemplate # type: ignore

Expand Down Expand Up @@ -441,6 +425,18 @@ def render_template(template, ctx: Dict[str, Any], node=None) -> str:
return template.render(ctx)


def _requote_result(raw_value, rendered):
double_quoted = raw_value.startswith('"') and raw_value.endswith('"')
single_quoted = raw_value.startswith("'") and raw_value.endswith("'")
if double_quoted:
quote_char = '"'
elif single_quoted:
quote_char = "'"
else:
quote_char = ''
return f'{quote_char}{rendered}{quote_char}'


def get_rendered(
string: str,
ctx: Dict[str, Any],
Expand All @@ -456,7 +452,11 @@ def get_rendered(
native=native,
)

return render_template(template, ctx, node)
result = render_template(template, ctx, node)

if native and isinstance(result, str):
result = _requote_result(string, result)
return result


def undefined_error(msg) -> NoReturn:
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/parser/schema_test_builders.py
@@ -1,5 +1,6 @@
import hashlib
import re
from copy import deepcopy
from dataclasses import dataclass
from typing import (
Generic, TypeVar, Dict, Any, Tuple, Optional, List, Sequence
Expand Down Expand Up @@ -287,6 +288,7 @@ def extract_test_args(test, name=None) -> Tuple[str, Dict[str, Any]]:
type(test_name), test_name
)
)
test_args = deepcopy(test_args)
if name is not None:
test_args['column_name'] = name
return test_name, test_args
Expand Down
15 changes: 0 additions & 15 deletions core/dbt/parser/schemas.py
Expand Up @@ -196,21 +196,6 @@ def parse_column_tests(
for test in column.tests:
self.parse_test(block, test, column)

def _build_raw_test_keyword_args(
self, parsed_node: ParsedSchemaTestNode, builder: TestBuilder
) -> Dict[str, Any]:
"""Build up the test keyword arguments."""
kwargs = parsed_node.test_metadata.kwargs.copy()
if isinstance(builder.target, UnparsedNodeUpdate):
fmt = "{{{{ ref('{0.name}') }}}}"
elif isinstance(builder.target, SourceTarget):
fmt = "{{{{ source('{0.source.name}', '{0.table.name}') }}}}"
else:
raise TypeError(f'invalid target type "{type(builder.target)}"')

kwargs['model'] = fmt.format(builder.target)
return kwargs

def parse_node(self, block: SchemaTestBlock) -> ParsedSchemaTestNode:
"""In schema parsing, we rewrite most of the part of parse_node that
builds the initial node to be parsed, but rendering is basically the
Expand Down
2 changes: 1 addition & 1 deletion core/setup.py
Expand Up @@ -53,7 +53,7 @@ def read(fname):
'scripts/dbt',
],
install_requires=[
'Jinja2>=2.11,<3',
'Jinja2==2.11.2',
'PyYAML>=3.11',
'sqlparse>=0.2.3,<0.4',
'networkx>=2.3,<3',
Expand Down