signal_guard.testing¶
Assertions for tests. The guide shows them in use.
Reports¶
signal_guard.testing.capture_reports
¶
Collect reports instead of raising, warning or logging them.
with capture_reports() as reports:
run_import()
assert reports.bypasses == []
With kinds ("bypass", "recursion", "side_effect"), only those are
collected; others are emitted as usual. Settings still apply: a check
set to "off" reports nothing.
signal_guard.testing.assert_no_reports
¶
Fail if the block causes any report (of kinds, if given).
A context manager or a decorator. Reports of other kinds are emitted as usual.
signal_guard.testing.assert_bypass
¶
assert_bypass(
model: type[Model] | str | None = None,
*,
method: str | None = None,
receivers: Iterable[str] = (),
count: int | None = None,
)
Fail unless the block makes a bulk write that skips receivers.
with assert_bypass(Order, method="update", receivers=["*.reindex_order"]):
Order.objects.update(status="paid")
model is a model class or an "app_label.Model" label. Each of
receivers is an fnmatch pattern that must match one of the skipped
receivers' dotted paths. count asks for exactly that many reports
instead of at least one. Bypasses that don't match are emitted as usual.
signal_guard.testing.assert_side_effect
¶
assert_side_effect(
call: str | None = None,
*,
receiver: str | None = None,
count: int | None = None,
)
Fail unless the block causes a side effect before commit.
call is an fnmatch pattern for the call as reports name it, such as
"Email*", "Celery task shop.tasks.*" or "HTTP POST *".
receiver is a pattern for the receiver's dotted path.
signal_guard.testing.assert_recursion
¶
Fail unless a receiver re-enters itself in the block.
The loop is stopped, as raise mode would, and the block ends there.
receiver is a pattern that one receiver in the chain must match.
signal_guard.testing.Reports
¶
A list of Report objects, with filters by kind.
signal_guard.testing.Report
dataclass
¶
Report(
kind: str,
location: str | None,
message: str,
test: str | None,
where: str | None = None,
fingerprint: str = "",
error: Exception | None = None,
)
One problem found by a check.
Passed to captures (signal_guard.testing) and listeners (the pytest
plugin) before it is raised, warned or logged.
Signals sent¶
signal_guard.testing.capture_signals
¶
Record the signals sent in the block, with their arguments.
with capture_signals(post_save, sender=Order) as sent:
order.save()
assert sent[0].kwargs["created"] is False
With no signals, records every signal; sender limits it to one
sender. Signals with no receivers are recorded too. Each SentSignal
has signal, signal_name, sender, kwargs, instance and
responses.
signal_guard.testing.assert_sent
¶
assert_sent(
signal: Signal,
*,
sender: Any = None,
count: int | None = None,
**kwargs: Any,
) -> Iterator[SentSignals]
Fail unless the block sends signal (from sender, with kwargs).
with assert_sent(post_save, sender=Order, created=True) as sent:
Order.objects.create()
Each keyword must equal the argument the signal was sent with.
count asks for exactly that many sends instead of at least one. The
matching sends are yielded.
signal_guard.testing.assert_not_sent
¶
Fail if the block sends signal (from sender, with kwargs).
signal_guard.testing.SentSignals
¶
A list of SentSignal objects, oldest first.
signal_guard.tracing.SentSignal
¶
One signal sent while capture_signals() was recording.
Receivers connected¶
signal_guard.testing.assert_connected
¶
assert_connected(
signal: Signal,
receiver: Callable[..., Any] | str,
*,
sender: Any = None,
) -> None
Fail unless receiver would run when signal is sent by sender.
receiver is the function itself or an fnmatch pattern for its dotted
path. Receivers connected for every sender count. Catches the receiver
whose module is never imported.
signal_guard.testing.assert_not_connected
¶
assert_not_connected(
signal: Signal,
receiver: Callable[..., Any] | str,
*,
sender: Any = None,
) -> None
Fail if receiver would run when signal is sent by sender.
unittest¶
signal_guard.testing.SignalGuardAssertionsMixin
¶
The assertions as unittest methods, for TestCase classes.
Mix it into a test case:
class OrderTests(SignalGuardAssertionsMixin, TestCase):
def test_import(self):
with self.assertNoSignalReports():
import_orders(rows)
Each method returns the matching assertion from this module.