Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31281 closed New feature (duplicate)

Make TestClient run transaction.on_commit actions.

Reported by: François Freitag Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, when ATOMIC_REQUESTS is True, actions scheduled with the on_commit hook are not run by the test Client.

Commit hooks can be run manually, e.g. by subclassing Django’s test Client and running the hooks after the parent’s request()generated the response. However, it makes a difference with real-world requests, for which hooks run before the middleware chain processes the response.

The ClientHandler defined in django/test/client.py is private and the Client does not offer to override its handler (except by subclassing the view and forcefully setting the handler attribute to a customized handler instance.

Maybe the default test ClientHandler should override make_view_atomic() to run on_commit hooks right after the view function is executed?

Change History (3)

comment:1 by Simon Charette, 5 years ago

Cc: Simon Charette added

This is a common pitfall (#30601) and closely related or maybe a dupe of #30457.

Only changing the behavior of the test Client to perform such operation would be tricky because of the following scenario.

def test_atomic_view(self):
    def receiver():
        pass
    transaction.on_commit(receiver)
    client.post('/atomic_view')

Would you expect receiver to be called or not? I think it could be considered unexpected behaviour in both cases. If it's called then it causes unexpected global state alteration and if it's not, say only on_commit receivers attached within the request handling context are handled, you could be in state where the request handling code expect previous on_commit handlers to be called.

I think we need a solution akind to the execute_on_commit context manager with RuntimeWarning when transacition.on_commit is used without mocking within the context of a TestCase.

Last edited 5 years ago by Simon Charette (previous) (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed
Summary: Make TestClient run transaction.on_commit actionsMake TestClient run transaction.on_commit actions.

I agree with Simon, this can be treated as a duplicate of #30457.

comment:3 by François Freitag, 5 years ago

Many thanks for the detailed explanation and the triage.

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