|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +import datetime |
| 21 | +import unittest |
| 22 | +from unittest import mock |
| 23 | +import uuid |
| 24 | +from collections import namedtuple |
| 25 | + |
| 26 | +import jinja2 |
| 27 | +from parameterized import parameterized |
| 28 | + |
| 29 | +from airflow.models import DAG, BaseOperator |
| 30 | +from airflow.operators.dummy_operator import DummyOperator |
| 31 | +from airflow.utils.decorators import apply_defaults |
| 32 | +from tests.models import DEFAULT_DATE |
| 33 | + |
| 34 | + |
| 35 | +class TestOperator(BaseOperator): |
| 36 | + """Operator for testing purposes.""" |
| 37 | + |
| 38 | + template_fields = ("arg1", "arg2") |
| 39 | + |
| 40 | + @apply_defaults |
| 41 | + def __init__(self, arg1: str = "", arg2: str = "", **kwargs): |
| 42 | + super().__init__(**kwargs) |
| 43 | + self.arg1 = arg1 |
| 44 | + self.arg2 = arg2 |
| 45 | + |
| 46 | + def execute(self, context): |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +# Namedtuple for testing purposes |
| 51 | +TestNamedTuple = namedtuple("TestNamedTuple", ["var1", "var2"]) |
| 52 | + |
| 53 | + |
| 54 | +class BaseOperatorTest(unittest.TestCase): |
| 55 | + @parameterized.expand( |
| 56 | + [ |
| 57 | + ("{{ foo }}", {"foo": "bar"}, "bar"), |
| 58 | + ("{{ foo }}", {}, ""), |
| 59 | + (["{{ foo }}_1", "{{ foo }}_2"], {"foo": "bar"}, ["bar_1", "bar_2"]), |
| 60 | + (("{{ foo }}_1", "{{ foo }}_2"), {"foo": "bar"}, ("bar_1", "bar_2")), |
| 61 | + ( |
| 62 | + {"key1": "{{ foo }}_1", "key2": "{{ foo }}_2"}, |
| 63 | + {"foo": "bar"}, |
| 64 | + {"key1": "bar_1", "key2": "bar_2"}, |
| 65 | + ), |
| 66 | + ( |
| 67 | + {"key_{{ foo }}_1": 1, "key_2": "{{ foo }}_2"}, |
| 68 | + {"foo": "bar"}, |
| 69 | + {"key_{{ foo }}_1": 1, "key_2": "bar_2"}, |
| 70 | + ), |
| 71 | + (datetime.date(2018, 12, 6), {"foo": "bar"}, datetime.date(2018, 12, 6)), |
| 72 | + (datetime.datetime(2018, 12, 6, 10, 55), {"foo": "bar"}, datetime.datetime(2018, 12, 6, 10, 55)), |
| 73 | + (TestNamedTuple("{{ foo }}_1", "{{ foo }}_2"), {"foo": "bar"}, TestNamedTuple("bar_1", "bar_2")), |
| 74 | + ({"{{ foo }}_1", "{{ foo }}_2"}, {"foo": "bar"}, {"bar_1", "bar_2"}), |
| 75 | + ] |
| 76 | + ) |
| 77 | + def test_render_template(self, content, context, expected_output): |
| 78 | + """Test render_template given various input types.""" |
| 79 | + with DAG("test-dag", start_date=DEFAULT_DATE): |
| 80 | + task = DummyOperator(task_id="op1") |
| 81 | + |
| 82 | + result = task.render_template(content, context) |
| 83 | + self.assertEqual(result, expected_output) |
| 84 | + |
| 85 | + def test_render_template_fields(self): |
| 86 | + """Verify if operator attributes are correctly templated.""" |
| 87 | + with DAG("test-dag", start_date=DEFAULT_DATE): |
| 88 | + task = TestOperator(task_id="op1", arg1="{{ foo }}", arg2="{{ bar }}") |
| 89 | + |
| 90 | + # Assert nothing is templated yet |
| 91 | + self.assertEqual(task.arg1, "{{ foo }}") |
| 92 | + self.assertEqual(task.arg2, "{{ bar }}") |
| 93 | + |
| 94 | + # Trigger templating and verify if attributes are templated correctly |
| 95 | + task.render_template_fields(context={"foo": "footemplated", "bar": "bartemplated"}) |
| 96 | + self.assertEqual(task.arg1, "footemplated") |
| 97 | + self.assertEqual(task.arg2, "bartemplated") |
| 98 | + |
| 99 | + @parameterized.expand( |
| 100 | + [ |
| 101 | + ({"user_defined_macros": {"foo": "bar"}}, "{{ foo }}", {}, "bar"), |
| 102 | + ({"user_defined_macros": {"foo": "bar"}}, 1, {}, 1), |
| 103 | + ( |
| 104 | + {"user_defined_filters": {"hello": lambda name: "Hello %s" % name}}, |
| 105 | + "{{ 'world' | hello }}", |
| 106 | + {}, |
| 107 | + "Hello world", |
| 108 | + ), |
| 109 | + ] |
| 110 | + ) |
| 111 | + def test_render_template_fields_with_dag_settings(self, dag_kwargs, content, context, expected_output): |
| 112 | + """Test render_template with additional DAG settings.""" |
| 113 | + with DAG("test-dag", start_date=DEFAULT_DATE, **dag_kwargs): |
| 114 | + task = DummyOperator(task_id="op1") |
| 115 | + |
| 116 | + result = task.render_template(content, context) |
| 117 | + self.assertEqual(result, expected_output) |
| 118 | + |
| 119 | + @parameterized.expand([(object(),), (uuid.uuid4(),)]) |
| 120 | + def test_render_template_fields_no_change(self, content): |
| 121 | + """Tests if non-templatable types remain unchanged.""" |
| 122 | + with DAG("test-dag", start_date=DEFAULT_DATE): |
| 123 | + task = DummyOperator(task_id="op1") |
| 124 | + |
| 125 | + result = task.render_template(content, {"foo": "bar"}) |
| 126 | + self.assertEqual(content, result) |
| 127 | + |
| 128 | + def test_render_template_field_undefined_strict(self): |
| 129 | + """Test render_template with template_undefined configured.""" |
| 130 | + with DAG("test-dag", start_date=DEFAULT_DATE, template_undefined=jinja2.StrictUndefined): |
| 131 | + task = DummyOperator(task_id="op1") |
| 132 | + |
| 133 | + with self.assertRaises(jinja2.UndefinedError): |
| 134 | + task.render_template("{{ foo }}", {}) |
| 135 | + |
| 136 | + @mock.patch("jinja2.Environment", autospec=True) |
| 137 | + def test_jinja_env_creation(self, mock_jinja_env): |
| 138 | + """Verify if a Jinja environment is created only once when templating.""" |
| 139 | + with DAG("test-dag", start_date=DEFAULT_DATE): |
| 140 | + task = TestOperator(task_id="op1", arg1="{{ foo }}", arg2="{{ bar }}") |
| 141 | + |
| 142 | + task.render_template_fields(context={"foo": "whatever", "bar": "whatever"}) |
| 143 | + self.assertEqual(mock_jinja_env.call_count, 1) |
0 commit comments