Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Add more objects to dbgen #6013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions coderd/database/dbgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func User(t *testing.T, db database.Store, orig database.User) database.User {
return user
}

func GitSSHKey(t *testing.T, db database.Store, orig database.GitSSHKey) database.GitSSHKey {
key, err := db.InsertGitSSHKey(context.Background(), database.InsertGitSSHKeyParams{
UserID: takeFirst(orig.UserID, uuid.New()),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
UpdatedAt: takeFirst(orig.UpdatedAt, time.Now()),
PrivateKey: takeFirst(orig.PrivateKey, ""),
PublicKey: takeFirst(orig.PublicKey, ""),
})
require.NoError(t, err, "insert ssh key")
return key
}

func Organization(t *testing.T, db database.Store, orig database.Organization) database.Organization {
org, err := db.InsertOrganization(context.Background(), database.InsertOrganizationParams{
ID: takeFirst(orig.ID, uuid.New()),
Expand Down Expand Up @@ -254,6 +266,34 @@ func ProvisionerJob(t *testing.T, db database.Store, orig database.ProvisionerJo
return job
}

func WorkspaceApp(t *testing.T, db database.Store, orig database.WorkspaceApp) database.WorkspaceApp {
resource, err := db.InsertWorkspaceApp(context.Background(), database.InsertWorkspaceAppParams{
ID: takeFirst(orig.ID, uuid.New()),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
AgentID: takeFirst(orig.AgentID, uuid.New()),
Slug: takeFirst(orig.Slug, namesgenerator.GetRandomName(1)),
DisplayName: takeFirst(orig.DisplayName, namesgenerator.GetRandomName(1)),
Icon: takeFirst(orig.Icon, namesgenerator.GetRandomName(1)),
Command: sql.NullString{
String: takeFirst(orig.Command.String, "ls"),
Valid: orig.Command.Valid,
},
Url: sql.NullString{
String: takeFirst(orig.Url.String),
Valid: orig.Url.Valid,
},
External: orig.External,
Subdomain: orig.Subdomain,
SharingLevel: takeFirst(orig.SharingLevel, database.AppSharingLevelOwner),
HealthcheckUrl: takeFirst(orig.HealthcheckUrl, "https://localhost:8000"),
HealthcheckInterval: takeFirst(orig.HealthcheckInterval, 60),
HealthcheckThreshold: takeFirst(orig.HealthcheckThreshold, 60),
Health: takeFirst(orig.Health, database.WorkspaceAppHealthHealthy),
})
require.NoError(t, err, "insert app")
return resource
}

func WorkspaceResource(t *testing.T, db database.Store, orig database.WorkspaceResource) database.WorkspaceResource {
resource, err := db.InsertWorkspaceResource(context.Background(), database.InsertWorkspaceResourceParams{
ID: takeFirst(orig.ID, uuid.New()),
Expand Down Expand Up @@ -312,6 +352,21 @@ func UserLink(t *testing.T, db database.Store, orig database.UserLink) database.
return link
}

func GitAuthLink(t *testing.T, db database.Store, orig database.GitAuthLink) database.GitAuthLink {
link, err := db.InsertGitAuthLink(context.Background(), database.InsertGitAuthLinkParams{
ProviderID: takeFirst(orig.ProviderID, uuid.New().String()),
UserID: takeFirst(orig.UserID, uuid.New()),
OAuthAccessToken: takeFirst(orig.OAuthAccessToken, uuid.NewString()),
OAuthRefreshToken: takeFirst(orig.OAuthAccessToken, uuid.NewString()),
OAuthExpiry: takeFirst(orig.OAuthExpiry, time.Now().Add(time.Hour*24)),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
UpdatedAt: takeFirst(orig.UpdatedAt, time.Now()),
})

require.NoError(t, err, "insert git auth link")
return link
}

func TemplateVersion(t *testing.T, db database.Store, orig database.TemplateVersion) database.TemplateVersion {
version, err := db.InsertTemplateVersion(context.Background(), database.InsertTemplateVersionParams{
ID: takeFirst(orig.ID, uuid.New()),
Expand Down
24 changes: 24 additions & 0 deletions coderd/database/dbgen/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,30 @@ func TestGenerator(t *testing.T) {
require.Equal(t, exp, must(db.GetUserLinkByLinkedID(context.Background(), exp.LinkedID)))
})

t.Run("GitAuthLink", func(t *testing.T) {
t.Parallel()
db := dbfake.New()
exp := dbgen.GitAuthLink(t, db, database.GitAuthLink{})
require.Equal(t, exp, must(db.GetGitAuthLink(context.Background(), database.GetGitAuthLinkParams{
ProviderID: exp.ProviderID,
UserID: exp.UserID,
})))
})

t.Run("WorkspaceResource", func(t *testing.T) {
t.Parallel()
db := dbfake.New()
exp := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{})
require.Equal(t, exp, must(db.GetWorkspaceResourceByID(context.Background(), exp.ID)))
})

t.Run("WorkspaceApp", func(t *testing.T) {
t.Parallel()
db := dbfake.New()
exp := dbgen.WorkspaceApp(t, db, database.WorkspaceApp{})
require.Equal(t, exp, must(db.GetWorkspaceAppsByAgentID(context.Background(), exp.AgentID))[0])
})

t.Run("WorkspaceResourceMetadatum", func(t *testing.T) {
t.Parallel()
db := dbfake.New()
Expand Down Expand Up @@ -159,6 +176,13 @@ func TestGenerator(t *testing.T) {
exp := dbgen.User(t, db, database.User{})
require.Equal(t, exp, must(db.GetUserByID(context.Background(), exp.ID)))
})

t.Run("SSHKey", func(t *testing.T) {
t.Parallel()
db := dbfake.New()
exp := dbgen.GitSSHKey(t, db, database.GitSSHKey{})
require.Equal(t, exp, must(db.GetGitSSHKey(context.Background(), exp.UserID)))
})
}

func must[T any](value T, err error) T {
Expand Down