Skip to content

Commit fdf74aa

Browse files
committed
Test backend
1 parent b1ab93f commit fdf74aa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

coderd/workspaces_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,33 @@ func TestOffsetLimit(t *testing.T) {
822822
require.Len(t, ws, 0)
823823
}
824824

825+
func TestWorkspaceCount(t *testing.T) {
826+
t.Parallel()
827+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
828+
defer cancel()
829+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
830+
user := coderdtest.CreateFirstUser(t, client)
831+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
832+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
833+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
834+
template2 := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
835+
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
836+
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template2.ID)
837+
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template2.ID)
838+
839+
response, err := client.WorkspaceCount(ctx, codersdk.WorkspaceCountRequest{})
840+
require.NoError(t, err, "fetch workspace count")
841+
// counts all
842+
require.Equal(t, int(response.Count), 3)
843+
844+
response2, err2 := client.WorkspaceCount(ctx, codersdk.WorkspaceCountRequest{
845+
SearchQuery: fmt.Sprintf("template:%s", template.Name),
846+
})
847+
require.NoError(t, err2, "fetch workspace count")
848+
// counts only those that pass filter
849+
require.Equal(t, int(response2.Count), 1)
850+
}
851+
825852
func TestPostWorkspaceBuild(t *testing.T) {
826853
t.Parallel()
827854
t.Run("NoTemplateVersion", func(t *testing.T) {

codersdk/workspaces.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,34 @@ func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Work
324324
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
325325
}
326326

327+
func (c *Client) WorkspaceCount(ctx context.Context, req WorkspaceCountRequest) (WorkspaceCountResponse, error) {
328+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces/count", nil, func(r *http.Request) {
329+
q := r.URL.Query()
330+
var params []string
331+
if req.SearchQuery != "" {
332+
params = append(params, req.SearchQuery)
333+
}
334+
q.Set("q", strings.Join(params, " "))
335+
r.URL.RawQuery = q.Encode()
336+
})
337+
if err != nil {
338+
return WorkspaceCountResponse{}, err
339+
}
340+
defer res.Body.Close()
341+
342+
if res.StatusCode != http.StatusOK {
343+
return WorkspaceCountResponse{}, readBodyAsError(res)
344+
}
345+
346+
var countRes WorkspaceCountResponse
347+
err = json.NewDecoder(res.Body).Decode(&countRes)
348+
if err != nil {
349+
return WorkspaceCountResponse{}, err
350+
}
351+
352+
return countRes, nil
353+
}
354+
327355
// WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name.
328356
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceOptions) (Workspace, error) {
329357
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, func(r *http.Request) {

0 commit comments

Comments
 (0)