Commit 253adc2b authored by Hasan's avatar Hasan Committed by Tim Graham
Browse files

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

parent 3d0dcd7f
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -127,12 +127,12 @@ class TestRegistrationDecorator(SimpleTestCase):
        self.default_site.unregister(Place)

    def test_wrapped_class_not_a_model_admin(self):
        self.assertRaisesMessage(ValueError, 'Wrapped class must subclass ModelAdmin.',
            register(Person), CustomSite)
        with self.assertRaisesMessage(ValueError, 'Wrapped class must subclass ModelAdmin.'):
            register(Person)(CustomSite)

    def test_custom_site_not_an_admin_site(self):
        self.assertRaisesMessage(ValueError, 'site must subclass AdminSite',
            register(Person, site=Traveler), NameAdmin)
        with self.assertRaisesMessage(ValueError, 'site must subclass AdminSite'):
            register(Person, site=Traveler)(NameAdmin)

    def test_empty_models_list_registration_fails(self):
        with self.assertRaisesMessage(ValueError, 'At least one model must be passed to register.'):
+2 −5
Original line number Diff line number Diff line
@@ -161,11 +161,8 @@ class UserManagerTestCase(TestCase):
        self.assertEqual(returned, 'email\ with_whitespace@d.com')

    def test_empty_username(self):
        self.assertRaisesMessage(
            ValueError,
            'The given username must be set',
            User.objects.create_user, username=''
        )
        with self.assertRaisesMessage(ValueError, 'The given username must be set'):
            User.objects.create_user(username='')

    def test_create_user_is_staff(self):
        email = 'normal@normal.com'
+8 −17
Original line number Diff line number Diff line
@@ -91,31 +91,22 @@ class CustomColumnsTests(TestCase):
        self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))

    def test_filter_on_nonexistent_field(self):
        self.assertRaisesMessage(
            FieldError,
        msg = (
            "Cannot resolve keyword 'firstname' into field. Choices are: "
            "Author_ID, article, first_name, last_name, primary_set",
            Author.objects.filter,
            firstname__exact='John'
            "Author_ID, article, first_name, last_name, primary_set"
        )
        with self.assertRaisesMessage(FieldError, msg):
            Author.objects.filter(firstname__exact='John')

    def test_author_get_attributes(self):
        a = Author.objects.get(last_name__exact='Smith')
        self.assertEqual('John', a.first_name)
        self.assertEqual('Smith', a.last_name)
        self.assertRaisesMessage(
            AttributeError,
            "'Author' object has no attribute 'firstname'",
            getattr,
            a, 'firstname'
        )
        with self.assertRaisesMessage(AttributeError, "'Author' object has no attribute 'firstname'"):
            getattr(a, 'firstname')

        self.assertRaisesMessage(
            AttributeError,
            "'Author' object has no attribute 'last'",
            getattr,
            a, 'last'
        )
        with self.assertRaisesMessage(AttributeError, "'Author' object has no attribute 'last'"):
            getattr(a, 'last')

    def test_m2m_table(self):
        self.assertQuerysetEqual(
+2 −5
Original line number Diff line number Diff line
@@ -90,11 +90,8 @@ class DistinctOnTests(TestCase):

        # Combining queries with different distinct_fields is not allowed.
        base_qs = Celebrity.objects.all()
        self.assertRaisesMessage(
            AssertionError,
            "Cannot combine queries with different distinct fields.",
            lambda: (base_qs.distinct('id') & base_qs.distinct('name'))
        )
        with self.assertRaisesMessage(AssertionError, "Cannot combine queries with different distinct fields."):
            base_qs.distinct('id') & base_qs.distinct('name')

        # Test join unreffing
        c1 = Celebrity.objects.distinct('greatest_fan__id', 'greatest_fan__fan_of')
+4 −2
Original line number Diff line number Diff line
@@ -318,7 +318,8 @@ class BasicExpressionsTests(TestCase):
            'expressions.Company.num_employees. F() expressions can only be '
            'used to update, not to insert.'
        )
        self.assertRaisesMessage(ValueError, msg, acme.save)
        with self.assertRaisesMessage(ValueError, msg):
            acme.save()

        acme.num_employees = 12
        acme.name = Lower(F('name'))
@@ -327,7 +328,8 @@ class BasicExpressionsTests(TestCase):
            'expressions.Company.name))" on expressions.Company.name. F() '
            'expressions can only be used to update, not to insert.'
        )
        self.assertRaisesMessage(ValueError, msg, acme.save)
        with self.assertRaisesMessage(ValueError, msg):
            acme.save()

    def test_ticket_11722_iexact_lookup(self):
        Employee.objects.create(firstname="John", lastname="Doe")
Loading