TestEngine

zio.blocks.projection.testing.TestEngine
See theTestEngine companion object
final class TestEngine

A running ProjectionEngine wired entirely in memory, for tests and examples.

For every projection it creates an InMemoryProjectionStore, and for every distinct source name used by the projections it creates an in-memory event store backed by a Hub. If no projection declares a source, a single "_default" source is created. The engine is started when the TestEngine is built and stopped when the enclosing Scope closes, so no database or files are involved. This replaces the verbose ProjectionEngine.makeWithStores wiring.

Use append to feed events in and query to read the projected rows back. Appending only publishes the event; the engine processes it asynchronously, so a query issued straight after an append may not see the result yet.

Events are stored with the event class's simple name as their tag, and they are kept in memory only.

val projection = Projection[UserProfile]("userProfiles")
 .from("users").routeToSelf
 .on[UserCreated].insert((e, ctx) => UserProfile(ctx.entityId, e.name, e.email))

val program = ZIO.scoped {
 for {
   engine <- TestEngine.make(projection)
   _      <- engine.append("user-1", UserCreated("Alice", "a@b.com"))
   u1     <- engine.query(projection, "user-1")
 } yield u1
}

Value parameters

engine

the underlying engine that processes the events

projections

the projections this engine runs, in the order they were given

stores

the in-memory store of each projection, keyed by projection name

Attributes

Companion
object
Source
TestEngine.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def append[E : Schema](entityId: String, event: E): Task[Unit]

Appends an event for the given entity.

Appends an event for the given entity.

With a single source the event goes to it and any failure is returned. With several sources the event goes to every source whose binding has a handler for the event's runtime class, or to all sources if none has. In the several-sources case a failure while appending to one source is ignored.

To pick the source yourself, use the overload that takes a source name.

Value parameters

entityId

the entity the event is recorded under, exposed to handlers as ctx.entityId

event

the event to append

Attributes

Source
TestEngine.scala
def append[E : Schema](entityId: String, event: E, source: String): Task[Unit]

Appends an event to one named source.

Appends an event to one named source.

Fails with a RuntimeException listing the known sources if source is not one of them.

Value parameters

source

the source name, as passed to Projection.from

Attributes

Source
TestEngine.scala
def query[A](projection: Projection[A], id: String): Task[Option[A]]

Reads the row with the given ID from a projection, or None if there is none.

Reads the row with the given ID from a projection, or None if there is none.

Value parameters

id

the entity ID of the row

projection

the projection to read from

Attributes

Source
TestEngine.scala
def queryByName[A](specName: String, id: String): Task[Option[A]]

Like query, but finds the projection by its name instead of by value. The caller chooses the result type A, and it is not checked.

Like query, but finds the projection by its name instead of by value. The caller chooses the result type A, and it is not checked.

Attributes

Source
TestEngine.scala

The underlying ProjectionEngine, same as engine.

The underlying ProjectionEngine, same as engine.

Attributes

Source
TestEngine.scala

Concrete fields

Attributes

Source
TestEngine.scala
val projections: List[Projection[_]]

Attributes

Source
TestEngine.scala
val stores: Map[String, ProjectionStore[_]]

Attributes

Source
TestEngine.scala