From 53aa6c6ac1b6534f2e3a159014401b84bf1f70c1 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 20 Jan 2014 20:30:29 +0800 Subject: [PATCH] Added warning silencers to some noisy tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These warnings all emerged as the result of the introduction of the checks framework. Thanks to Anssi Kääriäinen for the report. --- tests/admin_checks/tests.py | 23 +++-- .../management/commands/color_command.py | 2 +- tests/admin_scripts/tests.py | 8 +- tests/admin_validation/tests.py | 85 ++++++++++++++----- 4 files changed, 85 insertions(+), 33 deletions(-) diff --git a/tests/admin_checks/tests.py b/tests/admin_checks/tests.py index 0f6320430e..934e3f391e 100644 --- a/tests/admin_checks/tests.py +++ b/tests/admin_checks/tests.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import warnings + from django import forms from django.contrib import admin from django.core import checks @@ -425,12 +427,15 @@ class SystemChecksTestCase(TestCase): class MyModelAdmin(admin.ModelAdmin): validator_class = MyValidator - errors = MyModelAdmin.check(model=Song) - expected = [ - checks.Error( - 'error!', - hint=None, - obj=MyModelAdmin, - ) - ] - self.assertEqual(errors, expected) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + errors = MyModelAdmin.check(model=Song) + + expected = [ + checks.Error( + 'error!', + hint=None, + obj=MyModelAdmin, + ) + ] + self.assertEqual(errors, expected) diff --git a/tests/admin_scripts/management/commands/color_command.py b/tests/admin_scripts/management/commands/color_command.py index 02da0a14ab..dda2cc826d 100644 --- a/tests/admin_scripts/management/commands/color_command.py +++ b/tests/admin_scripts/management/commands/color_command.py @@ -3,7 +3,7 @@ from django.core.management.base import NoArgsCommand class Command(NoArgsCommand): help = "Test color output" - requires_model_validation = False + requires_system_checks = False def handle_noargs(self, **options): return self.style.SQL_KEYWORD('BEGIN') diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 0d7ce41dcb..225441827f 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -6,7 +6,6 @@ A series of tests to establish that the command-line managment tools work as advertised - especially with regards to the handling of the DJANGO_SETTINGS_MODULE and default settings.py files. """ -from __future__ import unicode_literals import codecs import os @@ -16,6 +15,7 @@ import socket import subprocess import sys import unittest +import warnings import django from django import conf, get_version @@ -1556,8 +1556,10 @@ class CommandTypes(AdminScriptTestCase): self.assertOutput(out, str_prefix("EXECUTE:LabelCommand label=anotherlabel, options=[('no_color', False), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', %(_)s'1')]")) def test_requires_model_validation_and_requires_system_checks_both_defined(self): - from .management.commands.validation_command import InvalidCommand - self.assertRaises(ImproperlyConfigured, InvalidCommand) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.core.management.base') + from .management.commands.validation_command import InvalidCommand + self.assertRaises(ImproperlyConfigured, InvalidCommand) class Discovery(TestCase): diff --git a/tests/admin_validation/tests.py b/tests/admin_validation/tests.py index f724099c53..582f598d27 100644 --- a/tests/admin_validation/tests.py +++ b/tests/admin_validation/tests.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import warnings + from django import forms from django.contrib import admin from django.core.exceptions import ImproperlyConfigured @@ -41,13 +43,18 @@ class ValidationTestCase(TestCase): "fields": ["title", "original_release"], }), ] - SongAdmin.validate(Song) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_custom_modelforms_with_fields_fieldsets(self): """ # Regression test for #8027: custom ModelForms with fields/fieldsets """ - ValidFields.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + ValidFields.validate(Song) def test_custom_get_form_with_fieldsets(self): """ @@ -55,7 +62,9 @@ class ValidationTestCase(TestCase): is overridden. Refs #19445. """ - ValidFormFieldsets.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + ValidFormFieldsets.validate(Song) def test_exclude_values(self): """ @@ -63,6 +72,7 @@ class ValidationTestCase(TestCase): """ class ExcludedFields1(admin.ModelAdmin): exclude = ('foo') + self.assertRaisesMessage(ImproperlyConfigured, "'ExcludedFields1.exclude' must be a list or tuple.", ExcludedFields1.validate, @@ -71,6 +81,7 @@ class ValidationTestCase(TestCase): def test_exclude_duplicate_values(self): class ExcludedFields2(admin.ModelAdmin): exclude = ('name', 'name') + self.assertRaisesMessage(ImproperlyConfigured, "There are duplicate field(s) in ExcludedFields2.exclude", ExcludedFields2.validate, @@ -115,10 +126,12 @@ class ValidationTestCase(TestCase): class RawIdNonexistingAdmin(admin.ModelAdmin): raw_id_fields = ('nonexisting',) - self.assertRaisesMessage(ImproperlyConfigured, - "'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.", - RawIdNonexistingAdmin.validate, - Album) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + self.assertRaisesMessage(ImproperlyConfigured, + "'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.", + RawIdNonexistingAdmin.validate, + Album) def test_fk_exclusion(self): """ @@ -133,7 +146,10 @@ class ValidationTestCase(TestCase): class MyAdmin(admin.ModelAdmin): inlines = [TwoAlbumFKAndAnEInline] - MyAdmin.validate(Album) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + MyAdmin.validate(Album) def test_inline_self_validation(self): class TwoAlbumFKAndAnEInline(admin.TabularInline): @@ -153,13 +169,18 @@ class ValidationTestCase(TestCase): class MyAdmin(admin.ModelAdmin): inlines = [TwoAlbumFKAndAnEInline] - MyAdmin.validate(Album) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + MyAdmin.validate(Album) def test_readonly(self): class SongAdmin(admin.ModelAdmin): readonly_fields = ("title",) - SongAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_readonly_on_method(self): def my_function(obj): @@ -168,7 +189,9 @@ class ValidationTestCase(TestCase): class SongAdmin(admin.ModelAdmin): readonly_fields = (my_function,) - SongAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_readonly_on_modeladmin(self): class SongAdmin(admin.ModelAdmin): @@ -177,13 +200,17 @@ class ValidationTestCase(TestCase): def readonly_method_on_modeladmin(self, obj): pass - SongAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_readonly_method_on_model(self): class SongAdmin(admin.ModelAdmin): readonly_fields = ("readonly_method_on_model",) - SongAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_nonexistant_field(self): class SongAdmin(admin.ModelAdmin): @@ -212,13 +239,18 @@ class ValidationTestCase(TestCase): if instance.title == "Born to Run": return "Best Ever!" return "Status unknown." - SongAdmin.validate(Song) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_readonly_lambda(self): class SongAdmin(admin.ModelAdmin): readonly_fields = (lambda obj: "test",) - SongAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + SongAdmin.validate(Song) def test_graceful_m2m_fail(self): """ @@ -241,6 +273,7 @@ class ValidationTestCase(TestCase): ('Header 1', {'fields': ('name',)}), ('Header 2', {'fields': ('authors',)}), ) + self.assertRaisesMessage(ImproperlyConfigured, "'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.", FieldsetBookAdmin.validate, @@ -249,14 +282,20 @@ class ValidationTestCase(TestCase): def test_nested_fields(self): class NestedFieldsAdmin(admin.ModelAdmin): fields = ('price', ('name', 'subtitle')) - NestedFieldsAdmin.validate(Book) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + NestedFieldsAdmin.validate(Book) def test_nested_fieldsets(self): class NestedFieldsetAdmin(admin.ModelAdmin): fieldsets = ( ('Main', {'fields': ('price', ('name', 'subtitle'))}), ) - NestedFieldsetAdmin.validate(Book) + + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + NestedFieldsetAdmin.validate(Book) def test_explicit_through_override(self): """ @@ -273,7 +312,9 @@ class ValidationTestCase(TestCase): # If the through model is still a string (and hasn't been resolved to a model) # the validation will fail. - BookAdmin.validate(Book) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + BookAdmin.validate(Book) def test_non_model_fields(self): """ @@ -287,7 +328,9 @@ class ValidationTestCase(TestCase): form = SongForm fields = ['title', 'extra_data'] - FieldsOnFormOnlyAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + FieldsOnFormOnlyAdmin.validate(Song) def test_non_model_first_field(self): """ @@ -305,4 +348,6 @@ class ValidationTestCase(TestCase): form = SongForm fields = ['extra_data', 'title'] - FieldsOnFormOnlyAdmin.validate(Song) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('ignore', module='django.contrib.admin.options') + FieldsOnFormOnlyAdmin.validate(Song)