ashb commented on a change in pull request #6793: [AIRFLOW-1076] Add get method 
for template variable accessor
URL: https://github.com/apache/airflow/pull/6793#discussion_r357112125
 
 

 ##########
 File path: tests/test_core.py
 ##########
 @@ -588,6 +588,42 @@ def verify_templated_field(context):
         t.execute = verify_templated_field
         t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, 
ignore_ti_state=True)
 
+    def test_template_with_variable_get(self):
+        val = {
+            'test_value': 'a test value'
+        }
+        Variable.set("a_variable", val['test_value'])
+
+        def verify_templated_field(context):
+            self.assertEqual(context['ti'].task.some_templated_field,
+                             val['test_value'])
+
+        t = OperatorSubclass(
+            task_id='test_complex_template',
+            some_templated_field='{{ var.value.get("a_variable") }}',
+            dag=self.dag)
+        t.execute = verify_templated_field
+        t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, 
ignore_ti_state=True)
+
+    def test_template_with_variable_get_fallback(self):
+        def verify_templated_field(context):
+            self.assertEqual(context['ti'].task.some_templated_field, 
'fallback')
+
+        t = OperatorSubclass(
+            task_id='test_complex_template',
+            some_templated_field='{{ var.value.get("missing_variable", 
"fallback") }}',
+            dag=self.dag)
+        t.execute = verify_templated_field
+        t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, 
ignore_ti_state=True)
 
 Review comment:
   Running the operator is overkill (yes I see you copied this existing 
pattern). Instead of putting these tests here look at the tests in 
tests/models/test_baseoperator.py which calls `task.render_template` more 
directly.

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

Reply via email to