Skip to content

Commit

Permalink
Add parameters as template field for SqlSensor (#39588)
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-fell committed May 13, 2024
1 parent dc61da2 commit ce4e847
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion airflow/providers/common/sql/sensors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SqlSensor(BaseSensorOperator):
Should match the desired hook constructor params.
"""

template_fields: Sequence[str] = ("sql", "hook_params")
template_fields: Sequence[str] = ("sql", "hook_params", "parameters")
template_ext: Sequence[str] = (
".hql",
".sql",
Expand Down
15 changes: 15 additions & 0 deletions tests/providers/common/sql/sensors/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,18 @@ def test_sql_sensor_hook_params(self):
)
hook = op._get_hook()
assert hook.log_sql == op.hook_params["log_sql"]

@mock.patch("airflow.providers.common.sql.sensors.sql.BaseHook")
def test_sql_sensor_templated_parameters(self, mock_base_hook):
op = SqlSensor(
task_id="sql_sensor_templated_parameters",
conn_id="snowflake_default",
sql="SELECT %(something)s",
parameters={"something": "{{ logical_date }}"},
)
op.render_template_fields(context={"logical_date": "1970-01-01"})
mock_base_hook.get_connection.return_value.get_hook.return_value = mock.MagicMock(spec=DbApiHook)
mock_get_records = mock_base_hook.get_connection.return_value.get_hook.return_value.get_records
op.execute(context=mock.MagicMock())

mock_get_records.assert_called_once_with("SELECT %(something)s", {"something": "1970-01-01"})

0 comments on commit ce4e847

Please sign in to comment.