Skip to content

Commit d76d9ca

Browse files
committed
chore: Convert into table based test with subtests
1 parent ae17114 commit d76d9ca

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

coderd/users_test.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,8 @@ func TestPaginatedUsers(t *testing.T) {
10951095
client := coderdtest.New(t, &coderdtest.Options{APIRateLimit: -1})
10961096
coderdtest.CreateFirstUser(t, client)
10971097

1098-
// This test can take longer than a long time.
1099-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*4)
1098+
// This test takes longer than a long time.
1099+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*2)
11001100
defer cancel()
11011101

11021102
me, err := client.User(ctx, codersdk.Me)
@@ -1153,25 +1153,41 @@ func TestPaginatedUsers(t *testing.T) {
11531153
sortUsers(allUsers)
11541154
sortUsers(specialUsers)
11551155

1156-
assertPagination(ctx, t, client, 10, allUsers, nil)
1157-
assertPagination(ctx, t, client, 5, allUsers, nil)
1158-
assertPagination(ctx, t, client, 3, allUsers, nil)
1159-
assertPagination(ctx, t, client, 1, allUsers, nil)
1160-
1161-
// Try a search
11621156
gmailSearch := func(request codersdk.UsersRequest) codersdk.UsersRequest {
11631157
request.Search = "gmail"
11641158
return request
11651159
}
1166-
assertPagination(ctx, t, client, 3, specialUsers, gmailSearch)
1167-
assertPagination(ctx, t, client, 7, specialUsers, gmailSearch)
1168-
11691160
usernameSearch := func(request codersdk.UsersRequest) codersdk.UsersRequest {
11701161
request.Search = "specialuser"
11711162
return request
11721163
}
1173-
assertPagination(ctx, t, client, 3, specialUsers, usernameSearch)
1174-
assertPagination(ctx, t, client, 1, specialUsers, usernameSearch)
1164+
1165+
tests := []struct {
1166+
name string
1167+
limit int
1168+
allUsers []codersdk.User
1169+
opt func(request codersdk.UsersRequest) codersdk.UsersRequest
1170+
}{
1171+
{name: "all users", limit: 10, allUsers: allUsers},
1172+
{name: "all users", limit: 5, allUsers: allUsers},
1173+
{name: "all users", limit: 3, allUsers: allUsers},
1174+
{name: "all users", limit: 1, allUsers: allUsers},
1175+
{name: "gmail search", limit: 3, allUsers: specialUsers, opt: gmailSearch},
1176+
{name: "gmail search", limit: 7, allUsers: specialUsers, opt: gmailSearch},
1177+
{name: "username search", limit: 3, allUsers: specialUsers, opt: usernameSearch},
1178+
{name: "username search", limit: 3, allUsers: specialUsers, opt: usernameSearch},
1179+
}
1180+
for _, tt := range tests {
1181+
tt := tt
1182+
t.Run(fmt.Sprintf("%s %d", tt.name, tt.limit), func(t *testing.T) {
1183+
t.Parallel()
1184+
1185+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
1186+
defer cancel()
1187+
1188+
assertPagination(ctx, t, client, tt.limit, tt.allUsers, tt.opt)
1189+
})
1190+
}
11751191
}
11761192

11771193
// Assert pagination will page through the list of all users using the given
@@ -1180,10 +1196,6 @@ func TestPaginatedUsers(t *testing.T) {
11801196
func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client, limit int, allUsers []codersdk.User,
11811197
opt func(request codersdk.UsersRequest) codersdk.UsersRequest,
11821198
) {
1183-
// Ensure any single assertion doesn't take too long.s
1184-
ctx, cancel := context.WithTimeout(ctx, testutil.WaitLong)
1185-
defer cancel()
1186-
11871199
var count int
11881200
if opt == nil {
11891201
opt = func(request codersdk.UsersRequest) codersdk.UsersRequest {

0 commit comments

Comments
 (0)