#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:2 by , 6 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
| Summary: | Make TestClient run transaction.on_commit actions → Make TestClient run transaction.on_commit actions. |
I agree with Simon, this can be treated as a duplicate of #30457.
This is a common pitfall (#30601) and closely related or maybe a dupe of #30457.
Only changing the behavior of the test
Clientto perform such operation would be tricky because of the following scenario.Would you expect
receiverto 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 onlyon_commitreceivers attached within the request handling context are handled, you could be in state where the request handling code expect previouson_commithandlers to be called.I think we need a solution akind to the
execute_on_commitcontext manager withRuntimeWarningwhentransacition.on_commitis used without mocking within the context of aTestCase.