UsersPagination creates a set of users for testing pagination. It can be used to test paginating both users and group members.
( ctx context.Context, t *testing.T, client *codersdk.Client, setup func(users []codersdk.User), fetch func(req codersdk.UsersRequest) ([]codersdk.ReducedUser, int), )
| 26 | // UsersPagination creates a set of users for testing pagination. It can be |
| 27 | // used to test paginating both users and group members. |
| 28 | func UsersPagination( |
| 29 | ctx context.Context, |
| 30 | t *testing.T, |
| 31 | client *codersdk.Client, |
| 32 | setup func(users []codersdk.User), |
| 33 | fetch func(req codersdk.UsersRequest) ([]codersdk.ReducedUser, int), |
| 34 | ) { |
| 35 | t.Helper() |
| 36 | |
| 37 | firstUser, err := client.User(ctx, codersdk.Me) |
| 38 | require.NoError(t, err, "fetch me") |
| 39 | |
| 40 | count := 10 |
| 41 | users := make([]codersdk.User, count) |
| 42 | orgID := firstUser.OrganizationIDs[0] |
| 43 | users[0] = firstUser |
| 44 | for i := range count - 1 { |
| 45 | _, user := CreateAnotherUserMutators(t, client, orgID, nil, func(r *codersdk.CreateUserRequestWithOrgs) { |
| 46 | if i < 5 { |
| 47 | r.Name = fmt.Sprintf("before%d", i) |
| 48 | } else { |
| 49 | r.Name = fmt.Sprintf("after%d", i) |
| 50 | } |
| 51 | }) |
| 52 | users[i+1] = user |
| 53 | } |
| 54 | |
| 55 | slices.SortFunc(users, func(a, b codersdk.User) int { |
| 56 | return slice.Ascending(strings.ToLower(a.Username), strings.ToLower(b.Username)) |
| 57 | }) |
| 58 | |
| 59 | if setup != nil { |
| 60 | setup(users) |
| 61 | } |
| 62 | |
| 63 | gotUsers, gotCount := fetch(codersdk.UsersRequest{}) |
| 64 | require.Len(t, gotUsers, count) |
| 65 | require.Equal(t, gotCount, count) |
| 66 | |
| 67 | gotUsers, gotCount = fetch(codersdk.UsersRequest{ |
| 68 | Pagination: codersdk.Pagination{ |
| 69 | Limit: 1, |
| 70 | }, |
| 71 | }) |
| 72 | require.Len(t, gotUsers, 1) |
| 73 | require.Equal(t, gotCount, count) |
| 74 | |
| 75 | gotUsers, gotCount = fetch(codersdk.UsersRequest{ |
| 76 | Pagination: codersdk.Pagination{ |
| 77 | Offset: 1, |
| 78 | }, |
| 79 | }) |
| 80 | require.Len(t, gotUsers, count-1) |
| 81 | require.Equal(t, gotCount, count) |
| 82 | |
| 83 | gotUsers, gotCount = fetch(codersdk.UsersRequest{ |
| 84 | Pagination: codersdk.Pagination{ |
| 85 | Limit: 1, |