-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_channel_test.go
151 lines (128 loc) · 3.96 KB
/
slack_channel_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package slackactivity_test
import (
"errors"
"fmt"
"strconv"
"testing"
slackactivity "github.com/nakatanakatana/slack-activity"
"github.com/slack-go/slack"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
type conversationsMock struct {
channels []slack.Channel
}
var _ slackactivity.SlackGetChannelsClient = conversationsMock{}
var (
ErrMock = errors.New("mock error")
ErrGetConversationInfoNotFound = errors.New("GetConversationInfo:not found")
)
func (m conversationsMock) GetConversations(
params *slack.GetConversationsParameters,
) ([]slack.Channel, string, error) {
var cur int
cur, _ = strconv.Atoi(params.Cursor)
if params.Cursor == "" {
cur = 0
}
if cur > len(m.channels) {
return []slack.Channel{}, "", fmt.Errorf("cur over max: %w", ErrMock)
}
restLength := len(m.channels[cur:])
switch {
case restLength >= params.Limit+1:
return m.channels[cur : cur+params.Limit], strconv.Itoa(cur + params.Limit), nil
case restLength > 0:
return m.channels[cur:], "", nil
default:
return []slack.Channel{}, "", fmt.Errorf("unknown: %w", ErrMock)
}
}
func (m conversationsMock) GetConversationInfo(channelID string, includeLocale bool) (*slack.Channel, error) {
if len(m.channels) == 0 {
return nil, ErrGetConversationInfoNotFound
}
return &m.channels[0], nil
}
func newConversationsMock(length int) conversationsMock {
c := make([]slack.Channel, length)
for i := range c {
c[i] = slack.Channel{
GroupConversation: slack.GroupConversation{
Name: strconv.Itoa(i),
},
IsChannel: true,
IsGeneral: false,
IsMember: true,
Locale: "",
}
}
return conversationsMock{
channels: c,
}
}
func TestConversationsMock(t *testing.T) {
t.Parallel()
type testCase struct {
name string
cur string
limit int
expectLen int
expectStart string
expectEnd string
expectNext string
expectErr string
}
for _, tt := range []testCase{
{"limitの件数結果を返す", "", 5, 5, "0", "4", "5", ""},
{"limitが全件を超える場合は全件を返す", "", 20, 10, "0", "9", "", ""},
{"curを指定した場合はそこからの結果を返す", "1", 10, 9, "1", "9", "", ""},
{"curが範囲外を指定した場合はエラー返す", "11", 0, 0, "", "", "", "cur over max"},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mock := newConversationsMock(10)
result, next, err := mock.GetConversations(&slack.GetConversationsParameters{
Cursor: tt.cur,
Limit: tt.limit,
})
if tt.expectErr == "" {
assert.NilError(t, err)
assert.Assert(t, is.Len(result, tt.expectLen))
assert.Equal(t, result[0].GroupConversation.Name, tt.expectStart)
assert.Equal(t, result[len(result)-1].GroupConversation.Name, tt.expectEnd)
assert.Equal(t, next, tt.expectNext)
} else {
assert.ErrorContains(t, err, tt.expectErr)
}
})
}
}
func TestGetAllUnarchivedChannels(t *testing.T) {
t.Parallel()
t.Run("1000件以内のときにページングをせずに全件のリストを返す", func(t *testing.T) {
t.Parallel()
const channelsLen = 200
mock := newConversationsMock(channelsLen)
result, err := slackactivity.GetAllUnarchivedChannels(mock)
assert.NilError(t, err)
assert.Assert(t, is.Len(result, channelsLen))
})
t.Run("チャネルが5500件のときにページングをして全件のリストを返す", func(t *testing.T) {
t.Parallel()
const channelsLen = 5500
mock := newConversationsMock(channelsLen)
result, err := slackactivity.GetAllUnarchivedChannels(mock)
assert.NilError(t, err)
assert.Assert(t, is.Len(result, channelsLen))
})
t.Run("チャネルが1000*30件以上のときにページングをして30000件のリストを返す(途中で中断する)", func(t *testing.T) {
t.Parallel()
const channelsLen = 35000
mock := newConversationsMock(channelsLen)
result, err := slackactivity.GetAllUnarchivedChannels(mock)
assert.NilError(t, err)
assert.Assert(t, is.Len(result, 1000*30))
})
}