Refs #31026 -- Fixed forms_tests if Jinja2 is not installed.

This commit is contained in:
Mariusz Felisiak 2021-09-21 10:16:44 +02:00 committed by GitHub
parent 1d16dbc745
commit 881a479911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 35 deletions

View File

@ -1,30 +1,16 @@
import inspect from unittest import skipIf
from django.test.utils import override_settings from django.test.utils import override_settings
TEST_SETTINGS = [ try:
{ import jinja2
'FORM_RENDERER': 'django.forms.renderers.DjangoTemplates', except ImportError:
'TEMPLATES': {'BACKEND': 'django.template.backends.django.DjangoTemplates'}, jinja2 = None
},
{
'FORM_RENDERER': 'django.forms.renderers.Jinja2',
'TEMPLATES': {'BACKEND': 'django.template.backends.jinja2.Jinja2'},
},
]
def test_all_form_renderers(): def jinja2_tests(test_func):
def wrapper(func): test_func = skipIf(jinja2 is None, 'this test requires jinja2')(test_func)
def inner(*args, **kwargs): return override_settings(
for settings in TEST_SETTINGS: FORM_RENDERER='django.forms.renderers.Jinja2',
with override_settings(**settings): TEMPLATES={'BACKEND': 'django.template.backends.jinja2.Jinja2'},
func(*args, **kwargs) )(test_func)
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

View File

@ -23,7 +23,7 @@ from django.test import SimpleTestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.safestring import mark_safe 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): class FrameworkForm(Form):
@ -56,7 +56,6 @@ class MultiValueDictLike(dict):
return [self[key]] return [self[key]]
@test_all_form_renderers()
class FormsTestCase(SimpleTestCase): class FormsTestCase(SimpleTestCase):
# A Form is a collection of Fields. It knows how to validate a set of data and it # 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). # knows how to render itself in a couple of default ways (e.g., an HTML table).
@ -3541,6 +3540,11 @@ Password: <input type="password" name="password" required>
) )
@jinja2_tests
class Jinja2FormsTestCase(FormsTestCase):
pass
class CustomRenderer(DjangoTemplates): class CustomRenderer(DjangoTemplates):
pass pass

View File

@ -11,7 +11,7 @@ from django.forms.formsets import BaseFormSet, all_valid, formset_factory
from django.forms.utils import ErrorList from django.forms.utils import ErrorList
from django.forms.widgets import HiddenInput from django.forms.widgets import HiddenInput
from django.test import SimpleTestCase 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): class Choice(Form):
@ -48,7 +48,6 @@ class CustomKwargForm(Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@test_all_form_renderers()
class FormsFormsetTestCase(SimpleTestCase): class FormsFormsetTestCase(SimpleTestCase):
def make_choiceformset( def make_choiceformset(
@ -1315,7 +1314,11 @@ class FormsFormsetTestCase(SimpleTestCase):
self.assertEqual(formset.empty_form.renderer, renderer) self.assertEqual(formset.empty_form.renderer, renderer)
@test_all_form_renderers() @jinja2_tests
class Jinja2FormsFormsetTestCase(FormsFormsetTestCase):
pass
class FormsetAsTagTests(SimpleTestCase): class FormsetAsTagTests(SimpleTestCase):
def setUp(self): def setUp(self):
data = { data = {
@ -1364,6 +1367,11 @@ class FormsetAsTagTests(SimpleTestCase):
) )
@jinja2_tests
class Jinja2FormsetAsTagTests(FormsetAsTagTests):
pass
class ArticleForm(Form): class ArticleForm(Form):
title = CharField() title = CharField()
pub_date = DateField() pub_date = DateField()
@ -1372,7 +1380,6 @@ class ArticleForm(Form):
ArticleFormSet = formset_factory(ArticleForm) ArticleFormSet = formset_factory(ArticleForm)
@test_all_form_renderers()
class TestIsBoundBehavior(SimpleTestCase): class TestIsBoundBehavior(SimpleTestCase):
def test_no_data_error(self): def test_no_data_error(self):
formset = ArticleFormSet({}) formset = ArticleFormSet({})
@ -1485,6 +1492,11 @@ class TestIsBoundBehavior(SimpleTestCase):
self.assertHTMLEqual(empty_forms[0].as_p(), empty_forms[1].as_p()) self.assertHTMLEqual(empty_forms[0].as_p(), empty_forms[1].as_p())
@jinja2_tests
class TestIsBoundBehavior(TestIsBoundBehavior):
pass
class TestEmptyFormSet(SimpleTestCase): class TestEmptyFormSet(SimpleTestCase):
def test_empty_formset_is_valid(self): def test_empty_formset_is_valid(self):
"""An empty formset still calls clean()""" """An empty formset still calls clean()"""

View File

@ -4,10 +4,9 @@ from django.forms import (
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.utils import translation from django.utils import translation
from django.utils.translation import gettext_lazy 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): class FormsI18nTests(SimpleTestCase):
def test_lazy_labels(self): def test_lazy_labels(self):
class SomeForm(Form): class SomeForm(Form):
@ -95,3 +94,8 @@ class FormsI18nTests(SimpleTestCase):
degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),))) degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),)))
CopyForm() CopyForm()
@jinja2_tests
class Jinja2FormsI18nTests(FormsI18nTests):
pass

View File

@ -5,7 +5,7 @@ from django.db import models
from django.forms import CharField, FileField, Form, ModelForm from django.forms import CharField, FileField, Form, ModelForm
from django.forms.models import ModelFormMetaclass from django.forms.models import ModelFormMetaclass
from django.test import SimpleTestCase, TestCase 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 ( from ..models import (
BoundaryModel, ChoiceFieldModel, ChoiceModel, ChoiceOptionModel, Defaults, 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']) self.assertEqual([obj.pk for obj in form.instance.multi_choice_int.all()], data['multi_choice_int'])
@test_all_form_renderers()
class EmptyLabelTestCase(TestCase): class EmptyLabelTestCase(TestCase):
def test_empty_field_char(self): def test_empty_field_char(self):
f = EmptyCharLabelChoiceForm() f = EmptyCharLabelChoiceForm()
@ -374,3 +373,8 @@ class EmptyLabelTestCase(TestCase):
<option value="2">Bar</option> <option value="2">Bar</option>
</select></p>""" </select></p>"""
) )
@jinja2_tests
class Jinja2EmptyLabelTestCase(EmptyLabelTestCase):
pass