mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package handler_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/micro/services/chats/handler"
|
|
pb "github.com/micro/services/chats/proto"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreateMessage(t *testing.T) {
|
|
h := testHandler(t)
|
|
|
|
// seed some data
|
|
var cRsp pb.CreateChatResponse
|
|
err := h.CreateChat(context.TODO(), &pb.CreateChatRequest{
|
|
UserIds: []string{uuid.New().String(), uuid.New().String()},
|
|
}, &cRsp)
|
|
if err != nil {
|
|
t.Fatalf("Error creating chat: %v", err)
|
|
return
|
|
}
|
|
|
|
tt := []struct {
|
|
Name string
|
|
AuthorID string
|
|
ChatID string
|
|
Text string
|
|
Error error
|
|
}{
|
|
{
|
|
Name: "MissingChatID",
|
|
Text: "HelloWorld",
|
|
AuthorID: uuid.New().String(),
|
|
Error: handler.ErrMissingChatID,
|
|
},
|
|
{
|
|
Name: "MissingAuthorID",
|
|
ChatID: uuid.New().String(),
|
|
Text: "HelloWorld",
|
|
Error: handler.ErrMissingAuthorID,
|
|
},
|
|
{
|
|
Name: "MissingText",
|
|
ChatID: uuid.New().String(),
|
|
AuthorID: uuid.New().String(),
|
|
Error: handler.ErrMissingText,
|
|
},
|
|
{
|
|
Name: "ChatNotFound",
|
|
ChatID: uuid.New().String(),
|
|
AuthorID: uuid.New().String(),
|
|
Text: "HelloWorld",
|
|
Error: handler.ErrNotFound,
|
|
},
|
|
{
|
|
Name: "Valid",
|
|
ChatID: cRsp.Chat.Id,
|
|
AuthorID: uuid.New().String(),
|
|
Text: "HelloWorld",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
var rsp pb.CreateMessageResponse
|
|
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
|
Text: tc.Text, ChatId: tc.ChatID, AuthorId: tc.AuthorID,
|
|
}, &rsp)
|
|
|
|
assert.Equal(t, tc.Error, err)
|
|
if tc.Error != nil {
|
|
assert.Nil(t, rsp.Message)
|
|
return
|
|
}
|
|
|
|
assertMessagesMatch(t, &pb.Message{
|
|
AuthorId: tc.AuthorID,
|
|
ChatId: tc.ChatID,
|
|
SentAt: timestamppb.New(h.Time()),
|
|
Text: tc.Text,
|
|
}, rsp.Message)
|
|
})
|
|
}
|
|
}
|