audit_trail.testing¶
Test helpers for applications that use audit_trail.
assert_audited checks that an entry with the given fields was written and
assert_not_audited that none was; aassert_audited and
aassert_not_audited do the same on async binds. A failed assertion lists
the closest entries and the fields that differ.
create_test_tables, drop_test_tables and clear_audit_entries set
up the audit tables on a test database whose migrations do not create them.
The pytest fixtures built on them live in audit_trail.pytest_plugin;
enable them in the root conftest.py::
pytest_plugins = ["audit_trail.pytest_plugin"]
This module does not import pytest.
UNSET
module-attribute
¶
Filter not given: the field is not checked. None means NULL.
Unset ¶
Bases: Enum
Type of UNSET, the default of every assertion filter.
assert_audited ¶
assert_audited(
audit: AuditTrail,
bind: Session | Connection | Engine,
*,
verb: str | Unset = UNSET,
obj: object = UNSET,
target: object = UNSET,
actor_id: str | Unset | None = UNSET,
scope_id: str | Unset | None = UNSET,
severity: int | Unset = UNSET,
changes: Mapping[str, object] | Unset = UNSET,
payload: Mapping[str, object] | Unset = UNSET,
) -> list[ActivityRow]
Assert that at least one audit entry matches every given filter.
Reads the raw audit_activity rows as written, not compacted. A
Session is flushed first, so its pending changes are audited. A
Session or Connection also sees rows not committed yet; an
Engine reads on a new connection and sees committed rows only.
Values are compared with their stored JSON form: a datetime is an ISO
string, a redacted value "***". unittest.mock.ANY matches any
value, e.g. changes={"status": [ANY, "paid"]}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
The |
required |
bind
|
Session | Connection | Engine
|
Session, connection or engine to read with. |
required |
verb
|
str | Unset
|
The verb; an |
UNSET
|
obj
|
object
|
The object: a mapped instance, a |
UNSET
|
target
|
object
|
The parent object, like |
UNSET
|
actor_id
|
str | Unset | None
|
The actor id; |
UNSET
|
scope_id
|
str | Unset | None
|
The scope id; |
UNSET
|
severity
|
int | Unset
|
The severity. |
UNSET
|
changes
|
Mapping[str, object] | Unset
|
Changes the entry must contain; other keys are ignored.
A column change is |
UNSET
|
payload
|
Mapping[str, object] | Unset
|
Payload fields the entry must contain; other keys are ignored. |
UNSET
|
Returns:
| Type | Description |
|---|---|
list[ActivityRow]
|
The matching entries, oldest first. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
No entry matches; the message lists the closest ones and how they differ. |
ValueError
|
|
Source code in audit_trail/testing.py
assert_not_audited ¶
assert_not_audited(
audit: AuditTrail,
bind: Session | Connection | Engine,
*,
verb: str | Unset = UNSET,
obj: object = UNSET,
target: object = UNSET,
actor_id: str | Unset | None = UNSET,
scope_id: str | Unset | None = UNSET,
severity: int | Unset = UNSET,
changes: Mapping[str, object] | Unset = UNSET,
payload: Mapping[str, object] | Unset = UNSET,
) -> None
Assert that no audit entry matches every given filter.
Reads as assert_audited does, with the same filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
See |
required |
bind
|
Session | Connection | Engine
|
See |
required |
verb
|
str | Unset
|
See |
UNSET
|
obj
|
object
|
See |
UNSET
|
target
|
object
|
See |
UNSET
|
actor_id
|
str | Unset | None
|
See |
UNSET
|
scope_id
|
str | Unset | None
|
See |
UNSET
|
severity
|
int | Unset
|
See |
UNSET
|
changes
|
Mapping[str, object] | Unset
|
See |
UNSET
|
payload
|
Mapping[str, object] | Unset
|
See |
UNSET
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
An entry matches; the message lists the matches. |
ValueError
|
See |
Source code in audit_trail/testing.py
aassert_audited
async
¶
aassert_audited(
audit: AuditTrail,
bind: AsyncSession | AsyncConnection | AsyncEngine,
*,
verb: str | Unset = UNSET,
obj: object = UNSET,
target: object = UNSET,
actor_id: str | Unset | None = UNSET,
scope_id: str | Unset | None = UNSET,
severity: int | Unset = UNSET,
changes: Mapping[str, object] | Unset = UNSET,
payload: Mapping[str, object] | Unset = UNSET,
) -> list[ActivityRow]
Async assert_audited, for an async session, connection or engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
See |
required |
bind
|
AsyncSession | AsyncConnection | AsyncEngine
|
Async session, connection or engine to read with. |
required |
verb
|
str | Unset
|
See |
UNSET
|
obj
|
object
|
See |
UNSET
|
target
|
object
|
See |
UNSET
|
actor_id
|
str | Unset | None
|
See |
UNSET
|
scope_id
|
str | Unset | None
|
See |
UNSET
|
severity
|
int | Unset
|
See |
UNSET
|
changes
|
Mapping[str, object] | Unset
|
See |
UNSET
|
payload
|
Mapping[str, object] | Unset
|
See |
UNSET
|
Returns:
| Type | Description |
|---|---|
list[ActivityRow]
|
The matching entries, oldest first. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
See |
ValueError
|
See |
Source code in audit_trail/testing.py
aassert_not_audited
async
¶
aassert_not_audited(
audit: AuditTrail,
bind: AsyncSession | AsyncConnection | AsyncEngine,
*,
verb: str | Unset = UNSET,
obj: object = UNSET,
target: object = UNSET,
actor_id: str | Unset | None = UNSET,
scope_id: str | Unset | None = UNSET,
severity: int | Unset = UNSET,
changes: Mapping[str, object] | Unset = UNSET,
payload: Mapping[str, object] | Unset = UNSET,
) -> None
Async assert_not_audited, for an async session, connection or engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
See |
required |
bind
|
AsyncSession | AsyncConnection | AsyncEngine
|
Async session, connection or engine to read with. |
required |
verb
|
str | Unset
|
See |
UNSET
|
obj
|
object
|
See |
UNSET
|
target
|
object
|
See |
UNSET
|
actor_id
|
str | Unset | None
|
See |
UNSET
|
scope_id
|
str | Unset | None
|
See |
UNSET
|
severity
|
int | Unset
|
See |
UNSET
|
changes
|
Mapping[str, object] | Unset
|
See |
UNSET
|
payload
|
Mapping[str, object] | Unset
|
See |
UNSET
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
See |
ValueError
|
See |
Source code in audit_trail/testing.py
create_test_tables ¶
create_test_tables(
audit: AuditTrail,
connection: Connection,
*,
months_ahead: int = 1,
now: datetime | None = None,
) -> None
Create the audit tables and their partitions on a test database.
For databases whose migrations do not create the audit tables: it fails
when they exist. Runs in the connection's current transaction, which must
not be AUTOCOMMIT; the caller commits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
The |
required |
connection
|
Connection
|
Connection with DDL privileges. |
required |
months_ahead
|
int
|
Monthly partitions to create after the current month. |
1
|
now
|
datetime | None
|
Reference time for the partitions. |
None
|
Source code in audit_trail/testing.py
drop_test_tables ¶
Drop the audit tables with their partitions; the schema is kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
The |
required |
connection
|
Connection
|
Connection with DDL privileges. |
required |
Source code in audit_trail/testing.py
clear_audit_entries ¶
Delete every audit entry and transaction row with TRUNCATE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audit
|
AuditTrail
|
The |
required |
connection
|
Connection
|
Connection to run on. |
required |