-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fb90bf
commit 041aeb9
Showing
5 changed files
with
49 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from contextlib import contextmanager | ||
from unittest import mock | ||
|
||
from django.contrib.auth.management import DEFAULT_DB_ALIAS | ||
|
||
|
||
@contextmanager | ||
def immediate_on_commit(using=None): | ||
""" | ||
Context manager executing transaction.on_commit() hooks immediately as | ||
if the connection was in auto-commit mode. This is required when | ||
using a subclass of django.test.TestCase as all tests are wrapped in | ||
a transaction that never gets committed. | ||
Source: https://code.djangoproject.com/ticket/30457#comment:1 | ||
""" | ||
immediate_using = DEFAULT_DB_ALIAS if using is None else using | ||
|
||
def on_commit(func, using=None): | ||
using = DEFAULT_DB_ALIAS if using is None else using | ||
if using == immediate_using: | ||
func() | ||
|
||
with mock.patch('django.db.transaction.on_commit', side_effect=on_commit) as patch: | ||
yield patch |