CQRS Implementation #1

Merged
Rene Nochebuena merged 4 commits from feature/cqrs-implementation into develop 2025-04-13 09:35:48 -06:00
Showing only changes of commit 5bea19ecf1 - Show all commits

View File

@ -2,6 +2,7 @@ package stonecqrs
import (
"context"
"errors"
"fmt"
"testing"
)
@ -49,6 +50,30 @@ func (handler *testEventHandler) Handle(
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 {
@ -98,7 +123,7 @@ func Test_UnknownEvent(t *testing.T) {
t.Fatal("expected error")
}
err = d.DispatchEvent(context.Background(), unknownEvent{})
_, err = d.DispatchQuery(context.Background(), unknownEvent{})
if err == nil {
t.Fatal("expected error")
@ -110,3 +135,25 @@ func Test_UnknownEvent(t *testing.T) {
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")
}
}