From 4e5369a5962e59877ade8975620d73485061a706 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 29 Dec 2012 22:19:47 +0100 Subject: [PATCH] Silenced warnings in the tests of deprecated features. --- django/contrib/auth/tests/models.py | 26 +++++++++------ tests/modeltests/select_related/tests.py | 32 +++++++++++++------ .../multiple_database/tests.py | 7 ++-- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py index ca65dee71b..da0e45a55e 100644 --- a/django/contrib/auth/tests/models.py +++ b/django/contrib/auth/tests/models.py @@ -1,3 +1,5 @@ +import warnings + from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable, @@ -17,21 +19,27 @@ class ProfileTestCase(TestCase): # calling get_profile without AUTH_PROFILE_MODULE set del settings.AUTH_PROFILE_MODULE - with six.assertRaisesRegex(self, SiteProfileNotAvailable, - "You need to set AUTH_PROFILE_MODULE in your project"): - user.get_profile() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + with six.assertRaisesRegex(self, SiteProfileNotAvailable, + "You need to set AUTH_PROFILE_MODULE in your project"): + user.get_profile() # Bad syntax in AUTH_PROFILE_MODULE: settings.AUTH_PROFILE_MODULE = 'foobar' - with six.assertRaisesRegex(self, SiteProfileNotAvailable, - "app_label and model_name should be separated by a dot"): - user.get_profile() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + with six.assertRaisesRegex(self, SiteProfileNotAvailable, + "app_label and model_name should be separated by a dot"): + user.get_profile() # module that doesn't exist settings.AUTH_PROFILE_MODULE = 'foo.bar' - with six.assertRaisesRegex(self, SiteProfileNotAvailable, - "Unable to load the profile model"): - user.get_profile() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + with six.assertRaisesRegex(self, SiteProfileNotAvailable, + "Unable to load the profile model"): + user.get_profile() @skipIfCustomUser diff --git a/tests/modeltests/select_related/tests.py b/tests/modeltests/select_related/tests.py index 557a5c318a..27d65fecb1 100644 --- a/tests/modeltests/select_related/tests.py +++ b/tests/modeltests/select_related/tests.py @@ -1,5 +1,7 @@ from __future__ import absolute_import, unicode_literals +import warnings + from django.test import TestCase from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species @@ -53,7 +55,9 @@ class SelectRelatedTests(TestCase): extra queries """ with self.assertNumQueries(1): - person = Species.objects.select_related(depth=10).get(name="sapiens") + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + person = Species.objects.select_related(depth=10).get(name="sapiens") domain = person.genus.family.order.klass.phylum.kingdom.domain self.assertEqual(domain.name, 'Eukaryota') @@ -94,7 +98,9 @@ class SelectRelatedTests(TestCase): """ # Notice: one fewer queries than above because of depth=1 with self.assertNumQueries(expected): - pea = Species.objects.select_related(depth=depth).get(name="sativum") + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + pea = Species.objects.select_related(depth=depth).get(name="sativum") self.assertEqual( pea.genus.family.order.klass.phylum.kingdom.domain.name, 'Eukaryota' @@ -113,14 +119,18 @@ class SelectRelatedTests(TestCase): particular level. This can be used on lists as well. """ with self.assertNumQueries(5): - world = Species.objects.all().select_related(depth=2) - orders = [o.genus.family.order.name for o in world] + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + world = Species.objects.all().select_related(depth=2) + orders = [o.genus.family.order.name for o in world] self.assertEqual(sorted(orders), ['Agaricales', 'Diptera', 'Fabales', 'Primates']) def test_select_related_with_extra(self): - s = Species.objects.all().select_related(depth=1)\ - .extra(select={'a': 'select_related_species.id + 10'})[0] + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + s = Species.objects.all().select_related(depth=1)\ + .extra(select={'a': 'select_related_species.id + 10'})[0] self.assertEqual(s.id + 10, s.a) def test_certain_fields(self): @@ -156,7 +166,9 @@ class SelectRelatedTests(TestCase): self.assertEqual(s, 'Diptera') def test_depth_fields_fails(self): - self.assertRaises(TypeError, - Species.objects.select_related, - 'genus__family__order', depth=4 - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + self.assertRaises(TypeError, + Species.objects.select_related, + 'genus__family__order', depth=4 + ) diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index d2a5058206..a79c9c0aa1 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, unicode_literals import datetime import pickle from operator import attrgetter +import warnings from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType @@ -1612,8 +1613,10 @@ class UserProfileTestCase(TestCase): bob_profile = UserProfile(user=bob, flavor='crunchy frog') bob_profile.save() - self.assertEqual(alice.get_profile().flavor, 'chocolate') - self.assertEqual(bob.get_profile().flavor, 'crunchy frog') + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + self.assertEqual(alice.get_profile().flavor, 'chocolate') + self.assertEqual(bob.get_profile().flavor, 'crunchy frog') class AntiPetRouter(object): # A router that only expresses an opinion on syncdb,