Skip to content

logicblocks.event.testing

class BaseProjectionBuilder[State = JsonValue, Metadata = JsonValue](
    *,
    id: str | None = None,
    name: str | None = None,
    source: Projectable | None = None,
    state: State | None = None,
    metadata: Metadata | None = None,
)

Bases: ABC

Base class for builders of Projection instances for tests.

Subclasses implement default_state_factory and default_metadata_factory to supply the state and metadata used when none is given. Each with_* method returns a new builder, of the same class, with that field replaced.

Type parameters:

  • State — The type of the projection state.
  • Metadata — The type of the projection metadata.

Parameters:

  • id (str | None) — The projection id. Defaults to a random UUID.
  • name (str | None) — The projection name. Defaults to a random name.
  • source (Projectable | None) — The source identifier. Defaults to a StreamIdentifier with random category and stream names.
  • state (State | None) — The projection state. Defaults to the result of default_state_factory.
  • metadata (Metadata | None) — The projection metadata. Defaults to the result of default_metadata_factory.
id: str
name: str
source: Projectable
state: State
metadata: Metadata
def default_state_factory() -> State

Create the state to use when none is given.

Returns:

  • State — the default projection state.
def default_metadata_factory() -> Metadata

Create the metadata to use when none is given.

Returns:

  • Metadata — the default projection metadata.
def with_id(id: str) -> Self

Return a copy of the builder with the given id.

def with_name(name: str) -> Self

Return a copy of the builder with the given name.

def with_source(source: Projectable) -> Self

Return a copy of the builder with the given source.

def with_state(state: State) -> Self

Return a copy of the builder with the given state.

def with_metadata(metadata: Metadata) -> Self

Return a copy of the builder with the given metadata.

def build() -> Projection[State, Metadata]

Build the projection.

Returns:

  • Projection[State, Metadata] — a Projection with the builder’s fields.
class MappingProjectionBuilder(
    *,
    id: str | None = None,
    name: str | None = None,
    source: Projectable | None = None,
    state: State | None = None,
    metadata: Metadata | None = None,
)

Bases: BaseProjectionBuilder[Mapping[str, Any], Mapping[str, Any]]

Projection builder whose state and metadata are mappings.

State and metadata default to random string-to-string mappings.

def default_state_factory() -> Mapping[str, Any]

Create a random string-to-string mapping.

def default_metadata_factory() -> Mapping[str, Any]

Create a random string-to-string mapping.

class NewEventBuilder[Name = str, Payload = JsonValue, Metadata = JsonValue](
    *,
    name: Name | None = None,
    payload: Payload | None = None,
    metadata: Metadata | None = None,
    occurred_at: datetime | None = None,
    observed_at: datetime | None = None,
)

Immutable builder of NewEvent instances for tests.

Each with_* method returns a new builder with that field replaced. Fields that are not provided default to random values, except the timestamps, which default to None so that NewEvent assigns them on build.

Parameters:

  • name (Name | None) — The event name. Defaults to a random name.
  • payload (Payload | None) — The event payload. Defaults to a random mapping.
  • metadata (Metadata | None) — The event metadata. Defaults to a random mapping.
  • occurred_at (datetime | None) — When the event occurred.
  • observed_at (datetime | None) — When the event was observed.
name: Name
payload: Payload
metadata: Metadata
occurred_at: datetime | None
observed_at: datetime | None
def with_name(name: Name)

Return a copy of the builder with the given name.

def with_payload(payload: Payload)

Return a copy of the builder with the given payload.

def with_metadata(metadata: Metadata)

Return a copy of the builder with the given metadata.

def with_occurred_at(occurred_at: datetime | None)

Return a copy of the builder with the given occurrence time.

def with_observed_at(observed_at: datetime | None)

Return a copy of the builder with the given observation time.

def build()

Build a NewEvent from the builder’s fields.

class StoredEventBuilder[Name = str, Payload = JsonValue, Metadata = JsonValue](
    *,
    id: str | None = None,
    name: Name | None = None,
    stream: str | None = None,
    category: str | None = None,
    position: int | None = None,
    sequence_number: int | None = None,
    payload: Payload | None = None,
    metadata: Metadata | None = None,
    occurred_at: datetime | None = None,
    observed_at: datetime | None = None,
    clock: Clock = SystemClock(),
)

Immutable builder of StoredEvent instances for tests.

Each with_* method returns a new builder with that field replaced. Fields that are not provided default to random values, except the timestamps: observed_at defaults to the current time from clock, in UTC, and occurred_at defaults to observed_at.

