MCPcopy Index your code
hub / github.com/coder/coder / TestGetLatestWorkspaceBuildsByWorkspaceIDs

Function TestGetLatestWorkspaceBuildsByWorkspaceIDs

coderd/database/querier_test.go:8760–8826  ·  view source on GitHub ↗

TestGetLatestWorkspaceBuildsByWorkspaceIDs populates the database with workspaces and builds. It then tests that GetLatestWorkspaceBuildsByWorkspaceIDs returns the latest build for some subset of the workspaces.

(t *testing.T)

Source from the content-addressed store, hash-verified

8758// GetLatestWorkspaceBuildsByWorkspaceIDs returns the latest build for some
8759// subset of the workspaces.
8760func TestGetLatestWorkspaceBuildsByWorkspaceIDs(t *testing.T) {
8761 t.Parallel()
8762
8763 db, _ := dbtestutil.NewDB(t)
8764
8765 org := dbgen.Organization(t, db, database.Organization{})
8766 admin := dbgen.User(t, db, database.User{})
8767
8768 tv := dbfake.TemplateVersion(t, db).
8769 Seed(database.TemplateVersion{
8770 OrganizationID: org.ID,
8771 CreatedBy: admin.ID,
8772 }).
8773 Do()
8774
8775 users := make([]database.User, 5)
8776 wrks := make([][]database.WorkspaceTable, len(users))
8777 exp := make(map[uuid.UUID]database.WorkspaceBuild)
8778 for i := range users {
8779 users[i] = dbgen.User(t, db, database.User{})
8780 dbgen.OrganizationMember(t, db, database.OrganizationMember{
8781 UserID: users[i].ID,
8782 OrganizationID: org.ID,
8783 })
8784
8785 // Each user gets 2 workspaces.
8786 wrks[i] = make([]database.WorkspaceTable, 2)
8787 for wi := range wrks[i] {
8788 wrks[i][wi] = dbgen.Workspace(t, db, database.WorkspaceTable{
8789 TemplateID: tv.Template.ID,
8790 OwnerID: users[i].ID,
8791 })
8792
8793 // Choose a deterministic number of builds per workspace
8794 // No more than 5 builds though, that would be excessive.
8795 for j := int32(1); int(j) <= (i+wi)%5; j++ {
8796 wb := dbfake.WorkspaceBuild(t, db, wrks[i][wi]).
8797 Seed(database.WorkspaceBuild{
8798 WorkspaceID: wrks[i][wi].ID,
8799 BuildNumber: j + 1,
8800 }).
8801 Do()
8802
8803 exp[wrks[i][wi].ID] = wb.Build // Save the final workspace build
8804 }
8805 }
8806 }
8807
8808 // Only take half the users. And only take 1 workspace per user for the test.
8809 // The others are just noice. This just queries a subset of workspaces and builds
8810 // to make sure the noise doesn't interfere with the results.
8811 assertWrks := wrks[:len(users)/2]
8812 ctx := testutil.Context(t, testutil.WaitLong)
8813 ids := slice.Convert[[]database.WorkspaceTable, uuid.UUID](assertWrks, func(pair []database.WorkspaceTable) uuid.UUID {
8814 return pair[0].ID
8815 })
8816
8817 require.Greater(t, len(ids), 0, "expected some workspace ids for test")

Callers

nothing calls this directly

Calls 13

NewDBFunction · 0.92
OrganizationFunction · 0.92
UserFunction · 0.92
TemplateVersionFunction · 0.92
OrganizationMemberFunction · 0.92
WorkspaceFunction · 0.92
WorkspaceBuildFunction · 0.92
ContextFunction · 0.92
ConvertFunction · 0.92
DoMethod · 0.65
SeedMethod · 0.45

Tested by

no test coverage detected