Skip to content

Commit

Permalink
fixup! Add basic templating to snow sql
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Mar 14, 2024
1 parent 60f42ca commit 3b0f8b5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/snowflake/cli/api/utils/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def _env_bootstrap(env: Environment) -> Environment:
variable_end_string="}",
block_start_string=_RANDOM_BLOCK,
block_end_string=_RANDOM_BLOCK,
undefined=StrictUndefined,
)
)

Expand Down
8 changes: 6 additions & 2 deletions src/snowflake/cli/plugins/sql/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pathlib import Path
from typing import Dict, Iterable, Optional, Tuple

from click import UsageError
from click import ClickException, UsageError
from jinja2 import UndefinedError
from snowflake.cli.api.secure_path import UNLIMITED, SecurePath
from snowflake.cli.api.sql_execution import SqlExecutionMixin
from snowflake.cli.api.utils.rendering import snowflake_cli_jinja_render
Expand Down Expand Up @@ -36,7 +37,10 @@ def execute(

if data:
# Do rendering if any data was provided
query = snowflake_cli_jinja_render(content=query, data=data)
try:
query = snowflake_cli_jinja_render(content=query, data=data)
except UndefinedError as err:
raise ClickException(f"SQL template rendering error: {err}")

statements = tuple(
statement
Expand Down
34 changes: 34 additions & 0 deletions tests/api/utils/test_rendering.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from jinja2 import UndefinedError
from snowflake.cli.api.utils.rendering import snowflake_cli_jinja_render


Expand All @@ -21,3 +22,36 @@ def test_rendering_with_data():
)
def test_rendering(text, output):
assert snowflake_cli_jinja_render(text, data={"foo": "bar"}) == output


@pytest.mark.parametrize(
"text",
[
"""
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
""",
"""{% if loop.index is divisibleby 3 %}""",
"""
{% if True %}
yay
{% endif %}
""",
],
)
def test_that_common_logic_block_are_ignored(text):
assert snowflake_cli_jinja_render(text) == text


def test_that_common_comments_are_respected():
assert snowflake_cli_jinja_render("{# note a comment %{ foo } #}") == ""
assert (
snowflake_cli_jinja_render("{# note a comment #}%{ foo }", data={"foo": "bar"})
== "bar"
)


def test_that_undefined_variables_raise_error():
with pytest.raises(UndefinedError):
snowflake_cli_jinja_render("%{ foo }")
5 changes: 5 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,8 @@ def test_no_rendering_of_sql_if_no_data(mock_execute_query, runner):
result = runner.invoke(["sql", "-q", "select %{ aaa }.%{ bbb }"])
assert result.exit_code == 0, result.output
mock_execute_query.assert_called_once_with("select %{ aaa }.%{ bbb }")


def test_execution_fails_if_unknown_variable(runner):
result = runner.invoke(["sql", "-q", "select %{ foo }", "-D", "bbb=1"])
assert "SQL template rendering error: 'foo' is undefined" in result.output

0 comments on commit 3b0f8b5

Please sign in to comment.