-
Notifications
You must be signed in to change notification settings - Fork 887
test: Fix user pagination sort order #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Since the title is prefixed with test:
, there's slight duplication in it with the suffix.
LGTM
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we always just sort by UUID?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because created_at
ensures new users are always at the end meaning if you paginate by after_user
, you are guaranteed to never miss a user even if one is inserted as you paginate.
If our uuid
s were strictly increasing, then yes.
The only reason I sort by (created_at, uuid)
is to cover this small edge case of 2 users being created with the same time. Within 1 ms for postgres, and for the fake db it's a little more nuanced. Since we marshal over the API, our user's time's are not monotonic.
But tl'dr this fixes things in that edge case. In production, users will be sorted by time. Which is a good consistent ordering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh. Thank you for the explanation!
Codecov Report
@@ Coverage Diff @@
## main #1143 +/- ##
==========================================
- Coverage 66.58% 66.41% -0.18%
==========================================
Files 255 261 +6
Lines 15743 16237 +494
Branches 152 156 +4
==========================================
+ Hits 10482 10783 +301
- Misses 4194 4346 +152
- Partials 1067 1108 +41
Continue to review full report at Codecov.
|
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.
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.