-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
users_test.go
193 lines (180 loc) · 4.03 KB
/
users_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package slackdump
import (
"context"
"log/slog"
"reflect"
"testing"
"time"
"errors"
"github.com/rusq/slack"
"go.uber.org/mock/gomock"
"github.com/rusq/slackdump/v3/internal/fixtures"
"github.com/rusq/slackdump/v3/internal/network"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/types"
)
const testSuffix = "UNIT"
var testUsers = types.Users(fixtures.TestUsers)
func TestUsers_IndexByID(t *testing.T) {
users := []slack.User{
{ID: "USLACKBOT", Name: "slackbot"},
{ID: "USER2", Name: "User 2"},
}
tests := []struct {
name string
us types.Users
want structures.UserIndex
}{
{"test 1", users, structures.UserIndex{
"USLACKBOT": &users[0],
"USER2": &users[1],
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.us.IndexByID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Users.MakeUserIDIndex() = %v, want %v", got, tt.want)
}
})
}
}
func TestSession_fetchUsers(t *testing.T) {
type fields struct {
Users types.Users
config config
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
expectFn func(*mockClienter)
want types.Users
wantErr bool
}{
{
"ok",
fields{config: defConfig},
args{context.Background()},
func(mc *mockClienter) {
mc.EXPECT().GetUsersContext(gomock.Any()).Return([]slack.User(testUsers), nil)
},
testUsers,
false,
},
{
"api error",
fields{config: defConfig},
args{context.Background()},
func(mc *mockClienter) {
mc.EXPECT().GetUsersContext(gomock.Any()).Return(nil, errors.New("i don't think so"))
},
nil,
true,
},
{
"zero users",
fields{config: defConfig},
args{context.Background()},
func(mc *mockClienter) {
mc.EXPECT().GetUsersContext(gomock.Any()).Return([]slack.User{}, nil)
},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := NewmockClienter(gomock.NewController(t))
tt.expectFn(mc)
sd := &Session{
client: mc,
cfg: tt.fields.config,
}
got, err := sd.fetchUsers(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("Session.fetchUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Session.fetchUsers() = %v, want %v", got, tt.want)
}
})
}
}
func TestSession_GetUsers(t *testing.T) {
type fields struct {
config config
usercache usercache
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
expectFn func(*mockClienter)
want types.Users
wantErr bool
}{
{
"everything goes as planned",
fields{
config: config{limits: network.Limits{
Tier2: network.TierLimit{Burst: 1},
Tier3: network.TierLimit{Burst: 1},
}},
usercache: usercache{},
},
args{context.Background()},
func(mc *mockClienter) {
mc.EXPECT().GetUsersContext(gomock.Any()).Return([]slack.User(testUsers), nil)
},
testUsers,
false,
},
{
"loaded from cache",
fields{
config: config{limits: network.Limits{
Tier2: network.TierLimit{Burst: 1},
Tier3: network.TierLimit{Burst: 1},
}},
usercache: usercache{
users: testUsers,
cachedAt: time.Now(),
},
},
args{context.Background()},
func(mc *mockClienter) {
// we don't expect any API calls
},
testUsers,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := NewmockClienter(gomock.NewController(t))
tt.expectFn(mc)
sd := &Session{
client: mc,
wspInfo: &slack.AuthTestResponse{TeamID: testSuffix},
cfg: tt.fields.config,
uc: &tt.fields.usercache,
log: slog.Default(),
}
got, err := sd.GetUsers(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("Session.GetUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Session.GetUsers() = %v, want %v", got, tt.want)
}
})
}
}