Skip to content

Commit

Permalink
分割送信処理修正 & テスト修正
Browse files Browse the repository at this point in the history
  • Loading branch information
hattorihotaka committed Feb 21, 2023
1 parent 2d77497 commit f2788dd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 19 deletions.
22 changes: 15 additions & 7 deletions app/internal/domain/service/slack_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ const (
// postEmailList method posts emailList using slack postMessageAPI.
// The chunkedLines are generated and requested for each chunk,
// because of considering the limit the number of characters of slackAPI.
func (s *slackResponseService) postEmailList(ctx context.Context, channelID, message, ts string, lineSizeOfPostEmailList int) error {
lines := strings.Split(message, "\n")
func (s *slackResponseService) postEmailList(ctx context.Context, channelID string, emails []*model.SlackUserEmail, ts string) error {
lines := append(make([]string, 0, len(emails)+1), "参加者一覧")
for _, email := range emails {
lines = append(lines, email.Email)
}
chunkedLines := slice.SplitStringSliceInChunks(lines, lineSizeOfPostEmailList)
for _, chunkedLine := range chunkedLines {
err := s.slackRepository.PostMessage(ctx, channelID, strings.Join(chunkedLine, "\n"), ts)
Expand All @@ -70,12 +73,17 @@ func (s *slackResponseService) postEmailList(ctx context.Context, channelID, mes
}

func (s *slackResponseService) ReplyEmailList(ctx context.Context, event *slackevents.AppMentionEvent, emails []*model.SlackUserEmail) error {
msg := "参加者一覧\n"
for _, email := range emails {
msg += email.Email
msg += "\n"
if len(emails) <= lineSizeOfPostEmailList-1 {
var b strings.Builder
b.Grow(len(emails) + 1)
b.WriteString("参加者一覧\n")
for _, email := range emails {
b.WriteString(email.Email)
}
return s.slackRepository.PostMessage(ctx, event.Channel, b.String(), event.ThreadTimeStamp)
}
return s.postEmailList(ctx, event.Channel, msg, event.ThreadTimeStamp, lineSizeOfPostEmailList)

return s.postEmailList(ctx, event.Channel, emails, event.ThreadTimeStamp)
}

func (s *slackResponseService) ReplyError(ctx context.Context, event *slackevents.AppMentionEvent, err error) error {
Expand Down
74 changes: 62 additions & 12 deletions app/internal/domain/service/slack_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package service
import (
"context"
"errors"
"fmt"
"strings"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -28,12 +30,37 @@ import (
"github.com/slack-go/slack/slackevents"
)

func createEmails(suffixBase, n int) []*model.SlackUserEmail {
emails := make([]*model.SlackUserEmail, n)
for index := range emails {
emails[index] = &model.SlackUserEmail{
Email: fmt.Sprintf("user_%[email protected]", suffixBase+index),
}
}
return emails
}

func createMessage(base string, emails []*model.SlackUserEmail) string {
emailStrings := convertEmailsToStrings(emails)
if base != "" {
emailStrings = append([]string{base}, emailStrings...)
}
return strings.Join(emailStrings, "\n")
}

func convertEmailsToStrings(emails []*model.SlackUserEmail) []string {
es := make([]string, len(emails))
for index, email := range emails {
es[index] = email.Email
}
return es
}

func Test_slackResponseService_postEmailList(t *testing.T) {
channelID := "cid"
ts := "ts"
type args struct {
channelID string
message string
ts string
lineSizeOfPostEmailList int
emails []*model.SlackUserEmail
}
tests := []struct {
name string
Expand All @@ -42,20 +69,43 @@ func Test_slackResponseService_postEmailList(t *testing.T) {
wantErr bool
}{
{
name: "OK",
name: "OK: number of emails = 1",
args: args{
emails: createEmails(0, 1),
},
prepare: func(msr *mock_repository.MockSlackRepository) {
gomock.InOrder(
msr.EXPECT().PostMessage(
gomock.Any(), channelID, createMessage("参加者一覧", createEmails(0, 1)), ts).
Return(nil),
)
},
},
{
name: "OK: number of emails = lineSizeOfPostEmailList - 1",
args: args{
emails: createEmails(0, lineSizeOfPostEmailList-1),
},
prepare: func(msr *mock_repository.MockSlackRepository) {
gomock.InOrder(
msr.EXPECT().PostMessage(
gomock.Any(), channelID, createMessage("参加者一覧", createEmails(0, lineSizeOfPostEmailList-1)), ts).
Return(nil),
)
},
},
{
name: "OK: number of emails = lineSizeOfPostEmailList",
args: args{
channelID: "cid",
message: "参加者一覧\n[email protected]\n[email protected]\n[email protected]",
ts: "ts",
lineSizeOfPostEmailList: 2,
emails: createEmails(0, lineSizeOfPostEmailList),
},
prepare: func(msr *mock_repository.MockSlackRepository) {
gomock.InOrder(
msr.EXPECT().PostMessage(
gomock.Any(), "cid", "参加者一覧\n[email protected]", "ts").
gomock.Any(), channelID, createMessage("参加者一覧", createEmails(0, lineSizeOfPostEmailList-1)), ts).
Return(nil),
msr.EXPECT().PostMessage(
gomock.Any(), "cid", "[email protected]\n[email protected]", "ts").
gomock.Any(), channelID, createMessage("", createEmails(lineSizeOfPostEmailList-1, 1)), ts).
Return(nil),
)
},
Expand All @@ -75,7 +125,7 @@ func Test_slackResponseService_postEmailList(t *testing.T) {
slackRepository: msr,
errorRepository: mer,
}
if err := s.postEmailList(ctx, tt.args.channelID, tt.args.message, tt.args.ts, tt.args.lineSizeOfPostEmailList); (err != nil) != tt.wantErr {
if err := s.postEmailList(ctx, channelID, tt.args.emails, ts); (err != nil) != tt.wantErr {
t.Errorf("postEmailList() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down

0 comments on commit f2788dd

Please sign in to comment.