ashb commented on a change in pull request #6788: [AIRFLOW-5944] Rendering 
template_fields without accessing DAG files
URL: https://github.com/apache/airflow/pull/6788#discussion_r389587265
 
 

 ##########
 File path: tests/models/test_renderedtifields.py
 ##########
 @@ -0,0 +1,221 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Unit tests for RenderedTaskInstanceFields."""
+
+import unittest
+from datetime import date, timedelta
+
+from parameterized import parameterized
+
+from airflow import settings
+from airflow.models import Variable
+from airflow.models.dag import DAG
+from airflow.models.renderedtifields import RenderedTaskInstanceFields as RTIF
+from airflow.models.taskinstance import TaskInstance as TI
+from airflow.operators.bash import BashOperator
+from airflow.utils.session import create_session
+from airflow.utils.timezone import datetime
+
+TEST_DAG = DAG("example_rendered_ti_field", schedule_interval=None)
+START_DATE = datetime(2018, 1, 1)
+EXECUTION_DATE = datetime(2019, 1, 1)
+
+
+def clear_rendered_ti_fields():
+    with create_session() as session:
+        session.query(RTIF).delete()
+
+
+class ClassWithCustomAttributes:
+    """Class for testing purpose: allows to create objects with custom 
attributes in one single statement."""
+
+    def __init__(self, **kwargs):
+        for key, value in kwargs.items():
+            setattr(self, key, value)
+
+    def __str__(self):
+        return "{}({})".format(ClassWithCustomAttributes.__name__, 
str(self.__dict__))
+
+    def __repr__(self):
+        return self.__str__()
+
+    def __eq__(self, other):
+        return self.__dict__ == other.__dict__
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+
+class TestRenderedTaskInstanceFields(unittest.TestCase):
+    """Unit tests for RenderedTaskInstanceFields."""
+
+    def setUp(self):
+        clear_rendered_ti_fields()
+
+    def tearDown(self):
+        clear_rendered_ti_fields()
+
+    @parameterized.expand([
+        (None, None),
+        ([], []),
+        ({}, {}),
+        ("test-string", "test-string"),
+        ({"foo": "bar"}, {"foo": "bar"}),
+        ("{{ task.task_id }}", "test"),
+        (date(2018, 12, 6), "2018-12-06"),
+        (datetime(2018, 12, 6, 10, 55), "2018-12-06 10:55:00+00:00"),
+        (
+            ClassWithCustomAttributes(
+                att1="{{ task.task_id }}", att2="{{ task.task_id }}", 
template_fields=["att1"]),
+            str(ClassWithCustomAttributes(
+                att1="test", att2="{{ task.task_id }}", 
template_fields=["att1"])),
+        ),
+        (
+            
ClassWithCustomAttributes(nested1=ClassWithCustomAttributes(att1="{{ 
task.task_id }}",
+                                                                        
att2="{{ task.task_id }}",
+                                                                        
template_fields=["att1"]),
+                                      
nested2=ClassWithCustomAttributes(att3="{{ task.task_id }}",
+                                                                        
att4="{{ task.task_id }}",
+                                                                        
template_fields=["att3"]),
+                                      template_fields=["nested1"]),
+            
str(ClassWithCustomAttributes(nested1=ClassWithCustomAttributes(att1="test",
 
 Review comment:
   Can you hard-code this string please? This is otherwise not really testing 
that the output is what we expect.

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