Silenced all admin validation warnings.

Warnings could escape depending on the order in which tests were run.
This commit is contained in:
Tim Graham 2014-11-27 09:45:24 -05:00
parent 17fe0bd808
commit 8402909876
1 changed files with 55 additions and 37 deletions

View File

@ -73,19 +73,23 @@ class ValidationTestCase(TestCase):
class ExcludedFields1(admin.ModelAdmin): class ExcludedFields1(admin.ModelAdmin):
exclude = ('foo') exclude = ('foo')
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"'ExcludedFields1.exclude' must be a list or tuple.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
ExcludedFields1.validate, self.assertRaisesMessage(ImproperlyConfigured,
Book) "'ExcludedFields1.exclude' must be a list or tuple.",
ExcludedFields1.validate,
Book)
def test_exclude_duplicate_values(self): def test_exclude_duplicate_values(self):
class ExcludedFields2(admin.ModelAdmin): class ExcludedFields2(admin.ModelAdmin):
exclude = ('name', 'name') exclude = ('name', 'name')
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"There are duplicate field(s) in ExcludedFields2.exclude", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
ExcludedFields2.validate, self.assertRaisesMessage(ImproperlyConfigured,
Book) "There are duplicate field(s) in ExcludedFields2.exclude",
ExcludedFields2.validate,
Book)
def test_exclude_in_inline(self): def test_exclude_in_inline(self):
class ExcludedFieldsInline(admin.TabularInline): class ExcludedFieldsInline(admin.TabularInline):
@ -96,10 +100,12 @@ class ValidationTestCase(TestCase):
model = Album model = Album
inlines = [ExcludedFieldsInline] inlines = [ExcludedFieldsInline]
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"'ExcludedFieldsInline.exclude' must be a list or tuple.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
ExcludedFieldsAlbumAdmin.validate, self.assertRaisesMessage(ImproperlyConfigured,
Album) "'ExcludedFieldsInline.exclude' must be a list or tuple.",
ExcludedFieldsAlbumAdmin.validate,
Album)
def test_exclude_inline_model_admin(self): def test_exclude_inline_model_admin(self):
""" """
@ -114,10 +120,12 @@ class ValidationTestCase(TestCase):
model = Album model = Album
inlines = [SongInline] inlines = [SongInline]
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
AlbumAdmin.validate, self.assertRaisesMessage(ImproperlyConfigured,
Album) "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
AlbumAdmin.validate,
Album)
def test_app_label_in_admin_validation(self): def test_app_label_in_admin_validation(self):
""" """
@ -158,9 +166,11 @@ class ValidationTestCase(TestCase):
class MyAdmin(admin.ModelAdmin): class MyAdmin(admin.ModelAdmin):
inlines = [TwoAlbumFKAndAnEInline] inlines = [TwoAlbumFKAndAnEInline]
self.assertRaisesMessage(ValueError, with warnings.catch_warnings(record=True):
"'admin_validation.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_validation.Album'.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
MyAdmin.validate, Album) self.assertRaisesMessage(ValueError,
"'admin_validation.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_validation.Album'.",
MyAdmin.validate, Album)
def test_inline_with_specified(self): def test_inline_with_specified(self):
class TwoAlbumFKAndAnEInline(admin.TabularInline): class TwoAlbumFKAndAnEInline(admin.TabularInline):
@ -216,22 +226,26 @@ class ValidationTestCase(TestCase):
class SongAdmin(admin.ModelAdmin): class SongAdmin(admin.ModelAdmin):
readonly_fields = ("title", "nonexistent") readonly_fields = ("title", "nonexistent")
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistent' is not a callable " warnings.filterwarnings('ignore', module='django.contrib.admin.options')
"or an attribute of 'SongAdmin' or found in the model 'Song'."), self.assertRaisesMessage(ImproperlyConfigured,
SongAdmin.validate, str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistent' is not a callable "
Song) "or an attribute of 'SongAdmin' or found in the model 'Song'."),
SongAdmin.validate,
Song)
def test_nonexistent_field_on_inline(self): def test_nonexistent_field_on_inline(self):
class CityInline(admin.TabularInline): class CityInline(admin.TabularInline):
model = City model = City
readonly_fields = ['i_dont_exist'] # Missing attribute readonly_fields = ['i_dont_exist'] # Missing attribute
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable " warnings.filterwarnings('ignore', module='django.contrib.admin.options')
"or an attribute of 'CityInline' or found in the model 'City'."), self.assertRaisesMessage(ImproperlyConfigured,
CityInline.validate, str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable "
City) "or an attribute of 'CityInline' or found in the model 'City'."),
CityInline.validate,
City)
def test_extra(self): def test_extra(self):
class SongAdmin(admin.ModelAdmin): class SongAdmin(admin.ModelAdmin):
@ -262,10 +276,12 @@ class ValidationTestCase(TestCase):
class BookAdmin(admin.ModelAdmin): class BookAdmin(admin.ModelAdmin):
fields = ['authors'] fields = ['authors']
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
BookAdmin.validate, self.assertRaisesMessage(ImproperlyConfigured,
Book) "'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
BookAdmin.validate,
Book)
def test_cannot_include_through(self): def test_cannot_include_through(self):
class FieldsetBookAdmin(admin.ModelAdmin): class FieldsetBookAdmin(admin.ModelAdmin):
@ -274,10 +290,12 @@ class ValidationTestCase(TestCase):
('Header 2', {'fields': ('authors',)}), ('Header 2', {'fields': ('authors',)}),
) )
self.assertRaisesMessage(ImproperlyConfigured, with warnings.catch_warnings(record=True):
"'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.", warnings.filterwarnings('ignore', module='django.contrib.admin.options')
FieldsetBookAdmin.validate, self.assertRaisesMessage(ImproperlyConfigured,
Book) "'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
FieldsetBookAdmin.validate,
Book)
def test_nested_fields(self): def test_nested_fields(self):
class NestedFieldsAdmin(admin.ModelAdmin): class NestedFieldsAdmin(admin.ModelAdmin):