Some checks failed
Go CI/CD / go-ci (push) Has been cancelled
Introduce error-producing handlers to test command, query, and event dispatch processes. Ensure proper error propagation when handlers encounter failures. Validate that expected errors are correctly returned in all scenarios.
160 lines
3.3 KiB
Go
160 lines
3.3 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_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")
|
|
}
|
|
}
|