From dcac12c536fd8c14b2438c7e3760ab3035332563 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 25 Apr 2022 09:36:21 -0500 Subject: [PATCH 1/2] test: Fix user pagination unit test 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. --- coderd/users_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/coderd/users_test.go b/coderd/users_test.go index 0e08462f7537b..b664fb93213b4 100644 --- a/coderd/users_test.go +++ b/coderd/users_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "sort" "testing" "github.com/google/uuid" @@ -597,6 +598,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) @@ -679,3 +687,15 @@ 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) { + // Technically the postgres database also orders by uuid. So match + // that behavior + return users[i].ID.String() < users[j].ID.String() + } + return users[i].CreatedAt.Before(users[j].CreatedAt) + }) +} From d817adb5f4d023037f97c17327c71cd829e4f883 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 25 Apr 2022 09:38:55 -0500 Subject: [PATCH 2/2] fixup! test: Fix user pagination unit test --- coderd/users_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/coderd/users_test.go b/coderd/users_test.go index b664fb93213b4..25315ec1ec64b 100644 --- a/coderd/users_test.go +++ b/coderd/users_test.go @@ -692,8 +692,6 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client func sortUsers(users []codersdk.User) { sort.Slice(users, func(i, j int) bool { if users[i].CreatedAt.Equal(users[j].CreatedAt) { - // Technically the postgres database also orders by uuid. So match - // that behavior return users[i].ID.String() < users[j].ID.String() } return users[i].CreatedAt.Before(users[j].CreatedAt)