BasPH commented on a change in pull request #5461: [AIRFLOW-4835] Refactor
render_template
URL: https://github.com/apache/airflow/pull/5461#discussion_r309210653
##########
File path: tests/models/test_dag.py
##########
@@ -387,243 +388,93 @@ def test_get_num_task_instances(self):
)
session.close()
- def test_render_template_field(self):
- """Tests if render_template from a field works"""
-
- dag = DAG('test-dag',
- start_date=DEFAULT_DATE)
-
- with dag:
+ @parameterized.expand(
+ [
+ ("{{ foo }}", {"foo": "bar"}, "bar"),
+ ("{{ foo }}", {}, ""),
+ (["{{ foo }}_1", "{{ foo }}_2"], {"foo": "bar"}, ["bar_1",
"bar_2"]),
+ (('{{ foo }}_1', '{{ foo }}_2'), {"foo": "bar"}, ("bar_1",
"bar_2")),
+ ({'key1': '{{ foo }}_1', 'key2': '{{ foo }}_2'}, {"foo": "bar"},
{'key1': 'bar_1', 'key2': 'bar_2'}), # noqa: E501, pylint:
disable=line-too-long
+ ({'key_{{ foo }}_1': 1, 'key_2': '{{ foo }}_2'}, {'foo': 'bar'},
{'key_{{ foo }}_1': 1, 'key_2': 'bar_2'}), # noqa: E501, pylint:
disable=line-too-long
+ (datetime.date(2018, 12, 6), {'foo': 'bar'}, datetime.date(2018,
12, 6)),
+ (datetime.datetime(2018, 12, 6, 10, 55), {'foo': 'bar'},
datetime.datetime(2018, 12, 6, 10, 55)),
+ ]
+ )
+ def test_render_template_fields(self, content, context, expected_output):
+ """Test render_template given various input types."""
+ with DAG('test-dag', start_date=DEFAULT_DATE):
task = DummyOperator(task_id='op1')
- result = task.render_template('', '{{ foo }}', dict(foo='bar'))
- self.assertEqual(result, 'bar')
-
- def test_render_template_field_undefined(self):
- """Tests if render_template from a field works"""
-
- dag = DAG('test-dag',
- start_date=DEFAULT_DATE)
-
- with dag:
+ result = task.render_template(content, context)
+ self.assertEqual(result, expected_output)
+
+ @parameterized.expand(
+ [
+ ({"user_defined_macros": {"foo": "bar"}}, "{{ foo }}", {}, "bar"),
+ ({"user_defined_macros": {"foo": "bar"}}, 1, {}, 1),
+ ({"user_defined_filters": {"hello": lambda name: 'Hello %s' %
name}}, "{{ 'world' | hello }}", {}, "Hello world"), # noqa: E501, pylint:
disable=line-too-long
+ ]
+ )
+ def test_render_template_fields_with_dag_settings(self, dag_kwargs,
content, context, expected_output):
+ """Test render_template with additional DAG settings."""
+ with DAG('test-dag', start_date=DEFAULT_DATE, **dag_kwargs):
task = DummyOperator(task_id='op1')
- result = task.render_template('', '{{ foo }}', {})
- self.assertEqual(result, '')
+ result = task.render_template(content, context)
+ self.assertEqual(result, expected_output)
def test_render_template_field_undefined_strict(self):
- """Tests if render_template from a field works"""
-
- dag = DAG('test-dag',
- start_date=DEFAULT_DATE,
- template_undefined=jinja2.StrictUndefined)
-
- with dag:
+ """Test render_template with template_undefined configured."""
+ with DAG('test-dag', start_date=DEFAULT_DATE,
template_undefined=jinja2.StrictUndefined):
task = DummyOperator(task_id='op1')
with self.assertRaises(jinja2.UndefinedError):
- task.render_template('', '{{ foo }}', {})
-
- def test_render_template_list_field(self):
- """Tests if render_template from a list field works"""
-
- dag = DAG('test-dag',
- start_date=DEFAULT_DATE)
-
- with dag:
- task = DummyOperator(task_id='op1')
-
- self.assertListEqual(
- task.render_template('', ['{{ foo }}_1', '{{ foo }}_2'], {'foo':
'bar'}),
- ['bar_1', 'bar_2']
- )
-
- def test_render_template_tuple_field(self):
- """Tests if render_template from a tuple field works"""
-
- dag = DAG('test-dag',
- start_date=DEFAULT_DATE)
-
- with dag:
- task = DummyOperator(task_id='op1')
-
- # tuple is returned
- self.assertTupleEqual(
- task.render_template('', ('{{ foo }}_1', '{{ foo }}_2'), {'foo':
'bar'}),
- ('bar_1', 'bar_2')
- )
+ task.render_template('{{ foo }}', {})
- def test_render_template_named_tuple_field(self):
+ def test_render_template_namedtuple_field(self):
Review comment:
There's a few more tests for `render_template` which don't fit the general
parameterized tests. This is one of them, since we must first initialize `Named
= namedtuple('Named', ['var1', 'var2'])` in the test.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services