ashb commented on a change in pull request #5461: [AIRFLOW-4835] Refactor render_template URL: https://github.com/apache/airflow/pull/5461#discussion_r309626704
########## File path: tests/models/test_dag.py ########## @@ -387,243 +392,72 @@ 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)), + (TestNamedTuple('{{ foo }}_1', '{{ foo }}_2'), {'foo': 'bar'}, TestNamedTuple('bar_1', 'bar_2')), + ] + ) + 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""" + 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 Review comment: ```suggestion ({"user_defined_filters": {"hello": lambda name: 'Hello %s' % name}}, "{{ 'world' | hello }}", {}, "Hello world"), ``` perhaps? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services