Documentation
¶
Overview ¶
Package test contains fixtures and helpers for testing event-sourced entities.
Index ¶
- Constants
- func NewTestEventContext() evt.Context
- type CreateEntity
- type Entity
- func (entity *Entity) Apply(event evt.Event) error
- func (entity *Entity) Base() evt.BaseEntity
- func (entity *Entity) DeserializeEvent(serializedEvent evt.SerializedEvent) (evt.Event, error)
- func (entity *Entity) EventUpcasters() []evt.EventUpcaster
- func (entity *Entity) GetID() evt.EntityID
- func (entity *Entity) Handle(_ context.Context, command evt.Command) (evt.CommandResult, error)
- func (entity *Entity) Projectors() []evt.EventProjector
- func (entity *Entity) Type() evt.EntityType
- type EntityCreated
- type EntityUpdated
- type FakeCommand
- type FakeEvent
- type Framework
- type MockStore
- func (store *MockStore) Commit(ctx context.Context, result evt.CommandResult, eventContext evt.Context, ...) ([]evt.SerializedEvent, error)
- func (store *MockStore) Execute(ctx context.Context, entity evt.Entity, entityID evt.EntityID, ...) error
- func (store *MockStore) LoadEntity(ctx context.Context, entity evt.Entity, entityID evt.EntityID) (evt.Context, error)
- type ReplaceEntity
- type SimpleEvent
Constants ¶
const CreateCommand evt.CommandType = evt.CommandType(string(EntityType) + ":create")
CreateCommand is the type for Create Commands for the Test Entity
const CreatedEvent evt.EventType = evt.EventType(string(EntityType) + ":created")
CreatedEvent is an event type
const EntityType evt.EntityType = "test"
EntityType is a unique entity type
const ReplaceCommand evt.CommandType = evt.CommandType(string(EntityType) + ":update")
ReplaceCommand is the type for Update Commands for the Test Entity
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 ¶
NewTestEventContext creates a new test event context with default values
Types ¶
type CreateEntity ¶
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 NewTestEntity ¶
NewTestEntity creates a new test Entity with the given ID
func (*Entity) Apply ¶
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 ¶
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) 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
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) EntityType ¶
func (f *FakeEvent) EntityType() evt.EntityType
EntityType returns the entity 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
type MockStore ¶
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 (*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
type ReplaceEntity ¶
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) Version ¶
func (e *SimpleEvent) Version() evt.EventVersion
Version returns the event version