-
Notifications
You must be signed in to change notification settings - Fork 2
/
commandbus_test.go
60 lines (54 loc) · 1.71 KB
/
commandbus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cromberbus_test
import (
"context"
"errors"
"fmt"
"testing"
cb "github.com/chiguirez/cromberbus/v3"
"github.com/stretchr/testify/require"
"github.com/chiguirez/cromberbus/v3/commandhandler"
"github.com/chiguirez/cromberbus/v3/middleware"
"github.com/chiguirez/cromberbus/v3/middleware/multierror"
)
//nolint:funlen
func TestCommandBus(t *testing.T) {
t.Run("Given a command a handler and a command bus", func(t *testing.T) {
handler := commandhandler.New(func(ctx context.Context, a struct{}) error {
return nil
})
commandBus := cb.New(handler)
command := struct{}{}
t.Run("When command goes into the bus", func(t *testing.T) {
err := commandBus.Dispatch(context.Background(), command)
t.Run("Then handler is executed", func(t *testing.T) {
require.NoError(t, err)
})
})
})
t.Run("Given a command a handler and a command bus and a middleware", func(t *testing.T) {
handlerErr := fmt.Errorf("handler") //nolint:goerr113
middlewareErr := fmt.Errorf("middleware") //nolint:goerr113
handler := commandhandler.New(func(ctx context.Context, a struct{}) error {
return handlerErr
})
commandBus := cb.New(handler)
commandBus.Use(
middleware.HandlerFunc(
func(ctx context.Context, command middleware.Command, next middleware.NextFn) error {
return multierror.New(
next(ctx, command),
middlewareErr,
)
},
),
)
command := struct{}{}
t.Run("When command goes into the bus", func(t *testing.T) {
err := commandBus.Dispatch(context.Background(), command)
t.Run("Then handler and middleware are executed", func(t *testing.T) {
require.True(t, errors.Is(err, middlewareErr))
require.True(t, errors.Is(err, handlerErr))
})
})
})
}