ashb commented on a change in pull request #4743: [AIRFLOW-3871] render
Operators template fields recursively
URL: https://github.com/apache/airflow/pull/4743#discussion_r310051778
##########
File path: tests/operators/test_python_operator.py
##########
@@ -242,6 +249,510 @@ def test_echo_env_variables(self):
)
t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
+ def test_template_field_value_rendering(self):
+ """Test PythonOperator template field with a simple templated string"""
+ recorded_calls = []
+
+ task = PythonOperator(
+ task_id='python_operator',
+ python_callable=build_recording_function(recorded_calls),
+ provide_context=True,
+ templates_dict={
+ 'a_templated_string': "dag {{dag.dag_id}} ran on {{ds}}."
+ },
+ dag=self.dag)
+
+ self.dag.create_dagrun(
+ run_id='manual__' + DEFAULT_DATE.isoformat(),
+ execution_date=DEFAULT_DATE,
+ start_date=DEFAULT_DATE,
+ state=State.RUNNING
+ )
+ task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
+
+ self.assertEqual(1, len(recorded_calls))
+ self.assertTrue('templates_dict' in recorded_calls[0].kwargs)
+ self.assertDictEqual(
+ recorded_calls[0].kwargs['templates_dict'],
+ {'a_templated_string': "dag {} ran on {}.".format(self.dag.dag_id,
DEFAULT_DATE.date())}
+ )
+
+ def test_template_field_object_rendering(self):
+ """Test PythonOperator template field with a nested templated string
attribute"""
+ recorded_calls = []
+ ClassWithCustomAttributes.template_fields = ['templated_string_attr']
+
+ task = PythonOperator(
+ task_id='python_operator',
+ python_callable=build_recording_function(recorded_calls),
+ provide_context=True,
+ templates_dict={
+ 'a_templated_object': ClassWithCustomAttributes(
+ templated_string_attr="dag {{dag.dag_id}} ran on {{ds}}."
+ )
+ },
+ dag=self.dag)
+
+ self.dag.create_dagrun(
+ run_id='manual__' + DEFAULT_DATE.isoformat(),
+ execution_date=DEFAULT_DATE,
+ start_date=DEFAULT_DATE,
+ state=State.RUNNING
+ )
+ task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
+
+ self.assertEqual(1, len(recorded_calls))
+ self.assertTrue('templates_dict' in recorded_calls[0].kwargs)
+ self.assertDictEqual(
+ recorded_calls[0].kwargs['templates_dict'],
+ {
+ 'a_templated_object': ClassWithCustomAttributes(
+ templated_string_attr="dag {} ran on
{}.".format(self.dag.dag_id, DEFAULT_DATE.date())
+ )
+ }
+ )
+
+ def test_multiple_template_fields_rendering(self):
+ """Test PythonOperator template field with multiple templated and non
templated values"""
+ recorded_calls = []
+ ClassWithCustomAttributes.template_fields = ['templated_string_attr']
+
+ task = PythonOperator(
+ task_id='python_operator',
+ python_callable=build_recording_function(recorded_calls),
+ provide_context=True,
+ templates_dict={
+ 'an_int': 4,
+ 'a_date': date(2019, 2, 18),
+ 'a_string': "this is a static string",
+ 'a_templated_string': "this is a templated string for dag
{{dag.dag_id}}",
+ 'an_object': ClassWithCustomAttributes(
+ templated_string_attr="static string here",
+ static_string_attr='static string',
+ int_attr=5,
+ date_attr=date(2019, 2, 19)
+ ),
+ 'a_templated_object': ClassWithCustomAttributes(
+ templated_string_attr="dag {{dag.dag_id}}",
+ static_string_attr="static_string",
+ int_attr=6,
+ date_attr=date(2019, 2, 20)
+ )
+ },
+ dag=self.dag)
+
+ self.dag.create_dagrun(
+ run_id='manual__' + DEFAULT_DATE.isoformat(),
+ execution_date=DEFAULT_DATE,
+ start_date=DEFAULT_DATE,
+ state=State.RUNNING
+ )
+ task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
+
+ self.assertEqual(1, len(recorded_calls))
+ self.assertTrue('templates_dict' in recorded_calls[0].kwargs)
+ self.assertDictEqual(
+ recorded_calls[0].kwargs['templates_dict'],
+ {
+ 'an_int': 4,
+ 'a_date': date(2019, 2, 18),
+ 'a_string': "this is a static string",
+ 'a_templated_string': "this is a templated string for dag
{}".format(self.dag.dag_id),
+ 'an_object': ClassWithCustomAttributes(
+ templated_string_attr="static string here",
+ static_string_attr='static string',
+ int_attr=5,
+ date_attr=date(2019, 2, 19)
+ ),
+ 'a_templated_object': ClassWithCustomAttributes(
+ templated_string_attr="dag {}".format(self.dag.dag_id),
+ static_string_attr="static_string",
+ int_attr=6,
+ date_attr=date(2019, 2, 20)
+ )
+ }
+ )
+
+ def
test_inner_attributes_are_rendered_when_marked_as_template_fields_only(self):
Review comment:
This looks like it covers the same cases as
`test_multiple_template_fields_rendering`?
----------------------------------------------------------------
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