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:
Steven Masley
2022-04-25 10:27:08 -05:00
committed by GitHub
parent c08fdc0c8c
commit 8b54ea8562

View File

@ -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)
})
}