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

 ##########
 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:
   Yep that looks much cleaner, plus parametrized them as well. Also moved to 
`tests/models/test_taskinstance.py`, but let me know if it belongs elsewhere or 
we want to keep it in core.

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