Skip to content

Commit

Permalink
add test for CommandBus.SendWithModifiedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Sep 24, 2023
1 parent c75db74 commit be7b7db
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions components/cqrs/command_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/ThreeDotsLabs/watermill/message"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -150,6 +151,55 @@ func TestCommandBus_Send_OnSend(t *testing.T) {
assert.Equal(t, "value", publisher.messages["whatever"][0].Metadata.Get("key"))
}

func TestCommandBus_SendWithModifiedMessage(t *testing.T) {
publisher := newPublisherStub()

cb, err := cqrs.NewCommandBusWithConfig(
publisher,
cqrs.CommandBusConfig{
GeneratePublishTopic: func(params cqrs.CommandBusGeneratePublishTopicParams) (string, error) {
return "whatever", nil
},
Marshaler: cqrs.JSONMarshaler{},
},
)
require.NoError(t, err)

err = cb.SendWithModifiedMessage(context.Background(), TestCommand{}, func(message *message.Message) error {
message.Metadata.Set("key", "value")
return nil
})
require.NoError(t, err)

assert.Equal(t, "value", publisher.messages["whatever"][0].Metadata.Get("key"))
}

func TestCommandBus_SendWithModifiedMessage_modify_error(t *testing.T) {
publisher := newPublisherStub()

cb, err := cqrs.NewCommandBusWithConfig(
publisher,
cqrs.CommandBusConfig{
GeneratePublishTopic: func(params cqrs.CommandBusGeneratePublishTopicParams) (string, error) {
return "whatever", nil
},
Marshaler: cqrs.JSONMarshaler{},
},
)
require.NoError(t, err)

expectedErr := errors.New("some error")

err = cb.SendWithModifiedMessage(
context.Background(),
TestCommand{},
func(message *message.Message) error {
return expectedErr
},
)
assert.ErrorContains(t, err, expectedErr.Error())
}

func TestCommandBus_Send_OnSend_error(t *testing.T) {
publisher := newPublisherStub()

Expand Down

0 comments on commit be7b7db

Please sign in to comment.