From bc1fa8ebcd9adbe969261b07337d1cad4852edfb Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 21 Sep 2021 10:16:44 +0200 Subject: [PATCH] [4.0.x] Refs #31026 -- Fixed forms_tests if Jinja2 is not installed. Backport of 881a4799114fccefbc0f56c6524110ede2682e16 from main --- tests/forms_tests/tests/__init__.py | 36 ++++++++---------------- tests/forms_tests/tests/test_forms.py | 8 ++++-- tests/forms_tests/tests/test_formsets.py | 20 ++++++++++--- tests/forms_tests/tests/test_i18n.py | 8 ++++-- tests/forms_tests/tests/tests.py | 8 ++++-- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/tests/forms_tests/tests/__init__.py b/tests/forms_tests/tests/__init__.py index 7fc0269ec5..3d1d8eefa4 100644 --- a/tests/forms_tests/tests/__init__.py +++ b/tests/forms_tests/tests/__init__.py @@ -1,30 +1,16 @@ -import inspect +from unittest import skipIf from django.test.utils import override_settings -TEST_SETTINGS = [ - { - 'FORM_RENDERER': 'django.forms.renderers.DjangoTemplates', - 'TEMPLATES': {'BACKEND': 'django.template.backends.django.DjangoTemplates'}, - }, - { - 'FORM_RENDERER': 'django.forms.renderers.Jinja2', - 'TEMPLATES': {'BACKEND': 'django.template.backends.jinja2.Jinja2'}, - }, -] +try: + import jinja2 +except ImportError: + jinja2 = None -def test_all_form_renderers(): - def wrapper(func): - def inner(*args, **kwargs): - for settings in TEST_SETTINGS: - with override_settings(**settings): - func(*args, **kwargs) - return inner - - def decorator(cls): - for name, func in inspect.getmembers(cls, inspect.isfunction): - if name.startswith('test_'): - setattr(cls, name, wrapper(func)) - return cls - return decorator +def jinja2_tests(test_func): + test_func = skipIf(jinja2 is None, 'this test requires jinja2')(test_func) + return override_settings( + FORM_RENDERER='django.forms.renderers.Jinja2', + TEMPLATES={'BACKEND': 'django.template.backends.jinja2.Jinja2'}, + )(test_func) diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 8c08cd5ad4..9af103dbd8 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -23,7 +23,7 @@ from django.test import SimpleTestCase from django.test.utils import override_settings from django.utils.datastructures import MultiValueDict from django.utils.safestring import mark_safe -from tests.forms_tests.tests import test_all_form_renderers +from tests.forms_tests.tests import jinja2_tests class FrameworkForm(Form): @@ -56,7 +56,6 @@ class MultiValueDictLike(dict): return [self[key]] -@test_all_form_renderers() class FormsTestCase(SimpleTestCase): # A Form is a collection of Fields. It knows how to validate a set of data and it # knows how to render itself in a couple of default ways (e.g., an HTML table). @@ -3541,6 +3540,11 @@ Password: ) +@jinja2_tests +class Jinja2FormsTestCase(FormsTestCase): + pass + + class CustomRenderer(DjangoTemplates): pass diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index 0d7d452d71..329b513f3a 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -11,7 +11,7 @@ from django.forms.formsets import BaseFormSet, all_valid, formset_factory from django.forms.utils import ErrorList from django.forms.widgets import HiddenInput from django.test import SimpleTestCase -from tests.forms_tests.tests import test_all_form_renderers +from tests.forms_tests.tests import jinja2_tests class Choice(Form): @@ -48,7 +48,6 @@ class CustomKwargForm(Form): super().__init__(*args, **kwargs) -@test_all_form_renderers() class FormsFormsetTestCase(SimpleTestCase): def make_choiceformset( @@ -1315,7 +1314,11 @@ class FormsFormsetTestCase(SimpleTestCase): self.assertEqual(formset.empty_form.renderer, renderer) -@test_all_form_renderers() +@jinja2_tests +class Jinja2FormsFormsetTestCase(FormsFormsetTestCase): + pass + + class FormsetAsTagTests(SimpleTestCase): def setUp(self): data = { @@ -1364,6 +1367,11 @@ class FormsetAsTagTests(SimpleTestCase): ) +@jinja2_tests +class Jinja2FormsetAsTagTests(FormsetAsTagTests): + pass + + class ArticleForm(Form): title = CharField() pub_date = DateField() @@ -1372,7 +1380,6 @@ class ArticleForm(Form): ArticleFormSet = formset_factory(ArticleForm) -@test_all_form_renderers() class TestIsBoundBehavior(SimpleTestCase): def test_no_data_error(self): formset = ArticleFormSet({}) @@ -1485,6 +1492,11 @@ class TestIsBoundBehavior(SimpleTestCase): self.assertHTMLEqual(empty_forms[0].as_p(), empty_forms[1].as_p()) +@jinja2_tests +class TestIsBoundBehavior(TestIsBoundBehavior): + pass + + class TestEmptyFormSet(SimpleTestCase): def test_empty_formset_is_valid(self): """An empty formset still calls clean()""" diff --git a/tests/forms_tests/tests/test_i18n.py b/tests/forms_tests/tests/test_i18n.py index fc5d59c480..38840e748e 100644 --- a/tests/forms_tests/tests/test_i18n.py +++ b/tests/forms_tests/tests/test_i18n.py @@ -4,10 +4,9 @@ from django.forms import ( from django.test import SimpleTestCase from django.utils import translation from django.utils.translation import gettext_lazy -from tests.forms_tests.tests import test_all_form_renderers +from tests.forms_tests.tests import jinja2_tests -@test_all_form_renderers() class FormsI18nTests(SimpleTestCase): def test_lazy_labels(self): class SomeForm(Form): @@ -95,3 +94,8 @@ class FormsI18nTests(SimpleTestCase): degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),))) CopyForm() + + +@jinja2_tests +class Jinja2FormsI18nTests(FormsI18nTests): + pass diff --git a/tests/forms_tests/tests/tests.py b/tests/forms_tests/tests/tests.py index ec08a5f9ef..819c2481a5 100644 --- a/tests/forms_tests/tests/tests.py +++ b/tests/forms_tests/tests/tests.py @@ -5,7 +5,7 @@ from django.db import models from django.forms import CharField, FileField, Form, ModelForm from django.forms.models import ModelFormMetaclass from django.test import SimpleTestCase, TestCase -from tests.forms_tests.tests import test_all_form_renderers +from tests.forms_tests.tests import jinja2_tests from ..models import ( BoundaryModel, ChoiceFieldModel, ChoiceModel, ChoiceOptionModel, Defaults, @@ -284,7 +284,6 @@ class ManyToManyExclusionTestCase(TestCase): self.assertEqual([obj.pk for obj in form.instance.multi_choice_int.all()], data['multi_choice_int']) -@test_all_form_renderers() class EmptyLabelTestCase(TestCase): def test_empty_field_char(self): f = EmptyCharLabelChoiceForm() @@ -374,3 +373,8 @@ class EmptyLabelTestCase(TestCase):

""" ) + + +@jinja2_tests +class Jinja2EmptyLabelTestCase(EmptyLabelTestCase): + pass