Parameters:

  • id (str | None) — The event id. Defaults to a random UUID.
  • name (Name | None) — The event name. Defaults to a random name.
  • stream (str | None) — The stream name. Defaults to a random name.
  • category (str | None) — The category name. Defaults to a random name.
  • position (int | None) — The position in the stream. Defaults to a random number.
  • sequence_number (int | None) — The sequence number. Defaults to a random number.
  • payload (Payload | None) — The event payload. Defaults to a random mapping.
  • metadata (Metadata | None) — The event metadata. Defaults to a random mapping.
  • occurred_at (datetime | None) — When the event occurred.
  • observed_at (datetime | None) — When the event was observed.
  • clock (Clock) — The clock used to default observed_at.
id: str
name: Name
stream: str
category: str
position: int
sequence_number: int
payload: Payload
metadata: Metadata
occurred_at: datetime
observed_at: datetime
def from_new_event(event: NewEvent[Name, Payload, Metadata])

Return a copy of the builder with fields taken from a new event.

Copies the name, payload, metadata and timestamps, leaving the id, stream, category, position and sequence number unchanged.

Parameters:

  • event (NewEvent[Name, Payload, Metadata]) — The new event to copy fields from.
def with_id(id: str)

Return a copy of the builder with the given id.

def with_name(name: Name)

Return a copy of the builder with the given name.

def with_stream(stream: str)

Return a copy of the builder with the given stream name.

def with_category(category: str)

Return a copy of the builder with the given category name.

def with_position(position: int)

Return a copy of the builder with the given stream position.

def with_sequence_number(sequence_number: int)

Return a copy of the builder with the given sequence number.

def with_payload(payload: Payload)

Return a copy of the builder with the given payload.

def with_metadata(metadata: Metadata)

Return a copy of the builder with the given metadata.

def with_occurred_at(occurred_at: datetime | None)

Return a copy of the builder with the given occurrence time.

def with_observed_at(observed_at: datetime | None)

Return a copy of the builder with the given observation time.

def build()

Build a StoredEvent from the builder’s fields.

Random test data generators.

Generators are not seeded, so values differ between runs. Unless stated otherwise, generated names are lowercase ASCII letters and hyphens, never starting or ending with a hyphen, and generated mappings have between 1 and 10 entries mapping 10 character lowercase keys to 20 character alphanumeric values.

def random_int(min: int = 0, max: int = 99999999999999) -> int

Generate a random integer between min and max inclusive.

def random_string(characters: str, length: int = 1) -> str

Generate a random string of length drawn from characters.

def random_lowercase_ascii_alphabetics_string(length: int = 1) -> str

Generate a random string of lowercase ASCII letters.

def random_uppercase_ascii_alphabetics_string(length: int = 1) -> str

Generate a random string of uppercase ASCII letters.

def random_ascii_alphanumerics_string(length: int = 1) -> str

Generate a random string of ASCII letters and digits.

random_hyphenated_lowercase_ascii_alphabetics_string

Section titled “random_hyphenated_lowercase_ascii_alphabetics_string”
def random_hyphenated_lowercase_ascii_alphabetics_string(
    length: int = 1,
) -> str

Generate a random name of lowercase ASCII letters and hyphens.

def random_uuid4_string() -> str

Generate a random UUID4 as a string.

def random_event_category_name() -> str

Generate a random 10 character category name.

def random_event_stream_name() -> str

Generate a random 42 character stream name.

def random_event_name() -> str

Generate a random 15 character event name.

def random_event_id() -> str

Generate a random event id, as a UUID4 string.

def random_event_position() -> int

Generate a random stream position.

def random_event_sequence_number() -> int

Generate a random sequence number.

def random_event_payload() -> Mapping[str, Any]

Generate a random event payload mapping.

def random_event_metadata() -> Mapping[str, Any]

Generate a random event metadata mapping.

def random_projection_id() -> str

Generate a random projection id, as a UUID4 string.

def random_projection_name() -> str

Generate a random 15 character projection name.

def random_projection_state() -> Mapping[str, Any]

Generate a random projection state mapping.

def random_projection_metadata() -> Mapping[str, Any]

Generate a random projection metadata mapping.

def random_node_id() -> str

Generate a random node id, as a UUID4 string.

def random_subscriber_group() -> str

Generate a random 15 character subscriber group name.

def random_subscriber_id() -> str

Generate a random subscriber id, as a UUID4 string.