Capture and make assertions on transaction.on_commit() callbacks.
Project description
Capture and make assertions on transaction.on_commit() callbacks. This allows you to write your tests with the TestCase, rather than needing the slower TransactionTestCase to actually commit the transactions. See Ticket #30457.
Installation
Use pip:
python -m pip install django-capture-on-commit-callbacks
Requirements
Python 3.5 to 3.8 supported.
Django 2.0 to 3.0 suppported.
API
capture_on_commit_callbacks(*, using="default", execute=False)
Acts as a context manager that captures commit hooks for the given database connection. It returns the hook function as a list, from where you can all them.
All arguments must be passed as keyword arguments.
using is the alias of the database connection to capture hooks for.
execute specifies whether to call all the hook functions automatically as the context manager exits.
For example, you can test a commit hook that sends an email like so:
from django.core import mail
from django.test import TestCase
from django_capture_on_commit_callbacks import capture_on_commit_callbacks
class ContactTests(TestCase):
def test_post(self):
with capture_on_commit_callbacks() as hooks:
response = self.client.post(
"/contact/",
{"message": "I like your site"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(hooks), 1)
# Execute the hook
hooks[0]()
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, "Contact Form")
self.assertEqual(mail.outbox[0].body, "I like your site")
The same test can be written a bit more succinctly with execute=True:
from django.core import mail
from django.test import TestCase
from django_capture_on_commit_callbacks import capture_on_commit_callbacks
class ContactTests(TestCase):
def test_post(self):
with capture_on_commit_callbacks(execute=True) as hooks:
response = self.client.post(
"/contact/",
{"message": "I like your site"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(hooks), 1)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, "Contact Form")
self.assertEqual(mail.outbox[0].body, "I like your site")
TestCaseMixin
A mixin class to be added to your custom TestCase subclass. It adds one method, captureOnCommitCallbacks() that aliases capture_on_commit_callbacks(), to match the camelCase style of unittest assertions.
You can add to your custom TestCase classes like so:
from django import test
from django_capture_on_commit_callbacks import TestCaseMixin
class TestCase(TestCaseMixin, test.TestCase):
pass
You could then rewrite the above tests with your custom TestCase class like so:
from django.core import mail
from example.test import TestCase
class ContactTests(TestCase):
def test_post(self):
with self.captureOnCommitCallbacks(execute=True) as hooks:
response = self.client.post(
"/contact/",
{"message": "I like your site"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(hooks), 1)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, "Contact Form")
self.assertEqual(mail.outbox[0].body, "I like your site")
History
1.0.0 (2020-05-20)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-capture-on-commit-callbacks-1.0.0.tar.gz
.
File metadata
- Download URL: django-capture-on-commit-callbacks-1.0.0.tar.gz
- Upload date:
- Size: 49.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 548ac15d673d23f3ec50d005ad769c5d1e48541ce2ada3975a629b6d99066bb6 |
|
MD5 | 579628db4fe5a123e5b272663b71a043 |
|
BLAKE2b-256 | 21025165f5bc2cb3f070a9d4d3eb00385f597267f06ada0f569be3b40bdb76db |
File details
Details for the file django_capture_on_commit_callbacks-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_capture_on_commit_callbacks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d471d2b6e1d2227e2dfafcbf7e662a5454a91ca1e703625d6a5ae25c9a8adee2 |
|
MD5 | cba1c63661f65637a17a7511d2d58fb0 |
|
BLAKE2b-256 | 947bbff2cd202d954e0d1e0e3aea9ee6fce362affdb0f199caa71a5d4335856d |