Skip to content

Commit 8402909

Browse files
committed
Silenced all admin validation warnings.
Warnings could escape depending on the order in which tests were run.
1 parent 17fe0bd commit 8402909

File tree

1 file changed

+55
-37
lines changed

1 file changed

+55
-37
lines changed

Diff for: tests/admin_validation/tests.py

+55-37
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,23 @@ def test_exclude_values(self):
7373
class ExcludedFields1(admin.ModelAdmin):
7474
exclude = ('foo')
7575

76-
self.assertRaisesMessage(ImproperlyConfigured,
77-
"'ExcludedFields1.exclude' must be a list or tuple.",
78-
ExcludedFields1.validate,
79-
Book)
76+
with warnings.catch_warnings(record=True):
77+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
78+
self.assertRaisesMessage(ImproperlyConfigured,
79+
"'ExcludedFields1.exclude' must be a list or tuple.",
80+
ExcludedFields1.validate,
81+
Book)
8082

8183
def test_exclude_duplicate_values(self):
8284
class ExcludedFields2(admin.ModelAdmin):
8385
exclude = ('name', 'name')
8486

85-
self.assertRaisesMessage(ImproperlyConfigured,
86-
"There are duplicate field(s) in ExcludedFields2.exclude",
87-
ExcludedFields2.validate,
88-
Book)
87+
with warnings.catch_warnings(record=True):
88+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
89+
self.assertRaisesMessage(ImproperlyConfigured,
90+
"There are duplicate field(s) in ExcludedFields2.exclude",
91+
ExcludedFields2.validate,
92+
Book)
8993

9094
def test_exclude_in_inline(self):
9195
class ExcludedFieldsInline(admin.TabularInline):
@@ -96,10 +100,12 @@ class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
96100
model = Album
97101
inlines = [ExcludedFieldsInline]
98102

99-
self.assertRaisesMessage(ImproperlyConfigured,
100-
"'ExcludedFieldsInline.exclude' must be a list or tuple.",
101-
ExcludedFieldsAlbumAdmin.validate,
102-
Album)
103+
with warnings.catch_warnings(record=True):
104+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
105+
self.assertRaisesMessage(ImproperlyConfigured,
106+
"'ExcludedFieldsInline.exclude' must be a list or tuple.",
107+
ExcludedFieldsAlbumAdmin.validate,
108+
Album)
103109

104110
def test_exclude_inline_model_admin(self):
105111
"""
@@ -114,10 +120,12 @@ class AlbumAdmin(admin.ModelAdmin):
114120
model = Album
115121
inlines = [SongInline]
116122

117-
self.assertRaisesMessage(ImproperlyConfigured,
118-
"SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
119-
AlbumAdmin.validate,
120-
Album)
123+
with warnings.catch_warnings(record=True):
124+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
125+
self.assertRaisesMessage(ImproperlyConfigured,
126+
"SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
127+
AlbumAdmin.validate,
128+
Album)
121129

122130
def test_app_label_in_admin_validation(self):
123131
"""
@@ -158,9 +166,11 @@ class TwoAlbumFKAndAnEInline(admin.TabularInline):
158166
class MyAdmin(admin.ModelAdmin):
159167
inlines = [TwoAlbumFKAndAnEInline]
160168

161-
self.assertRaisesMessage(ValueError,
162-
"'admin_validation.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_validation.Album'.",
163-
MyAdmin.validate, Album)
169+
with warnings.catch_warnings(record=True):
170+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
171+
self.assertRaisesMessage(ValueError,
172+
"'admin_validation.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_validation.Album'.",
173+
MyAdmin.validate, Album)
164174

165175
def test_inline_with_specified(self):
166176
class TwoAlbumFKAndAnEInline(admin.TabularInline):
@@ -216,22 +226,26 @@ def test_nonexistent_field(self):
216226
class SongAdmin(admin.ModelAdmin):
217227
readonly_fields = ("title", "nonexistent")
218228

219-
self.assertRaisesMessage(ImproperlyConfigured,
220-
str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistent' is not a callable "
221-
"or an attribute of 'SongAdmin' or found in the model 'Song'."),
222-
SongAdmin.validate,
223-
Song)
229+
with warnings.catch_warnings(record=True):
230+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
231+
self.assertRaisesMessage(ImproperlyConfigured,
232+
str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistent' is not a callable "
233+
"or an attribute of 'SongAdmin' or found in the model 'Song'."),
234+
SongAdmin.validate,
235+
Song)
224236

225237
def test_nonexistent_field_on_inline(self):
226238
class CityInline(admin.TabularInline):
227239
model = City
228240
readonly_fields = ['i_dont_exist'] # Missing attribute
229241

230-
self.assertRaisesMessage(ImproperlyConfigured,
231-
str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable "
232-
"or an attribute of 'CityInline' or found in the model 'City'."),
233-
CityInline.validate,
234-
City)
242+
with warnings.catch_warnings(record=True):
243+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
244+
self.assertRaisesMessage(ImproperlyConfigured,
245+
str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable "
246+
"or an attribute of 'CityInline' or found in the model 'City'."),
247+
CityInline.validate,
248+
City)
235249

236250
def test_extra(self):
237251
class SongAdmin(admin.ModelAdmin):
@@ -262,10 +276,12 @@ def test_graceful_m2m_fail(self):
262276
class BookAdmin(admin.ModelAdmin):
263277
fields = ['authors']
264278

265-
self.assertRaisesMessage(ImproperlyConfigured,
266-
"'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
267-
BookAdmin.validate,
268-
Book)
279+
with warnings.catch_warnings(record=True):
280+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
281+
self.assertRaisesMessage(ImproperlyConfigured,
282+
"'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
283+
BookAdmin.validate,
284+
Book)
269285

270286
def test_cannot_include_through(self):
271287
class FieldsetBookAdmin(admin.ModelAdmin):
@@ -274,10 +290,12 @@ class FieldsetBookAdmin(admin.ModelAdmin):
274290
('Header 2', {'fields': ('authors',)}),
275291
)
276292

277-
self.assertRaisesMessage(ImproperlyConfigured,
278-
"'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
279-
FieldsetBookAdmin.validate,
280-
Book)
293+
with warnings.catch_warnings(record=True):
294+
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
295+
self.assertRaisesMessage(ImproperlyConfigured,
296+
"'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
297+
FieldsetBookAdmin.validate,
298+
Book)
281299

282300
def test_nested_fields(self):
283301
class NestedFieldsAdmin(admin.ModelAdmin):

0 commit comments

Comments
 (0)