test

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package test contains fixtures and helpers for testing event-sourced entities.

Index

Constants

View Source
const CreateCommand evt.CommandType = evt.CommandType(string(EntityType) + ":create")

CreateCommand is the type for Create Commands for the Test Entity

View Source
const CreatedEvent evt.EventType = evt.EventType(string(EntityType) + ":created")

CreatedEvent is an event type

View Source
const EntityType evt.EntityType = "test"

EntityType is a unique entity type

View Source
const ReplaceCommand evt.CommandType = evt.CommandType(string(EntityType) + ":update")

ReplaceCommand is the type for Update Commands for the Test Entity

View Source
const UpdatedEvent evt.EventType = evt.EventType(string(EntityType) + ":updated")

UpdatedEvent represents the event type with a string unique to the entity type

Variables

This section is empty.

Functions

func NewTestEventContext

func NewTestEventContext() evt.Context

NewTestEventContext creates a new test event context with default values

Types

type CreateEntity

type CreateEntity struct {
	Value string
	Other *string
}

CreateEntity is the struct representing a Create Command

func (CreateEntity) EntityType

func (c CreateEntity) EntityType() evt.EntityType

EntityType returns the EntityType constant

func (CreateEntity) Type

func (c CreateEntity) Type() evt.CommandType

Type returns the CreateCommand type constant

type Entity

type Entity struct {
	evt.BaseEntity
	Value string  `json:"value"`
	Other *string `json:"other"`
}

The Entity handles commands and applies events for testing

func NewEntity

func NewEntity(id evt.EntityID) *Entity

NewEntity returns an Entity instance for the given id

func NewTestEntity

func NewTestEntity(id string) *Entity

NewTestEntity creates a new test Entity with the given ID

func (*Entity) Apply

func (entity *Entity) Apply(event evt.Event) error

Apply takes an event and applies it to the current state of this entity instance. Unknown events return a BadEventError (fail-closed).

func (*Entity) Base

func (entity *Entity) Base() evt.BaseEntity

Base returns the base entity fields

func (*Entity) DeserializeEvent

func (entity *Entity) DeserializeEvent(serializedEvent evt.SerializedEvent) (evt.Event, error)

DeserializeEvent allows the event store to deserialize an event for this entity

func (*Entity) EventUpcasters

func (entity *Entity) EventUpcasters() []evt.EventUpcaster

EventUpcasters returns the upcasters for events within this Entity

func (*Entity) GetID

func (entity *Entity) GetID() evt.EntityID

GetID returns the ID for this Entity instance

func (*Entity) Handle

func (entity *Entity) Handle(_ context.Context, command evt.Command) (evt.CommandResult, error)

Handle handles a command, yielding Events or an error

func (*Entity) Projectors

func (entity *Entity) Projectors() []evt.EventProjector

Projectors returns the projectors for this Entity

func (*Entity) Type

func (entity *Entity) Type() evt.EntityType

Type returns the Entity type constant

type EntityCreated

type EntityCreated struct {
	ID    evt.EntityID `json:"id"`
	Value string       `json:"value"`
	Other *string      `json:"other"`
}

EntityCreated is the "created" event

func (EntityCreated) EntityID

func (e EntityCreated) EntityID() evt.EntityID

EntityID returns the ID for this entity

func (EntityCreated) EntityType

func (e EntityCreated) EntityType() evt.EntityType

EntityType returns the entity type constant for this event

func (EntityCreated) Type

func (e EntityCreated) Type() evt.EventType

Type returns the event type constant

func (EntityCreated) Version

func (e EntityCreated) Version() evt.EventVersion

Version returns the version of the event schema - when changed, increment this

type EntityUpdated

type EntityUpdated struct {
	ID    evt.EntityID `json:"id"`
	Value string       `json:"value"`
	Other *string      `json:"other"`
}

EntityUpdated is the "updated" event

func (EntityUpdated) EntityID

func (e EntityUpdated) EntityID() evt.EntityID

EntityID returns the Entity entity ID

func (EntityUpdated) EntityType

func (e EntityUpdated) EntityType() evt.EntityType

EntityType returns the Entity type constant for this event

func (EntityUpdated) Type

func (e EntityUpdated) Type() evt.EventType

Type returns the event type constant

func (EntityUpdated) Version

func (e EntityUpdated) Version() evt.EventVersion

Version returns the version of the event schema - when changed, increment this

type FakeCommand

type FakeCommand struct{}

