ashb commented on a change in pull request #5461: [AIRFLOW-4835] Refactor 
render_template
URL: https://github.com/apache/airflow/pull/5461#discussion_r309626828
 
 

 ##########
 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
+        ]
+    )
+    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')
 
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
+        result = task.render_template(content, context)
+        self.assertEqual(result, expected_output)
 
-        with dag:
+    @parameterized.expand([
+        (object(),),
+        (uuid.uuid4(),)
+    ])
+    def test_render_template_fields_no_change(self, content):
+        """Tests if the templated object remains unchanged"""
+        with DAG('test-dag', start_date=DEFAULT_DATE):
             task = DummyOperator(task_id='op1')
 
-        result = task.render_template('', '{{ foo }}', {})
-        self.assertEqual(result, '')
+        result = task.render_template(content, {'foo': 'bar'})
+        self.assertEqual(content, result)
 
     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')
-        )
-
-    def test_render_template_named_tuple_field(self):
-        """Tests if render_template from a named tuple field works"""
-
-        Named = namedtuple('Named', ['var1', 'var2'])
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        expected = Named('bar_1', 'bar_2')
-        actual = task.render_template('', Named('{{ foo }}_1', '{{ foo }}_2'), 
{'foo': 'bar'})
-
-        # Named tuple's field access is preserved but are still rendered
-        self.assertTupleEqual(expected, actual)
-        self.assertEqual(
-            expected.var1,
-            actual.var1,
-            msg="Named tuples may not have been preserved in rendering"
-        )
-        self.assertEqual(
-            expected.var2,
-            actual.var2,
-            msg="Named tuples may not have been preserved in rendering"
-        )
-
-    def test_render_template_dict_field(self):
-        """Tests if render_template from a dict field works"""
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        self.assertDictEqual(
-            task.render_template('', {'key1': '{{ foo }}_1', 'key2': '{{ foo 
}}_2'}, {'foo': 'bar'}),
-            {'key1': 'bar_1', 'key2': 'bar_2'}
-        )
-
-    def test_render_template_dict_field_with_templated_keys(self):
-        """Tests if render_template from a dict field works as expected:
-        dictionary keys are not templated"""
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        self.assertDictEqual(
-            task.render_template('', {'key_{{ foo }}_1': 1, 'key_2': '{{ foo 
}}_2'}, {'foo': 'bar'}),
-            {'key_{{ foo }}_1': 1, 'key_2': 'bar_2'}
-        )
-
-    def test_render_template_date_field(self):
-        """Tests if render_template from a date field works"""
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        self.assertEqual(
-            task.render_template('', datetime.date(2018, 12, 6), {'foo': 
'bar'}),
-            datetime.date(2018, 12, 6)
-        )
-
-    def test_render_template_datetime_field(self):
-        """Tests if render_template from a datetime field works"""
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        self.assertEqual(
-            task.render_template('', datetime.datetime(2018, 12, 6, 10, 55), 
{'foo': 'bar'}),
-            datetime.datetime(2018, 12, 6, 10, 55)
-        )
-
-    def test_render_template_UUID_field(self):
-        """Tests if render_template from a UUID field works"""
-
-        dag = DAG('test-dag',
-                  start_date=DEFAULT_DATE)
-
-        with dag:
-            task = DummyOperator(task_id='op1')
-
-        random_uuid = uuid.uuid4()
-        self.assertIs(
-            task.render_template('', random_uuid, {'foo': 'bar'}),
-            random_uuid
-        )
-
 
 Review comment:
   Look at all these deletions 😍 

----------------------------------------------------------------
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

Reply via email to