def test_render_template_field_undefined_strict(self): """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 pytest.raises(jinja2.UndefinedError): task.render_template("{{ foo }}", {})
def test_jinja_invalid_expression_is_just_propagated(self): """Test render_template propagates Jinja invalid expression errors.""" with DAG("test-dag", start_date=DEFAULT_DATE): task = DummyOperator(task_id="op1") with pytest.raises(jinja2.exceptions.TemplateSyntaxError): task.render_template("{{ invalid expression }}", {})
def test_render_template_field_undefined_default(self): """Test render_template with template_undefined unchanged.""" with DAG("test-dag", start_date=DEFAULT_DATE): task = DummyOperator(task_id="op1") with pytest.raises(jinja2.UndefinedError): task.render_template("{{ foo }}", {})
def test_nested_template_fields_declared_must_exist(self): """Test render_template when a nested template field is missing.""" with DAG("test-dag", start_date=DEFAULT_DATE): task = DummyOperator(task_id="op1") with pytest.raises(AttributeError) as ctx: task.render_template(ClassWithCustomAttributes(template_fields=["missing_field"]), {}) assert "'ClassWithCustomAttributes' object has no attribute 'missing_field'" == str(ctx.value)
def test_nested_template_fields_declared_must_exist(self): """Test render_template when a nested template field is missing.""" with DAG("test-dag", start_date=DEFAULT_DATE): task = DummyOperator(task_id="op1") with self.assertRaises(AttributeError) as e: task.render_template( ClassWithCustomAttributes(template_fields=["missing_field"]), {}) self.assertEqual( "'ClassWithCustomAttributes' object has no attribute 'missing_field'", str(e.exception))
def test_render_template_fields_no_change(self, content): """Tests if non-templatable types remain unchanged.""" with DAG("test-dag", start_date=DEFAULT_DATE): task = DummyOperator(task_id="op1") result = task.render_template(content, {"foo": "bar"}) assert content == result
def test_render_template(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(content, context) assert result == expected_output
def test_override_jinja_env_option(self): """Test render_template given various input types.""" with DAG("test-dag", start_date=DEFAULT_DATE, jinja_environment_kwargs={'cache_size': 50}): task = DummyOperator(task_id="op1") result = task.render_template("{{ foo }}", {"foo": "bar"}) assert result == "bar"
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") result = task.render_template(content, context) assert result == expected_output
def test_render_template_field_undefined_not_strict(self): """Test render_template with template_undefined configured to silently error.""" with DAG("test-dag", start_date=DEFAULT_DATE, template_undefined=jinja2.Undefined): task = DummyOperator(task_id="op1") assert task.render_template("{{ foo }}", {}) == ""
def test_set_jinja_env_additional_option(self): """Test render_template given various input types.""" with DAG("test-dag", start_date=DEFAULT_DATE, jinja_environment_kwargs={'keep_trailing_newline': True}): task = DummyOperator(task_id="op1") result = task.render_template("{{ foo }}\n\n", {"foo": "bar"}) assert result == "bar\n\n"
def test_render_template_with_native_envs(self, content, context, expected_output): """Test render_template given various input types with Native Python types""" with DAG("test-dag", start_date=DEFAULT_DATE, render_template_as_native_obj=True): task = DummyOperator(task_id="op1") result = task.render_template(content, context) assert result == expected_output