Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4469c84

Browse files
committed
fix sort ordering, dbfake still TODO
1 parentf0cf7f6 commit4469c84

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

‎coderd/database/modelqueries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
225225
arg.Dormant,
226226
arg.LastUsedBefore,
227227
arg.LastUsedAfter,
228+
arg.OrderByFavorite,
228229
arg.Offset,
229230
arg.Limit,
230231
)

‎coderd/database/queries.sql.go

Lines changed: 22 additions & 18 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/workspaces.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ WHERE
262262
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
263263
-- @authorize_filter
264264
ORDER BY
265-
favorite_ofIS NOT NULLAND
265+
CASE WHENworkspaces.favorite_of= @order_by_favorite THEN
266+
workspaces.favorite_of= @order_by_favorite
267+
ENDASC,
266268
(latest_build.completed_atIS NOT NULLAND
267269
latest_build.canceled_at ISNULLAND
268270
latest_build.error ISNULLAND

‎coderd/workspaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
159159
return
160160
}
161161

162+
// To show the user's favorite workspaces first, we pass their userID and compare it to
163+
// column favorite_of when ordering the rows.
164+
filter.OrderByFavorite= uuid.NullUUID{Valid:true,UUID:apiKey.UserID}
165+
162166
workspaceRows,err:=api.Database.GetAuthorizedWorkspaces(ctx,filter,prepared)
163167
iferr!=nil {
164168
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{

‎coderd/workspaces_test.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,14 @@ func TestAdminViewAllWorkspaces(t *testing.T) {
479479
funcTestWorkspacesSortOrder(t*testing.T) {
480480
t.Parallel()
481481

482+
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
483+
defercancel()
484+
482485
client,db:=coderdtest.NewWithDatabase(t,nil)
483486
firstUser:=coderdtest.CreateFirstUser(t,client,func(r*codersdk.CreateFirstUserRequest) {
484487
r.Username="aaa"
485488
})
486-
_,secondUser:=coderdtest.CreateAnotherUserMutators(t,client,firstUser.OrganizationID, []string{"owner"},func(r*codersdk.CreateUserRequest) {
489+
secondUserClient,secondUser:=coderdtest.CreateAnotherUserMutators(t,client,firstUser.OrganizationID, []string{"owner"},func(r*codersdk.CreateUserRequest) {
487490
r.Username="zzz"
488491
})
489492

@@ -502,41 +505,61 @@ func TestWorkspacesSortOrder(t *testing.T) {
502505
// e-workspace should also be stopped
503506
wsbE:=dbfake.WorkspaceBuild(t,db, database.Workspace{Name:"e-workspace",OwnerID:secondUser.ID,OrganizationID:firstUser.OrganizationID}).Seed(database.WorkspaceBuild{Transition:database.WorkspaceTransitionStop}).Do()
504507

505-
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
506-
defercancel()
508+
// f-workspace is also stopped, but is marked as favorite
509+
wsbF:=dbfake.WorkspaceBuild(t,db, database.Workspace{Name:"f-workspace",OwnerID:firstUser.UserID,OrganizationID:firstUser.OrganizationID}).Seed(database.WorkspaceBuild{Transition:database.WorkspaceTransitionStop}).Do()
510+
require.NoError(t,client.FavoriteWorkspace(ctx,wsbF.Workspace.ID))// need to do this via API call for now
511+
507512
workspacesResponse,err:=client.Workspaces(ctx, codersdk.WorkspaceFilter{})
508513
require.NoError(t,err,"(first) fetch workspaces")
509514
workspaces:=workspacesResponse.Workspaces
510515

511516
expectedNames:= []string{
517+
wsbF.Workspace.Name,// favorite
512518
wsbA.Workspace.Name,// running
513519
wsbC.Workspace.Name,// running
514520
wsbB.Workspace.Name,// stopped, aaa < zzz
515521
wsbD.Workspace.Name,// stopped, zzz > aaa
516522
wsbE.Workspace.Name,// stopped, zzz > aaa
517523
}
518524

519-
expectedStatus:= []codersdk.WorkspaceStatus{
520-
codersdk.WorkspaceStatusRunning,
521-
codersdk.WorkspaceStatusRunning,
522-
codersdk.WorkspaceStatusStopped,
523-
codersdk.WorkspaceStatusStopped,
524-
codersdk.WorkspaceStatusStopped,
525+
actualNames:=make([]string,0,len(expectedNames))
526+
for_,w:=rangeworkspaces {
527+
actualNames=append(actualNames,w.Name)
528+
}
529+
530+
// the correct sorting order is:
531+
// 1. Favorite workspaces (we have one, workspace-f)
532+
// 2. Running workspaces
533+
// 3. Sort by usernames
534+
// 4. Sort by workspace names
535+
require.Equal(t,expectedNames,actualNames)
536+
537+
// Once again but this time as a different user. This time we do not expect to see another
538+
// user's favorites first.
539+
workspacesResponse,err=secondUserClient.Workspaces(ctx, codersdk.WorkspaceFilter{})
540+
require.NoError(t,err,"(second) fetch workspaces")
541+
workspaces=workspacesResponse.Workspaces
542+
543+
expectedNames= []string{
544+
wsbA.Workspace.Name,// running
545+
wsbC.Workspace.Name,// running
546+
wsbB.Workspace.Name,// stopped, aaa < zzz
547+
wsbF.Workspace.Name,// stopped, aaa < zzz
548+
wsbD.Workspace.Name,// stopped, zzz > aaa
549+
wsbE.Workspace.Name,// stopped, zzz > aaa
525550
}
526551

527-
varactualNames []string
528-
varactualStatus []codersdk.WorkspaceStatus
552+
actualNames=make([]string,0,len(expectedNames))
529553
for_,w:=rangeworkspaces {
530554
actualNames=append(actualNames,w.Name)
531-
actualStatus=append(actualStatus,w.LatestBuild.Status)
532555
}
533556

534557
// the correct sorting order is:
535-
// 1.Running workspaces
536-
// 2.Sort by usernames
537-
// 3. Sort byworkspace names
538-
assert.Equal(t,expectedNames,actualNames)
539-
assert.Equal(t,expectedStatus,actualStatus)
558+
// 1.Favorite workspaces (we have none this time)
559+
// 2.Running workspaces
560+
// 3. Sort byusernames
561+
// 4. Sort by workspace names
562+
require.Equal(t,expectedNames,actualNames)
540563
}
541564

542565
funcTestPostWorkspacesByOrganization(t*testing.T) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp