Skip to content

Commit

Permalink
Merge pull request #2599 from fishtown-analytics/fix/more-jinja-quoti…
Browse files Browse the repository at this point in the history
…ng-sadness

Fix more jinja quoting sadness
  • Loading branch information
beckjake authored Jun 30, 2020
2 parents e7298d6 + b14676f commit a201fc7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## dbt 0.17.1 (Release TBD)

### Fixes
- dbt native rendering now avoids turning quoted strings into unquoted strings ([#2597](https://github.com/fishtown-analytics/dbt/issues/2597), [#2599](https://github.com/fishtown-analytics/dbt/pull/2599))
- Hash name of local packages ([#2600](https://github.com/fishtown-analytics/dbt/pull/2600))

## dbt 0.17.1rc2 (June 25, 2020)


## dbt 0.17.1rc2 (June 25, 2020)

### Fixes
- dbt config-version: 2 now properly defers rendering `+pre-hook` and `+post-hook` fields. ([#2583](https://github.com/fishtown-analytics/dbt/issues/2583), [#2854](https://github.com/fishtown-analytics/dbt/pull/2854))
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def quoted_native_concat(nodes):
except (ValueError, SyntaxError, MemoryError):
return raw

# if it was a str and it still is a str, return it as-is.
if isinstance(result, str):
result = raw

return result


Expand Down
62 changes: 62 additions & 0 deletions test/unit/test_jinja.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import unittest
import yaml

from dbt.clients.jinja import get_rendered
from dbt.clients.jinja import get_template
Expand Down Expand Up @@ -413,3 +415,63 @@ def test_if_endfor_newlines(self):
'''


native_expected_behaviors = [
# strings
('''foo: bar''', 'bar'),
('''foo: "bar"''', 'bar'),
('''foo: "'bar'"''', "'bar'"),
("""foo: '"bar"'""", '"bar"'),
# ints
('''foo: 1''', 1),
('''foo: "1"''', 1),
('''foo: "'1'"''', "'1'"),
('''foo: "{{ 1 }}"''', 1),
('''foo: "{{ '1' }}"''', 1),
('''foo: "'{{ 1 }}'"''', "'1'"),
('''foo: "'{{ '1' }}'"''', "'1'"),
('''foo: "{{ 1 | as_text }}"''', '1'),
('''foo: "{{ '1' | as_text }}"''', '1'),
# booleans.
# Note the discrepancy with true vs True: `true` is recognized by jinja but
# not literal_eval, but `True` is recognized by ast.literal_eval.
# For extra fun, yaml recognizes both.
('''foo: "{{ true }}"''', True),
('''foo: "{{ 'true' }}"''', 'true'),
('''foo: "'{{ true }}'"''', "'True'"),
('''foo: "{{ true | as_text }}"''', "True"), # true -> boolean True -> text -> str(True) -> 'True'
('''foo: "{{ 'true' | as_text }}"''', "true"), # 'true' -> string 'true' -> text -> str('true') -> 'true'
('''foo: "{{ True }}"''', True),
('''foo: "{{ 'True' }}"''', True),
('''foo: "'{{ True }}'"''', "'True'"),
('''foo: "{{ True | as_text }}"''', "True"), # True -> string 'True' -> text -> str('True') -> 'True'
('''foo: "{{ 'True' | as_text }}"''', "True"), # 'True' -> string 'True' -> text -> str('True') -> 'True'
('''foo: yes''', True), # yaml turns 'yes' into a boolean true
('''foo: "yes"''', "yes"),
# concatenation
('''foo: "{{ a_int + 100 }}"''', 200),
('''foo: "{{ a_str ~ 100 }}"''', 100100),
('''foo: "{{ a_int ~ 100 }}"''', 100100),
('''foo: "{{ a_str }}{{ a_str }}"''', 100100),
('''foo: "{{ a_int }}{{ a_int }}"''', 100100),
('''foo: "'{{ a_int }}{{ a_int }}'"''', "'100100'"),

]


def expected_id(arg):
if isinstance(arg, list):
return '_'.join(arg)


@pytest.mark.parametrize(
'inputvalue,expected', native_expected_behaviors, ids=expected_id
)
def test_native_rendering(inputvalue, expected):
# this test is pretty useless without preprocessing things in yaml.
value = yaml.safe_load(inputvalue)['foo']
ctx = {
'a_str': '100',
'a_int': 100,
'b_str': 'hello'
}
assert get_rendered(value, ctx, native=True) == expected

0 comments on commit a201fc7

Please sign in to comment.