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.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable, from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable,
@ -17,21 +19,27 @@ class ProfileTestCase(TestCase):
# calling get_profile without AUTH_PROFILE_MODULE set # calling get_profile without AUTH_PROFILE_MODULE set
del settings.AUTH_PROFILE_MODULE del settings.AUTH_PROFILE_MODULE
with six.assertRaisesRegex(self, SiteProfileNotAvailable, with warnings.catch_warnings():
"You need to set AUTH_PROFILE_MODULE in your project"): warnings.simplefilter("ignore", DeprecationWarning)
user.get_profile() with six.assertRaisesRegex(self, SiteProfileNotAvailable,
"You need to set AUTH_PROFILE_MODULE in your project"):
user.get_profile()
# Bad syntax in AUTH_PROFILE_MODULE: # Bad syntax in AUTH_PROFILE_MODULE:
settings.AUTH_PROFILE_MODULE = 'foobar' settings.AUTH_PROFILE_MODULE = 'foobar'
with six.assertRaisesRegex(self, SiteProfileNotAvailable, with warnings.catch_warnings():
"app_label and model_name should be separated by a dot"): warnings.simplefilter("ignore", DeprecationWarning)
user.get_profile() with six.assertRaisesRegex(self, SiteProfileNotAvailable,
"app_label and model_name should be separated by a dot"):
user.get_profile()
# module that doesn't exist # module that doesn't exist
settings.AUTH_PROFILE_MODULE = 'foo.bar' settings.AUTH_PROFILE_MODULE = 'foo.bar'
with six.assertRaisesRegex(self, SiteProfileNotAvailable, with warnings.catch_warnings():
"Unable to load the profile model"): warnings.simplefilter("ignore", DeprecationWarning)
user.get_profile() with six.assertRaisesRegex(self, SiteProfileNotAvailable,
"Unable to load the profile model"):
user.get_profile()
@skipIfCustomUser @skipIfCustomUser

View File

@ -1,5 +1,7 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import warnings
from django.test import TestCase from django.test import TestCase
from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
@ -53,7 +55,9 @@ class SelectRelatedTests(TestCase):
extra queries extra queries
""" """
with self.assertNumQueries(1): 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 domain = person.genus.family.order.klass.phylum.kingdom.domain
self.assertEqual(domain.name, 'Eukaryota') self.assertEqual(domain.name, 'Eukaryota')
@ -94,7 +98,9 @@ class SelectRelatedTests(TestCase):
""" """
# Notice: one fewer queries than above because of depth=1 # Notice: one fewer queries than above because of depth=1
with self.assertNumQueries(expected): 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( self.assertEqual(
pea.genus.family.order.klass.phylum.kingdom.domain.name, pea.genus.family.order.klass.phylum.kingdom.domain.name,
'Eukaryota' 'Eukaryota'
@ -113,14 +119,18 @@ class SelectRelatedTests(TestCase):
particular level. This can be used on lists as well. particular level. This can be used on lists as well.
""" """
with self.assertNumQueries(5): with self.assertNumQueries(5):
world = Species.objects.all().select_related(depth=2) with warnings.catch_warnings():
orders = [o.genus.family.order.name for o in world] 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), self.assertEqual(sorted(orders),
['Agaricales', 'Diptera', 'Fabales', 'Primates']) ['Agaricales', 'Diptera', 'Fabales', 'Primates'])
def test_select_related_with_extra(self): def test_select_related_with_extra(self):
s = Species.objects.all().select_related(depth=1)\ with warnings.catch_warnings():
.extra(select={'a': 'select_related_species.id + 10'})[0] 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) self.assertEqual(s.id + 10, s.a)
def test_certain_fields(self): def test_certain_fields(self):
@ -156,7 +166,9 @@ class SelectRelatedTests(TestCase):
self.assertEqual(s, 'Diptera') self.assertEqual(s, 'Diptera')
def test_depth_fields_fails(self): def test_depth_fields_fails(self):
self.assertRaises(TypeError, with warnings.catch_warnings():
Species.objects.select_related, warnings.simplefilter("ignore", DeprecationWarning)
'genus__family__order', depth=4 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 datetime
import pickle import pickle
from operator import attrgetter from operator import attrgetter
import warnings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -1612,8 +1613,10 @@ class UserProfileTestCase(TestCase):
bob_profile = UserProfile(user=bob, flavor='crunchy frog') bob_profile = UserProfile(user=bob, flavor='crunchy frog')
bob_profile.save() bob_profile.save()
self.assertEqual(alice.get_profile().flavor, 'chocolate') with warnings.catch_warnings():
self.assertEqual(bob.get_profile().flavor, 'crunchy frog') warnings.simplefilter("ignore", DeprecationWarning)
self.assertEqual(alice.get_profile().flavor, 'chocolate')
self.assertEqual(bob.get_profile().flavor, 'crunchy frog')
class AntiPetRouter(object): class AntiPetRouter(object):
# A router that only expresses an opinion on syncdb, # A router that only expresses an opinion on syncdb,