Repository: django/django
File: tests/test_utils/tests.py (Python)

Code before:
        self.assertEqual(str(raised_exception), "robust callback")

    def test_execute_robust_with_callback_as_partial(self):
        class MyException(Exception):
            pass

        def hook():
            self.callback_called = True
            raise MyException("robust callback")

        hook_partial = partial(hook)

        with self.assertLogs("django.test", "ERROR") as cm:
            with self.captureOnCommitCallbacks(execute=True) as callbacks:
                transaction.on_commit(hook_partial, robust=True)

        self.assertEqual(len(callbacks), 1)
        self.assertIs(self.callback_called, True)

        log_record = cm.records[0]
        self.assertRegex(
            log_record.getMessage(),
            r"Error calling functools\.partial\(<function CaptureOnCommitCallbacksTests"
            r"\.test_execute_robust_with_callback_as_partial\.<locals>\.hook"
            r" at .+>\) in on_commit\(\) \(robust callback\)\.",
        )
        self.assertIsNotNone(log_record.exc_info)
        raised_exception = log_record.exc_info[1]
        self.assertIsInstance(raised_exception, MyException)
        self.assertEqual(str(raised_exception), "robust callback")


class DisallowedDatabaseQueriesTests(SimpleTestCase):
    def test_disallowed_database_connections(self):
        expected_message = (
            "Database connections to 'default' are not allowed in SimpleTestCase "
            "subclasses. Either subclass TestCase or TransactionTestCase to "
            "ensure proper test isolation or add 'default' to "
            "test_utils.tests.DisallowedDatabaseQueriesTests.databases to "
            "silence this failure."
        )
        with self.assertRaisesMessage(DatabaseOperationForbidden, expected_message):
            connection.connect()
        with self.assertRaisesMessage(DatabaseOperationForbidden, expected_message):
            connection.temporary_connection()

    def test_disallowed_database_queries(self):
        expected_message = (
            "Database queries to 'default' are not allowed in SimpleTestCase "
            "subclasses. Either subclass TestCase or TransactionTestCase to "
            "ensure proper test isolation or add 'default' to "

Review comment (suggestion):
Django tends to assert very precisely on exception messages (for [another view](https://github.com/python/cpython/pull/98100#discussion_r1759570358), see Brett Cannon) -- but here I  think with so much escaping readability suffers. I would suggest:

```suggestion
        self.assertEqual(
            log_record.getMessage(),
            f"Error calling {hook_partial} in on_commit() (robust callback).",
        )
```
and similar for the other test.

Code after:
        self.assertEqual(str(raised_exception), "robust callback")

    def test_execute_robust_with_callback_as_partial(self):
        class MyException(Exception):
            pass

        def hook():
            self.callback_called = True
            raise MyException("robust callback")

        hook_partial = partial(hook)

        with self.assertLogs("django.test", "ERROR") as cm:
            with self.captureOnCommitCallbacks(execute=True) as callbacks:
                transaction.on_commit(hook_partial, robust=True)

        self.assertEqual(len(callbacks), 1)
        self.assertIs(self.callback_called, True)

        log_record = cm.records[0]
        self.assertEqual(
            log_record.getMessage(),
            f"Error calling {hook_partial} in on_commit() (robust callback).",
        )
        self.assertIsNotNone(log_record.exc_info)
        raised_exception = log_record.exc_info[1]
        self.assertIsInstance(raised_exception, MyException)
        self.assertEqual(str(raised_exception), "robust callback")


class DisallowedDatabaseQueriesTests(SimpleTestCase):
    def test_disallowed_database_connections(self):
        expected_message = (
            "Database connections to 'default' are not allowed in SimpleTestCase "
            "subclasses. Either subclass TestCase or TransactionTestCase to "
            "ensure proper test isolation or add 'default' to "
            "test_utils.tests.DisallowedDatabaseQueriesTests.databases to "
            "silence this failure."
        )
        with self.assertRaisesMessage(DatabaseOperationForbidden, expected_message):
            connection.connect()
        with self.assertRaisesMessage(DatabaseOperationForbidden, expected_message):
            connection.temporary_connection()

    def test_disallowed_database_queries(self):
        expected_message = (
            "Database queries to 'default' are not allowed in SimpleTestCase "
            "subclasses. Either subclass TestCase or TransactionTestCase to "
            "ensure proper test isolation or add 'default' to "
            "test_utils.tests.DisallowedDatabaseQueriesTests.databases to "
            "silence this failure."