Opened 11 months ago

Closed 3 months ago

#36280 closed Cleanup/optimization (fixed)

Replace cm.exception.messages with assertRaisesMessage() in tests

Reported by: Tim Graham Owned by: Sachi Jain
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: David Bogar Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Georgii (George) Randiuk)

I haven't audited each instance, but most likely many of the uses of cm.exception.messages could be made more consistent with the rest of the Django test suite by instead using self.assertRaisesMessage().

tests/composite_pk/test_models.py:                self.assertSequenceEqual(ctx.exception.messages, messages)
tests/composite_pk/test_models.py:            ctx.exception.messages, ("User with this Email already exists.",)
tests/validation/test_error_messages.py:        self.assertEqual(cm.exception.messages, expected)
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [msg_too_short])
tests/auth_tests/test_validators.py:            cm.exception.messages, ["This password is too common.", msg_too_short]
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % 8])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % 3])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % "username"])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % "email address"])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % "first name"])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error % "first name"])
tests/auth_tests/test_validators.py:            cm.exception.messages, ["The password is too similar to the username."]
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:                self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error])
tests/auth_tests/test_validators.py:        self.assertEqual(cm.exception.messages, [expected_error])
tests/forms_tests/tests/test_error_messages.py:        self.assertEqual(cm.exception.messages, expected)
tests/forms_tests/tests/test_validators.py:        self.assertEqual(2, len(e.exception.messages))
tests/test_exceptions/test_validation_error.py:        self.assertEqual(sorted(exception.messages), [])
tests/test_exceptions/test_validation_error.py:        self.assertEqual(sorted(exception.messages), ["E1", "E2"])
tests/test_exceptions/test_validation_error.py:        self.assertEqual(sorted(exception.messages), ["E1", "E2", "E3", "E4"])
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:            cm.exception.messages[0], "Nested arrays must have the same length."
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:            cm.exception.messages[0],
tests/postgres_tests/test_array.py:        self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_array.py:            cm.exception.messages,
tests/postgres_tests/test_array.py:            cm.exception.messages,
tests/postgres_tests/test_hstore.py:        self.assertEqual(cm.exception.messages[0], "Could not load JSON data.")
tests/postgres_tests/test_hstore.py:        self.assertEqual(cm.exception.messages[0], "Some keys were missing: b")
tests/postgres_tests/test_hstore.py:        self.assertEqual(cm.exception.messages[0], "Some unknown keys were provided: c")
tests/postgres_tests/test_hstore.py:        self.assertEqual(cm.exception.messages[0], "Foobar")
tests/postgres_tests/test_hstore.py:        self.assertEqual(cm.exception.messages[0], "Some unknown keys were provided: c")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], msg)
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], msg)
tests/postgres_tests/test_ranges.py:            cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter two whole numbers.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py:            cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter two numbers.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a number.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a number.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py:            cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter two valid dates.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "This field is required.")
tests/postgres_tests/test_ranges.py:            cm.exception.messages[0],
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter two valid date/times.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
tests/postgres_tests/test_ranges.py:        self.assertEqual(cm.exception.messages[0], "This field is required.")

PR link: https://github.com/django/django/pull/19317

Change History (14)

comment:1 by Sarah Boyce, 11 months ago

Triage Stage: UnreviewedAccepted

Agree that most can be updated
Likely that the ones that check the error code (cm.exception.code) may need to stay the same

in reply to:  1 comment:2 by Georgii (George) Randiuk, 11 months ago

Replying to Sarah Boyce:

Agree that most can be updated
Likely that the ones that check the error code (cm.exception.code) may need to stay the same

I assume the same goes for assertEqual's that validate two lists of errors? Since, assertRaisesMessage validates against a string, not a list of them. Otherwise, we would need to write a new assert for that specifically.

comment:3 by Georgii (George) Randiuk, 11 months ago

Owner: set to Georgii (George) Randiuk
Status: newassigned

comment:4 by Georgii (George) Randiuk, 11 months ago

Description: modified (diff)
Has patch: set
Patch needs improvement: set

comment:5 by Yahya Ehsan, 11 months ago

Hi! I noticed the current patch for this ticket is marked as needing improvement.

I’d love to help move this forward — would it be alright if I tried improving the patch or opened a new PR based on the feedback so far?

Just want to make sure it’s okay before proceeding. Thanks in advance!

— Yahya

comment:6 by Tim Graham, 11 months ago

No, you should wait at least a month before considering a patch abandoned due to inactivity.

comment:7 by David Bogar, 10 months ago

Cc: David Bogar added
Triage Stage: AcceptedReady for checkin

comment:8 by Tim Graham, 10 months ago

Triage Stage: Ready for checkinAccepted

The patch is still marked as needs improvement.

comment:9 by Clifford Gama, 10 months ago

The following pattern used to test assertions looks like it could also be covered by this ticket, maybe with the exception of tests for assertRaisesMessage itself?

        try:
            self.assertRedirects(response, "/get_view/")
        except AssertionError as e:
            self.assertIn(
                "Response redirected to '/get_view/?var=value', expected '/get_view/'",
                str(e),
            )

e.g. in test_client_regress.tests.*, where this pattern is used extensively and interchangeably with assertRaisesMessage.

comment:10 by Sachi Jain, 3 months ago

Owner: changed from Georgii (George) Randiuk to Sachi Jain

comment:11 by Jacob Walls, 3 months ago

Patch needs improvement: unset

comment:12 by Jacob Walls, 3 months ago

Patch needs improvement: set

comment:13 by Jacob Walls, 3 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:14 by Jacob Walls <jacobtylerwalls@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In d338c22:

Fixed #36280 -- Replaced exception checks with assertRaisesMessage().

Note: See TracTickets for help on using tickets.
Back to Top