-
Notifications
You must be signed in to change notification settings - Fork 6
Raise error when encountering unhandled callbacks #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,6 +194,102 @@ def raises() -> None: | |
| assert error_raised is True | ||
| assert counter.count == 1 | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "transaction_manager", | ||
| (db.transaction, db.transaction_if_not_already), | ||
| ) | ||
| def test_unhandled_callbacks_cause_error( | ||
| self, transaction_manager: DBContextManager | ||
| ) -> None: | ||
| """ | ||
| If callbacks from a previous atomic context remain, raise an error. | ||
| """ | ||
| counter = Counter() | ||
|
|
||
| # Django's `atomic` leaves unhandled after-commit actions on exit. | ||
| with django_transaction.atomic(): | ||
| db.run_after_commit(counter.increment) | ||
|
Comment on lines
+209
to
+211
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice demonstration of the cause of this problem. |
||
|
|
||
| # `transaction` will raise when it finds the unhandled callback. | ||
| with pytest.raises(db._UnhandledCallbacks) as exc_info: # noqa: SLF001 | ||
| with transaction_manager(): | ||
| ... | ||
|
|
||
| assert counter.count == 0 | ||
| assert exc_info.value.callbacks == (counter.increment,) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "transaction_manager", | ||
| (db.transaction, db.transaction_if_not_already), | ||
| ) | ||
| def test_unhandled_callbacks_check_can_be_disabled( | ||
| self, transaction_manager: DBContextManager | ||
| ) -> None: | ||
| """ | ||
| We can disable the check for unhandled callbacks. | ||
| """ | ||
| counter = Counter() | ||
|
|
||
| # Django's `atomic` leaves unhandled after-commit actions on exit. | ||
| with django_transaction.atomic(): | ||
| db.run_after_commit(counter.increment) | ||
|
|
||
| # Run after-commit callbacks when `transaction` exits, | ||
| # even if that means running them later than is realistic. | ||
| with override_settings( | ||
| SUBATOMIC_CATCH_UNHANDLED_AFTER_COMMIT_CALLBACKS_IN_TESTS=False | ||
| ): | ||
| with transaction_manager(): | ||
| assert counter.count == 0 | ||
|
|
||
| assert counter.count == 1 | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "transaction_manager", | ||
| (db.transaction, db.transaction_if_not_already), | ||
| ) | ||
| def test_handled_callbacks_are_not_an_error( | ||
| self, transaction_manager: DBContextManager | ||
| ) -> None: | ||
| """ | ||
| Already-handled checks do not cause an error. | ||
| """ | ||
| counter = Counter() | ||
|
|
||
| # Callbacks are handled by `transaction` and removed from the queue. | ||
| with db.transaction(): | ||
| db.run_after_commit(counter.increment) | ||
| assert counter.count == 0 | ||
| assert counter.count == 1 | ||
|
|
||
| # The callbacks have been handled, so a second `transaction` does not raise. | ||
| with transaction_manager(): | ||
| pass | ||
|
|
||
| # The callback was not run a second time. | ||
| assert counter.count == 1 | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "transaction_manager", | ||
| (db.transaction, db.transaction_if_not_already), | ||
| ) | ||
| def test_callbacks_ignored_by_transaction_if_not_already( | ||
| self, transaction_manager: DBContextManager | ||
| ) -> None: | ||
| """ | ||
| `transaction_if_not_already` ignores after-commit callbacks if a transaction already exists. | ||
| """ | ||
| counter = Counter() | ||
|
|
||
| with transaction_manager(): | ||
| db.run_after_commit(counter.increment) | ||
| with db.transaction_if_not_already(): | ||
| assert counter.count == 0 | ||
| assert counter.count == 0 | ||
|
|
||
| # The callback is run when the outermost transaction exits. | ||
| assert counter.count == 1 | ||
|
|
||
|
|
||
| class TestTransactionRequired: | ||
| @_parametrize_transaction_testcase | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.