|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "net/http"
|
| 7 | + "sort" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "github.com/google/uuid"
|
@@ -576,6 +577,13 @@ func TestPaginatedUsers(t *testing.T) {
|
576 | 577 | }
|
577 | 578 | }
|
578 | 579 |
|
| 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 | + |
579 | 587 | assertPagination(ctx, t, client, 10, allUsers, nil)
|
580 | 588 | assertPagination(ctx, t, client, 5, allUsers, nil)
|
581 | 589 | assertPagination(ctx, t, client, 3, allUsers, nil)
|
@@ -658,3 +666,13 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client
|
658 | 666 | count += len(page)
|
659 | 667 | }
|
660 | 668 | }
|
| 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