Skip to content

Commit 8b54ea8

Browse files
authored
test: Fix user pagination sort order (coder#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.
1 parent c08fdc0 commit 8b54ea8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

coderd/users_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"sort"
78
"testing"
89

910
"github.com/google/uuid"
@@ -576,6 +577,13 @@ func TestPaginatedUsers(t *testing.T) {
576577
}
577578
}
578579

580+
// Sorting the users will sort by (created_at, uuid). This is to handle
581+
// the off case that created_at is identical for 2 users.
582+
// This is a really rare case in production, but does happen in unit tests
583+
// due to the fake database being in memory and exceptionally quick.
584+
sortUsers(allUsers)
585+
sortUsers(specialUsers)
586+
579587
assertPagination(ctx, t, client, 10, allUsers, nil)
580588
assertPagination(ctx, t, client, 5, allUsers, nil)
581589
assertPagination(ctx, t, client, 3, allUsers, nil)
@@ -658,3 +666,13 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client
658666
count += len(page)
659667
}
660668
}
669+
670+
// sortUsers sorts by (created_at, id)
671+
func sortUsers(users []codersdk.User) {
672+
sort.Slice(users, func(i, j int) bool {
673+
if users[i].CreatedAt.Equal(users[j].CreatedAt) {
674+
return users[i].ID.String() < users[j].ID.String()
675+
}
676+
return users[i].CreatedAt.Before(users[j].CreatedAt)
677+
})
678+
}

0 commit comments

Comments
 (0)