mirror of
https://github.com/coder/coder.git
synced 2025-07-23 21:32:07 +00:00
test: Check created_at for prepareData to ensure user order (#6436)
* test: Check created_at for prepareData to ensure user order * test: Consistent user ordering in dbfake * import order * Linting
This commit is contained in:
@ -2922,6 +2922,21 @@ func (q *fakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
|
||||
return database.User{}, err
|
||||
}
|
||||
|
||||
// There is a common bug when using dbfake that 2 inserted users have the
|
||||
// same created_at time. This causes user order to not be deterministic,
|
||||
// which breaks some unit tests.
|
||||
// To fix this, we make sure that the created_at time is always greater
|
||||
// than the last user's created_at time.
|
||||
allUsers, _ := q.GetUsers(context.Background(), database.GetUsersParams{})
|
||||
if len(allUsers) > 0 {
|
||||
lastUser := allUsers[len(allUsers)-1]
|
||||
if arg.CreatedAt.Before(lastUser.CreatedAt) ||
|
||||
arg.CreatedAt.Equal(lastUser.CreatedAt) {
|
||||
// 1 ms is a good enough buffer.
|
||||
arg.CreatedAt = lastUser.CreatedAt.Add(time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
q.mutex.Lock()
|
||||
defer q.mutex.Unlock()
|
||||
|
||||
|
Reference in New Issue
Block a user