ditto.snapshot¶
Snapshot fixture implementation and supporting types.
SnapshotKey(module, group_name, key, identifier)
dataclass
¶
Fully-qualified identity for a single snapshot value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
Rootdir-relative test file stem, e.g. "tests/bar/test_api". Provides namespace isolation across files in shared backends. |
required |
group_name
|
str
|
Test function name, e.g. "test_something". For class-based tests includes the class prefix: "TestClass.test_something". |
required |
key
|
str
|
Per-snapshot identifier within the test. |
required |
identifier
|
str
|
Recorder identifier, e.g. "json", "yaml", "pandas.parquet". |
required |
filename
property
¶
Short key: 'group@key.ext'. Not used as the storage key for any backend.
Kept for reference and user code that inspects SnapshotKey objects.
File backends use _flat_key ('module.group@key.ext') and remote backends
use str(key) ('module/group@key.ext').
display_name
property
¶
Human-readable label for the session report: 'module/group@key.ext'.
__str__()
¶
Namespaced key for remote backends: 'module/group@key.ext'.
Unique across all test files in a shared backend (Redis, S3, etc.). Also used as the human-readable display name in session reports.
Source code in ditto/snapshot.py
LockSeen(target_id, scheme, nodeid, key, recorder)
dataclass
¶
A lock entry observed this session, plus where it lives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_id
|
str
|
Portable lock-file target id (rootdir-relative for |
required |
scheme
|
str
|
The target's URI scheme, e.g. |
required |
nodeid
|
str
|
Full pytest node id for the owning test, e.g. |
required |
key
|
str
|
Per-snapshot identifier within the test. |
required |
recorder
|
str
|
Recorder identifier, e.g. |
required |
SnapshotMode
¶
Bases: Enum
How resolve_snapshot treats the stored value for a key.
Attributes:
| Name | Type | Description |
|---|---|---|
RECORD |
Save the value when the key is absent; otherwise return the stored value. |
|
UPDATE |
Always save the value, overwriting a stored one. Set by |
|
VERIFY |
Never write: return the stored value, or the given value when the key is
absent. Set by |
|
A value that is saved, or returned under VERIFY for an absent key, is |
|
|
serialised and deserialised first, and the deserialised value is returned. |
|
Snapshot(group_name, module, target, _backend, recorder=_UNSET, recorder_name=_UNSET, mode=SnapshotMode.RECORD, nodeid='', target_id='', _tracker=_SessionTracker())
dataclass
¶
Immutable configuration for a snapshot: where to store it and how to record it.
Instances are created by the snapshot fixture and hold no I/O state.
All persistence is handled by the module-level free functions
save_snapshot, load_snapshot, and resolve_snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Prefix used in snapshot keys. Derived from the pytest nodeid minus the file path (e.g. "test_something" or "TestClass.test_something"). |
required |
module
|
str
|
Rootdir-relative test file stem (e.g. "tests/bar/test_api"). Required for all backends. Provides namespace isolation across test files. |
required |
target
|
str
|
URI identifying the storage location. The scheme controls key format:
|
required |
_backend
|
MutableMapping[str, bytes]
|
Resolved storage backend. Conventionally private — set by the fixture via
|
required |
recorder
|
Recorder
|
Serialisation strategy. Defaults to strict JSON. |
_UNSET
|
recorder_name
|
str
|
The recorder's registered name, which is its persisted identifier: it
ends snapshot filenames and is recorded in |
_UNSET
|
mode
|
SnapshotMode
|
Whether a snapshot is recorded, updated, or only verified. Defaults to
|
RECORD
|
nodeid
|
str
|
Full pytest node id for the owning test, e.g. |
''
|
target_id
|
str
|
Portable lock-file target id (rootdir-relative for |
''
|
_tracker
|
_SessionTracker
|
Where snapshot activity is recorded. The fixture passes its pytest
session's tracker; a directly constructed |
_SessionTracker()
|
__call__(data, key)
¶
Save or load the snapshot for key.
Delegates to resolve_snapshot: saves data on first call and
returns the stored value on subsequent calls. Either way the value
returned is what the recorder reads back, not data itself.
Source code in ditto/snapshot.py
save_snapshot(snapshot, data, key)
¶
Persist data to the backend as the snapshot for key.
Nothing is written if the recorder cannot deserialise the bytes it produced.
Source code in ditto/snapshot.py
load_snapshot(snapshot, key)
¶
Load and return the stored snapshot value for key.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
When no snapshot exists for |
Source code in ditto/snapshot.py
resolve_snapshot(snapshot, data, key)
¶
Return the snapshot value for key, first writing data if the mode requires.
How the stored value is treated depends on snapshot.mode; see SnapshotMode.
Whenever data is returned in place of a stored value, it is first passed
through the recorder (dumps, then loads), so the caller's assertion sees
what a later run would read back.
Raises:
| Type | Description |
|---|---|
DuplicateSnapshotKeyError
|
When the same |