logicblocks.event.testing
logicblocks.event.testing
Section titled “logicblocks.event.testing”BaseProjectionBuilder
Section titled “BaseProjectionBuilder”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 aStreamIdentifierwith random category and stream names.state(State | None) — The projection state. Defaults to the result ofdefault_state_factory.metadata(Metadata | None) — The projection metadata. Defaults to the result ofdefault_metadata_factory.
id: str
name: str
source
Section titled “source”source: Projectable
state: State
metadata
Section titled “metadata”metadata: Metadata
default_state_factory
Section titled “default_state_factory”def default_state_factory() -> State
Create the state to use when none is given.
Returns:
State— the default projection state.
default_metadata_factory
Section titled “default_metadata_factory”def default_metadata_factory() -> Metadata
Create the metadata to use when none is given.
Returns:
Metadata— the default projection metadata.
with_id
Section titled “with_id”def with_id(id: str) -> Self
Return a copy of the builder with the given id.
with_name
Section titled “with_name”def with_name(name: str) -> Self
Return a copy of the builder with the given name.
with_source
Section titled “with_source”def with_source(source: Projectable) -> Self
Return a copy of the builder with the given source.
with_state
Section titled “with_state”def with_state(state: State) -> Self
Return a copy of the builder with the given state.
with_metadata
Section titled “with_metadata”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]— aProjectionwith the builder’s fields.
MappingProjectionBuilder
Section titled “MappingProjectionBuilder”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.
default_state_factory
Section titled “default_state_factory”def default_state_factory() -> Mapping[str, Any]
Create a random string-to-string mapping.
default_metadata_factory
Section titled “default_metadata_factory”def default_metadata_factory() -> Mapping[str, Any]
Create a random string-to-string mapping.
NewEventBuilder
Section titled “NewEventBuilder”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
Section titled “payload”payload: Payload
metadata
Section titled “metadata”metadata: Metadata
occurred_at
Section titled “occurred_at”occurred_at: datetime | None
observed_at
Section titled “observed_at”observed_at: datetime | None
with_name
Section titled “with_name”def with_name(name: Name)
Return a copy of the builder with the given name.
with_payload
Section titled “with_payload”def with_payload(payload: Payload)
Return a copy of the builder with the given payload.
with_metadata
Section titled “with_metadata”def with_metadata(metadata: Metadata)
Return a copy of the builder with the given metadata.
with_occurred_at
Section titled “with_occurred_at”def with_occurred_at(occurred_at: datetime | None)
Return a copy of the builder with the given occurrence time.
with_observed_at
Section titled “with_observed_at”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.
StoredEventBuilder
Section titled “StoredEventBuilder”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 defaultobserved_at.
id: str
name: Name
stream
Section titled “stream”stream: str
category
Section titled “category”category: str
position
Section titled “position”position: int
sequence_number
Section titled “sequence_number”sequence_number: int
payload
Section titled “payload”payload: Payload
metadata
Section titled “metadata”metadata: Metadata
occurred_at
Section titled “occurred_at”occurred_at: datetime
observed_at
Section titled “observed_at”observed_at: datetime
from_new_event
Section titled “from_new_event”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.
with_id
Section titled “with_id”def with_id(id: str)
Return a copy of the builder with the given id.
with_name
Section titled “with_name”def with_name(name: Name)
Return a copy of the builder with the given name.
with_stream
Section titled “with_stream”def with_stream(stream: str)
Return a copy of the builder with the given stream name.
with_category
Section titled “with_category”def with_category(category: str)
Return a copy of the builder with the given category name.
with_position
Section titled “with_position”def with_position(position: int)
Return a copy of the builder with the given stream position.
with_sequence_number
Section titled “with_sequence_number”def with_sequence_number(sequence_number: int)
Return a copy of the builder with the given sequence number.
with_payload
Section titled “with_payload”def with_payload(payload: Payload)
Return a copy of the builder with the given payload.
with_metadata
Section titled “with_metadata”def with_metadata(metadata: Metadata)
Return a copy of the builder with the given metadata.
with_occurred_at
Section titled “with_occurred_at”def with_occurred_at(occurred_at: datetime | None)
Return a copy of the builder with the given occurrence time.
with_observed_at
Section titled “with_observed_at”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.
logicblocks.event.testing.data
Section titled “logicblocks.event.testing.data”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.
random_int
Section titled “random_int”def random_int(min: int = 0, max: int = 99999999999999) -> int
Generate a random integer between min and max inclusive.
random_string
Section titled “random_string”def random_string(characters: str, length: int = 1) -> str
Generate a random string of length drawn from characters.
random_lowercase_ascii_alphabetics_string
Section titled “random_lowercase_ascii_alphabetics_string”def random_lowercase_ascii_alphabetics_string(length: int = 1) -> str
Generate a random string of lowercase ASCII letters.
random_uppercase_ascii_alphabetics_string
Section titled “random_uppercase_ascii_alphabetics_string”def random_uppercase_ascii_alphabetics_string(length: int = 1) -> str
Generate a random string of uppercase ASCII letters.
random_ascii_alphanumerics_string
Section titled “random_ascii_alphanumerics_string”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.
random_uuid4_string
Section titled “random_uuid4_string”def random_uuid4_string() -> str
Generate a random UUID4 as a string.
random_event_category_name
Section titled “random_event_category_name”def random_event_category_name() -> str
Generate a random 10 character category name.
random_event_stream_name
Section titled “random_event_stream_name”def random_event_stream_name() -> str
Generate a random 42 character stream name.
random_event_name
Section titled “random_event_name”def random_event_name() -> str
Generate a random 15 character event name.
random_event_id
Section titled “random_event_id”def random_event_id() -> str
Generate a random event id, as a UUID4 string.
random_event_position
Section titled “random_event_position”def random_event_position() -> int
Generate a random stream position.
random_event_sequence_number
Section titled “random_event_sequence_number”def random_event_sequence_number() -> int
Generate a random sequence number.
random_event_payload
Section titled “random_event_payload”def random_event_payload() -> Mapping[str, Any]
Generate a random event payload mapping.
random_event_metadata
Section titled “random_event_metadata”def random_event_metadata() -> Mapping[str, Any]
Generate a random event metadata mapping.
random_projection_id
Section titled “random_projection_id”def random_projection_id() -> str
Generate a random projection id, as a UUID4 string.
random_projection_name
Section titled “random_projection_name”def random_projection_name() -> str
Generate a random 15 character projection name.
random_projection_state
Section titled “random_projection_state”def random_projection_state() -> Mapping[str, Any]
Generate a random projection state mapping.
random_projection_metadata
Section titled “random_projection_metadata”def random_projection_metadata() -> Mapping[str, Any]
Generate a random projection metadata mapping.
random_node_id
Section titled “random_node_id”def random_node_id() -> str
Generate a random node id, as a UUID4 string.
random_subscriber_group
Section titled “random_subscriber_group”def random_subscriber_group() -> str
Generate a random 15 character subscriber group name.
random_subscriber_id
Section titled “random_subscriber_id”def random_subscriber_id() -> str
Generate a random subscriber id, as a UUID4 string.