mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
test: Fix user pagination sort order (#1143)
Sort by uuid in expected output to cover when times are equal for 2 users. The database (fake & pg) use id as as second ordering to cover this edge case. Should realistically never happen in production.
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -576,6 +577,13 @@ func TestPaginatedUsers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Sorting the users will sort by (created_at, uuid). This is to handle
|
||||
// the off case that created_at is identical for 2 users.
|
||||
// This is a really rare case in production, but does happen in unit tests
|
||||
// due to the fake database being in memory and exceptionally quick.
|
||||
sortUsers(allUsers)
|
||||
sortUsers(specialUsers)
|
||||
|
||||
assertPagination(ctx, t, client, 10, allUsers, nil)
|
||||
assertPagination(ctx, t, client, 5, allUsers, nil)
|
||||
assertPagination(ctx, t, client, 3, allUsers, nil)
|
||||
@ -658,3 +666,13 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client
|
||||
count += len(page)
|
||||
}
|
||||
}
|
||||
|
||||
// sortUsers sorts by (created_at, id)
|
||||
func sortUsers(users []codersdk.User) {
|
||||
sort.Slice(users, func(i, j int) bool {
|
||||
if users[i].CreatedAt.Equal(users[j].CreatedAt) {
|
||||
return users[i].ID.String() < users[j].ID.String()
|
||||
}
|
||||
return users[i].CreatedAt.Before(users[j].CreatedAt)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user