All checks were successful
Go CI/CD / go-ci (push) Successful in 19m8s
This commit introduces a new test, Test_DispatchEventWithError, to verify error handling when dispatching events. It ensures that the dispatcher properly returns an error when an event handler triggers one.
171 lines
3.6 KiB
Go
171 lines
3.6 KiB
Go
package stonecqrs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type testCommand struct {
|
|
message string
|
|
}
|
|
|
|
type testQuery struct {
|
|
message string
|
|
}
|
|
|
|
type testEvent struct {
|
|
message string
|
|
}
|
|
|
|
type unknownEvent struct{}
|
|
|
|
type testCommandHandler struct{}
|
|
|
|
func (handler *testCommandHandler) Handle(
|
|
ctx context.Context, cmd Command,
|
|
) ([]Event, error) {
|
|
fmt.Println(cmd)
|
|
return []Event{
|
|
testEvent{message: "test"},
|
|
}, nil
|
|
}
|
|
|
|
type testQueryHandler struct{}
|
|
|
|
func (handler *testQueryHandler) Handle(
|
|
ctx context.Context, query Query,
|
|
) (interface{}, error) {
|
|
fmt.Println(query)
|
|
return "test", nil
|
|
}
|
|
|
|
type testEventHandler struct{}
|
|
|
|
func (handler *testEventHandler) Handle(
|
|
ctx context.Context, event Event,
|
|
) error {
|
|
fmt.Println(event)
|
|
return nil
|
|
}
|
|
|
|
type testErrorHandler struct{}
|
|
|
|
func (handler *testErrorHandler) Handle(
|
|
ctx context.Context, cmd Command,
|
|
) ([]Event, error) {
|
|
return nil, errors.New("test error")
|
|
}
|
|
|
|
type testErrorQueryHandler struct{}
|
|
|
|
func (handler *testErrorQueryHandler) Handle(
|
|
ctx context.Context, query Query,
|
|
) (interface{}, error) {
|
|
return nil, errors.New("test error")
|
|
}
|
|
|
|
type testErrorEventHandler struct{}
|
|
|
|
func (handler *testErrorEventHandler) Handle(
|
|
ctx context.Context, event Event,
|
|
) error {
|
|
return errors.New("test error")
|
|
}
|
|
|
|
func Test_NewDispatcher(t *testing.T) {
|
|
d := NewDispatcher()
|
|
if d == nil {
|
|
t.Fatal("expected non-nil dispatcher")
|
|
}
|
|
}
|
|
|
|
func Test_Register(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterCommandHandler(testCommand{}, &testCommandHandler{})
|
|
d.RegisterQueryHandler(testQuery{}, &testQueryHandler{})
|
|
d.RegisterEventHandler(testEvent{}, &testEventHandler{})
|
|
}
|
|
|
|
func Test_DispatchCommandWithEvents(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterCommandHandler(testCommand{}, &testCommandHandler{})
|
|
d.RegisterQueryHandler(testQuery{}, &testQueryHandler{})
|
|
d.RegisterEventHandler(testEvent{}, &testEventHandler{})
|
|
|
|
_, err := d.DispatchCommand(context.Background(), testCommand{})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func Test_DispatchQuery(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterQueryHandler(testQuery{}, &testQueryHandler{})
|
|
|
|
_, err := d.DispatchQuery(context.Background(), testQuery{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func Test_UnknownEvent(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterCommandHandler(testCommand{}, &testCommandHandler{})
|
|
d.RegisterQueryHandler(testQuery{}, &testQueryHandler{})
|
|
d.RegisterEventHandler(testEvent{}, &testEventHandler{})
|
|
|
|
_, err := d.DispatchCommand(context.Background(), unknownEvent{})
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
|
|
_, err = d.DispatchQuery(context.Background(), unknownEvent{})
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
|
|
err = d.DispatchEvent(context.Background(), unknownEvent{})
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func Test_DispatchEventWithError(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterCommandHandler(testCommand{}, &testCommandHandler{})
|
|
d.RegisterEventHandler(testEvent{}, &testErrorEventHandler{})
|
|
|
|
err := d.DispatchEvent(context.Background(), testEvent{})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func Test_Error(t *testing.T) {
|
|
d := NewDispatcher()
|
|
d.RegisterCommandHandler(testCommand{}, &testErrorHandler{})
|
|
d.RegisterQueryHandler(testQuery{}, &testErrorQueryHandler{})
|
|
d.RegisterEventHandler(testEvent{}, &testErrorEventHandler{})
|
|
|
|
_, err := d.DispatchCommand(context.Background(), testCommand{})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
|
|
_, err = d.DispatchQuery(context.Background(), testQuery{})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
|
|
err = d.DispatchEvent(context.Background(), testEvent{})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|