Skip to content

Commit 61da6b9

Browse files
committed
fix sort ordering, dbfake still TODO
1 parent cea20f0 commit 61da6b9

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

coderd/database/modelqueries.go

+1
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

+22-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

+3-1
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_of IS NOT NULL AND
265+
CASE WHEN workspaces.favorite_of = @order_by_favorite THEN
266+
workspaces.favorite_of = @order_by_favorite
267+
END ASC,
266268
(latest_build.completed_at IS NOT NULL AND
267269
latest_build.canceled_at IS NULL AND
268270
latest_build.error IS NULL AND

coderd/workspaces.go

+4
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
if err != nil {
164168
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{

coderd/workspaces_test.go

+40-17
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,14 @@ func TestAdminViewAllWorkspaces(t *testing.T) {
479479
func TestWorkspacesSortOrder(t *testing.T) {
480480
t.Parallel()
481481

482+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
483+
defer cancel()
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-
defer cancel()
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 := range workspaces {
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-
var actualNames []string
528-
var actualStatus []codersdk.WorkspaceStatus
552+
actualNames = make([]string, 0, len(expectedNames))
529553
for _, w := range workspaces {
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 by workspace 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 by usernames
561+
// 4. Sort by workspace names
562+
require.Equal(t, expectedNames, actualNames)
540563
}
541564

542565
func TestPostWorkspacesByOrganization(t *testing.T) {

0 commit comments

Comments
 (0)