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

View File

@ -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: <input type="password" name="password" required>
)
@jinja2_tests
class Jinja2FormsTestCase(FormsTestCase):
pass
class CustomRenderer(DjangoTemplates):
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.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()"""

View File

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

View File

@ -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):
<option value="2">Bar</option>
</select></p>"""
)
@jinja2_tests
class Jinja2EmptyLabelTestCase(EmptyLabelTestCase):
pass