ds_common_placeholder_py_lib.testing¶
File: testing.py
Region: ds-common-placeholder-py-lib
Description¶
Test helpers, for tests and offline tooling. Not for code that serves runs.
Classes¶
What |
Functions¶
|
Replace ds-vault while the block runs. |
|
Make the current time at while the block runs: for the date |
Module Contents¶
- class ds_common_placeholder_py_lib.testing.FakeVault[source]¶
What
fake_vault()serves, and every key it was asked for.- values: dict[str, bytes | str | Exception]¶
- default: bytes | str | None = None¶
- reads: list[str] = []¶
- ds_common_placeholder_py_lib.testing.fake_vault(values: collections.abc.Mapping[str, bytes | str | Exception] | None = None, *, default: bytes | str | None = None) collections.abc.Iterator[FakeVault][source]¶
Replace ds-vault while the block runs.
ctx = Context.from_values({"tenant_id": "t1", "db": {"pw": "@{get_secret, /ds/vault/s/t1/k/prod}"}}) with fake_vault({"/ds/vault/s/t1/k/prod": b"c2VjcmV0"}) as vault: ctx.resolve("db.pw") assert vault.reads == ["/ds/vault/s/t1/k/prod"]
The tenant check still runs first, so a key of another tenant never reaches the fake. The fake applies only to the current thread or async task (a context variable); threads started inside the block do not see it, and other threads keep reading ds-vault. Code that resolves in a thread pool needs the context passed along, for example
executor.submit(contextvars.copy_context().run, fn).- Parameters:
values – Vault key to stored value. An exception value is raised, to simulate a failing read.
default – Value for keys not in values;
Nonemeans not found.
- Yields:
The fake, with
readsrecording every key read.
- ds_common_placeholder_py_lib.testing.fixed_now(at: str | datetime.datetime) collections.abc.Iterator[None][source]¶
Make the current time at while the block runs: for the date functions given no time, and
get_iso_timestamp_nowandget_unix_timestamp_now.ctx = Context.from_values({"tenant_id": "t1", "run": {"at": "@{get_unix_timestamp_now}"}}) with fixed_now("2026-11-03T10:15:30Z"): ctx.resolve("run.at") # 1793700930
A date function given a time (
@{get_hour, UTC, run.logical_at}) is unaffected. Likefake_vault(), it applies to the current thread or async task only, and nests. A thread started inside the block reads the real time; pass the context along, e.g.executor.submit(contextvars.copy_context().run, fn).- Parameters:
at – An aware datetime, or an ISO 8601 string with a UTC offset.
- Raises:
ValueError – If at has no UTC offset.