FakeCommand is a dummy command that should not be recognized by the entity

func (*FakeCommand) EntityType

func (f *FakeCommand) EntityType() evt.EntityType

EntityType is a required method

func (*FakeCommand) Type

func (f *FakeCommand) Type() evt.CommandType

Type is a required method

type FakeEvent

type FakeEvent struct{}

FakeEvent is a dummy event that should not be recognized by the entity. It implements the Event interface to test fail-closed Apply behavior.

func (*FakeEvent) EntityID

func (f *FakeEvent) EntityID() evt.EntityID

EntityID returns an empty entity ID

func (*FakeEvent) EntityType

func (f *FakeEvent) EntityType() evt.EntityType

EntityType returns the entity type

func (*FakeEvent) Type

func (f *FakeEvent) Type() evt.EventType

Type returns a fake event type

func (*FakeEvent) Version

func (f *FakeEvent) Version() evt.EventVersion

Version returns the version

type Framework

type Framework struct {
	// contains filtered or unexported fields
}

Framework provides utilities for testing event-sourced entities

func NewFramework

func NewFramework(repo evt.Repository, store evt.Store, factory evt.EntityFactory, id evt.EntityID) *Framework

NewFramework creates a new testing framework for event-sourced entities.

func NewFrameworkWithFactory

func NewFrameworkWithFactory(
	repo evt.Repository,
	store evt.Store,
	factory evt.EntityFactory,
	id evt.EntityID,
) *Framework

NewFrameworkWithFactory creates a new testing framework from an aggregate constructor.

func (*Framework) Execute

func (f *Framework) Execute(ctx context.Context, cmd evt.Command, metadata evt.Metadata) (evt.Entity, error)

Execute runs the given command against the entity and persists any resulting events to the repository

func (*Framework) Load

func (f *Framework) Load(ctx context.Context) (evt.Entity, error)

Load retrieves the current state of the entity from the store

func (*Framework) WithEvents

func (f *Framework) WithEvents(ctx context.Context, events []evt.Event) (evt.Entity, error)

WithEvents applies the given events to the entity and persists them to the repository

type MockStore

type MockStore struct {
	mock.Mock
}

MockStore is a mock store that satisfies the `EventStore` interface

func NewMockStore

func NewMockStore() *MockStore

NewMockStore automatically initializes a testify mock on creating a new instance

func NewTestStore

func NewTestStore() *MockStore

NewTestStore creates a new MockStore for testing

func (*MockStore) Commit

func (store *MockStore) Commit(ctx context.Context, result evt.CommandResult, eventContext evt.Context, metadata evt.Metadata) ([]evt.SerializedEvent, error)

Commit mocks the corresponding method on the Store

func (*MockStore) Execute

func (store *MockStore) Execute(ctx context.Context, entity evt.Entity, entityID evt.EntityID, command evt.Command, metadata evt.Metadata) error

Execute mocks the corresponding method on the Store

func (*MockStore) LoadEntity

func (store *MockStore) LoadEntity(ctx context.Context, entity evt.Entity, entityID evt.EntityID) (evt.Context, error)

LoadEntity mocks the corresponding method on the Store

type ReplaceEntity

type ReplaceEntity struct {
	Value string
	Other *string
}

ReplaceEntity is the struct representing an Update Command

func (ReplaceEntity) EntityType

func (c ReplaceEntity) EntityType() evt.EntityType

EntityType returns the EntityType constant

func (ReplaceEntity) Type

func (c ReplaceEntity) Type() evt.CommandType

Type returns the ReplaceCommand type constant

type SimpleEvent

type SimpleEvent struct {
	// contains filtered or unexported fields
}

SimpleEvent is a simple event for integration testing

func NewTestEvent

func NewTestEvent(eventType string, payload map[string]any) *SimpleEvent

NewTestEvent creates a new SimpleEvent with the given type and payload

func (*SimpleEvent) EntityID

func (e *SimpleEvent) EntityID() evt.EntityID

EntityID returns an empty entity ID (set during serialization)

func (*SimpleEvent) EntityType

func (e *SimpleEvent) EntityType() evt.EntityType

EntityType returns the entity type for this event

func (*SimpleEvent) MarshalJSON

func (e *SimpleEvent) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for SimpleEvent

func (*SimpleEvent) Type

func (e *SimpleEvent) Type() evt.EventType

Type returns the event type

func (*SimpleEvent) Version

func (e *SimpleEvent) Version() evt.EventVersion

Version returns the event version

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL