Silenced warnings in the tests of deprecated features.

This commit is contained in:
Aymeric Augustin 2012-12-29 22:19:47 +01:00
parent 7ee7599ab3
commit 4e5369a596
3 changed files with 44 additions and 21 deletions

View File

@ -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

View File

@ -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
)

View File

@ -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,