summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-03-21 20:44:34 -0400
committerTim Graham <timograham@gmail.com>2014-03-22 07:56:48 -0400
commitee4edb1eda2ac8f09eb298929282b44776930c89 (patch)
tree385d5f30d927069256aa57d0b44ac377ac06dfcc /tests
parent1c8dbb0cc268c6a2edc8268776b440f64867e6fb (diff)
Made ModelForms raise ImproperlyConfigured if the list of fields is not specified.
Also applies to modelform(set)_factory and generic model views. refs #19733.
Diffstat (limited to 'tests')
-rw-r--r--tests/generic_views/test_edit.py38
-rw-r--r--tests/model_forms/tests.py33
-rw-r--r--tests/model_formsets/tests.py10
3 files changed, 35 insertions, 46 deletions
diff --git a/tests/generic_views/test_edit.py b/tests/generic_views/test_edit.py
index d7982685c0..86a02603f6 100644
--- a/tests/generic_views/test_edit.py
+++ b/tests/generic_views/test_edit.py
@@ -1,6 +1,5 @@
from __future__ import unicode_literals
-import warnings
from unittest import expectedFailure
from django.core.exceptions import ImproperlyConfigured
@@ -8,7 +7,6 @@ from django.core.urlresolvers import reverse
from django import forms
from django.test import TestCase
from django.test.client import RequestFactory
-from django.utils.deprecation import RemovedInDjango18Warning
from django.views.generic.base import View
from django.views.generic.edit import FormMixin, ModelFormMixin, CreateView
@@ -151,33 +149,23 @@ class CreateViewTests(TestCase):
['name'])
def test_create_view_all_fields(self):
+ class MyCreateView(CreateView):
+ model = Author
+ fields = '__all__'
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("always", RemovedInDjango18Warning)
-
- class MyCreateView(CreateView):
- model = Author
- fields = '__all__'
-
- self.assertEqual(list(MyCreateView().get_form_class().base_fields),
- ['name', 'slug'])
- self.assertEqual(len(w), 0)
+ self.assertEqual(list(MyCreateView().get_form_class().base_fields),
+ ['name', 'slug'])
def test_create_view_without_explicit_fields(self):
+ class MyCreateView(CreateView):
+ model = Author
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("always", RemovedInDjango18Warning)
-
- class MyCreateView(CreateView):
- model = Author
-
- # Until end of the deprecation cycle, should still create the form
- # as before:
- self.assertEqual(list(MyCreateView().get_form_class().base_fields),
- ['name', 'slug'])
-
- # but with a warning:
- self.assertEqual(w[0].category, RemovedInDjango18Warning)
+ message = (
+ "Using ModelFormMixin (base class of MyCreateView) without the "
+ "'fields' attribute is prohibited."
+ )
+ with self.assertRaisesMessage(ImproperlyConfigured, message):
+ MyCreateView().get_form_class()
class UpdateViewTests(TestCase):
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 58ed7cc0fb..c19461ac25 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -4,10 +4,9 @@ import datetime
import os
from decimal import Decimal
from unittest import skipUnless
-import warnings
from django import forms
-from django.core.exceptions import FieldError, NON_FIELD_ERRORS
+from django.core.exceptions import FieldError, ImproperlyConfigured, NON_FIELD_ERRORS
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.validators import ValidationError
from django.db import connection
@@ -15,7 +14,6 @@ from django.db.models.query import EmptyQuerySet
from django.forms.models import (construct_instance, fields_for_model,
model_to_dict, modelform_factory, ModelFormMetaclass)
from django.test import TestCase, skipUnlessDBFeature
-from django.utils.deprecation import RemovedInDjango18Warning
from django.utils._os import upath
from django.utils import six
@@ -202,24 +200,16 @@ class ModelFormBaseTest(TestCase):
self.assertEqual(instance.name, '')
def test_missing_fields_attribute(self):
- with warnings.catch_warnings(record=True):
- warnings.simplefilter("always", RemovedInDjango18Warning)
-
+ message = (
+ "Creating a ModelForm without either the 'fields' attribute "
+ "or the 'exclude' attribute is prohibited; form "
+ "MissingFieldsForm needs updating."
+ )
+ with self.assertRaisesMessage(ImproperlyConfigured, message):
class MissingFieldsForm(forms.ModelForm):
class Meta:
model = Category
- # There is some internal state in warnings module which means that
- # if a warning has been seen already, the catch_warnings won't
- # have recorded it. The following line therefore will not work reliably:
-
- # self.assertEqual(w[0].category, RemovedInDjango18Warning)
-
- # Until end of the deprecation cycle, should still create the
- # form as before:
- self.assertEqual(list(MissingFieldsForm.base_fields),
- ['name', 'slug', 'url'])
-
def test_extra_fields(self):
class ExtraFields(BaseCategoryForm):
some_extra_field = forms.BooleanField()
@@ -2329,11 +2319,12 @@ class FormFieldCallbackTests(TestCase):
def test_modelform_factory_without_fields(self):
""" Regression for #19733 """
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("always", RemovedInDjango18Warning)
- # This should become an error once deprecation cycle is complete.
+ message = (
+ "Calling modelform_factory without defining 'fields' or 'exclude' "
+ "explicitly is prohibited."
+ )
+ with self.assertRaisesMessage(ImproperlyConfigured, message):
modelform_factory(Person)
- self.assertEqual(w[0].category, RemovedInDjango18Warning)
def test_modelform_factory_with_all_fields(self):
""" Regression for #19733 """
diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
index c1e01cd6be..4c7d2a535e 100644
--- a/tests/model_formsets/tests.py
+++ b/tests/model_formsets/tests.py
@@ -6,6 +6,7 @@ from datetime import date
from decimal import Decimal
from django import forms
+from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.forms.models import (_get_foreign_key, inlineformset_factory,
modelformset_factory, BaseModelFormSet)
@@ -131,6 +132,15 @@ class DeletionTests(TestCase):
class ModelFormsetTest(TestCase):
+ def test_modelformset_factory_without_fields(self):
+ """ Regression for #19733 """
+ message = (
+ "Calling modelformset_factory without defining 'fields' or 'exclude' "
+ "explicitly is prohibited."
+ )
+ with self.assertRaisesMessage(ImproperlyConfigured, message):
+ modelformset_factory(Author)
+
def test_simple_save(self):
qs = Author.objects.all()
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=3)