Commit 3d0dcd7f authored by Hasan's avatar Hasan Committed by Tim Graham
Browse files

Refs #26022 -- Used context manager version of assertRaises in tests.

parent 57570633
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -850,7 +850,8 @@ class ListFiltersTests(TestCase):
        """
        modeladmin = DecadeFilterBookAdminWithFailingQueryset(Book, site)
        request = self.request_factory.get('/', {})
        self.assertRaises(ZeroDivisionError, self.get_changelist, request, Book, modeladmin)
        with self.assertRaises(ZeroDivisionError):
            self.get_changelist(request, Book, modeladmin)

    def test_simplelistfilter_with_queryset_based_lookups(self):
        modeladmin = DecadeFilterBookAdminWithQuerysetBasedLookups(Book, site)
+4 −4
Original line number Diff line number Diff line
@@ -36,9 +36,8 @@ class TestRegistration(SimpleTestCase):

    def test_prevent_double_registration(self):
        self.site.register(Person)
        self.assertRaises(admin.sites.AlreadyRegistered,
                          self.site.register,
                          Person)
        with self.assertRaises(admin.sites.AlreadyRegistered):
            self.site.register(Person)

    def test_registration_with_star_star_options(self):
        self.site.register(Person, search_fields=['name'])
@@ -68,7 +67,8 @@ class TestRegistration(SimpleTestCase):
        Exception is raised when trying to register an abstract model.
        Refs #12004.
        """
        self.assertRaises(ImproperlyConfigured, self.site.register, Location)
        with self.assertRaises(ImproperlyConfigured):
            self.site.register(Location)

    def test_is_registered_model(self):
        "Checks for registered models should return true."
+2 −4
Original line number Diff line number Diff line
@@ -221,10 +221,8 @@ class UtilsTests(SimpleTestCase):
            str("article")
        )

        self.assertRaises(
            AttributeError,
            lambda: label_for_field("unknown", Article)
        )
        with self.assertRaises(AttributeError):
            label_for_field("unknown", Article)

        def test_callable(obj):
            return "nothing"
+16 −24
Original line number Diff line number Diff line
@@ -376,20 +376,14 @@ class AggregationTests(TestCase):

    def test_field_error(self):
        # Bad field requests in aggregates are caught and reported
        self.assertRaises(
            FieldError,
            lambda: Book.objects.all().aggregate(num_authors=Count('foo'))
        )
        with self.assertRaises(FieldError):
            Book.objects.all().aggregate(num_authors=Count('foo'))

        self.assertRaises(
            FieldError,
            lambda: Book.objects.all().annotate(num_authors=Count('foo'))
        )
        with self.assertRaises(FieldError):
            Book.objects.all().annotate(num_authors=Count('foo'))

        self.assertRaises(
            FieldError,
            lambda: Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
        )
        with self.assertRaises(FieldError):
            Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))

    def test_more(self):
        # Old-style count aggregations can be mixed with new-style
@@ -698,21 +692,20 @@ class AggregationTests(TestCase):

    def test_duplicate_alias(self):
        # Regression for #11256 - duplicating a default alias raises ValueError.
        self.assertRaises(
            ValueError,
            Book.objects.all().annotate,
            Avg('authors__age'), authors__age__avg=Avg('authors__age')
        )
        with self.assertRaises(ValueError):
            Book.objects.all().annotate(Avg('authors__age'), authors__age__avg=Avg('authors__age'))

    def test_field_name_conflict(self):
        # Regression for #11256 - providing an aggregate name
        # that conflicts with a field name on the model raises ValueError
        self.assertRaises(ValueError, Author.objects.annotate, age=Avg('friends__age'))
        with self.assertRaises(ValueError):
            Author.objects.annotate(age=Avg('friends__age'))

    def test_m2m_name_conflict(self):
        # Regression for #11256 - providing an aggregate name
        # that conflicts with an m2m name on the model raises ValueError
        self.assertRaises(ValueError, Author.objects.annotate, friends=Count('friends'))
        with self.assertRaises(ValueError):
            Author.objects.annotate(friends=Count('friends'))

    def test_values_queryset_non_conflict(self):
        # Regression for #14707 -- If you're using a values query set, some potential conflicts are avoided.
@@ -739,7 +732,8 @@ class AggregationTests(TestCase):
    def test_reverse_relation_name_conflict(self):
        # Regression for #11256 - providing an aggregate name
        # that conflicts with a reverse-related name on the model raises ValueError
        self.assertRaises(ValueError, Author.objects.annotate, book_contact_set=Avg('friends__age'))
        with self.assertRaises(ValueError):
            Author.objects.annotate(book_contact_set=Avg('friends__age'))

    def test_pickle(self):
        # Regression for #10197 -- Queries with aggregates can be pickled.
@@ -900,10 +894,8 @@ class AggregationTests(TestCase):

        # Regression for #10766 - Shouldn't be able to reference an aggregate
        # fields in an aggregate() call.
        self.assertRaises(
            FieldError,
            lambda: Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
        )
        with self.assertRaises(FieldError):
            Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))

    def test_empty_filter_count(self):
        self.assertEqual(
+6 −3
Original line number Diff line number Diff line
@@ -429,7 +429,8 @@ class NoBackendsTest(TestCase):
        self.user = User.objects.create_user('test', 'test@example.com', 'test')

    def test_raises_exception(self):
        self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))
        with self.assertRaises(ImproperlyConfigured):
            self.user.has_perm(('perm', TestObj()))


@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.SimpleRowlevelBackend'])
@@ -575,7 +576,8 @@ class TypeErrorBackendTest(TestCase):

    @override_settings(AUTHENTICATION_BACKENDS=[backend])
    def test_type_error_raised(self):
        self.assertRaises(TypeError, authenticate, username='test', password='test')
        with self.assertRaises(TypeError):
            authenticate(username='test', password='test')


class ImproperlyConfiguredUserModelTest(TestCase):
@@ -598,7 +600,8 @@ class ImproperlyConfiguredUserModelTest(TestCase):
        request = HttpRequest()
        request.session = self.client.session

        self.assertRaises(ImproperlyConfigured, get_user, request)
        with self.assertRaises(ImproperlyConfigured):
            get_user(request)


class ImportedModelBackend(ModelBackend):
Loading