|
| 1 | +package databasefake_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/coder/coder/coderd/database" |
| 10 | + "github.com/coder/coder/coderd/database/databasefake" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGenerator(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + // Reuse the same database for all tests. |
| 17 | + db := databasefake.New() |
| 18 | + gen := databasefake.NewGenerator(t, db) |
| 19 | + |
| 20 | + t.Run("APIKey", func(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + exp, _ := gen.APIKey(context.Background(), database.APIKey{}) |
| 23 | + require.Equal(t, exp, must(db.GetAPIKeyByID(context.Background(), exp.ID))) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("File", func(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + exp := gen.File(context.Background(), database.File{}) |
| 29 | + require.Equal(t, exp, must(db.GetFileByID(context.Background(), exp.ID))) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("UserLink", func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + exp := gen.UserLink(context.Background(), database.UserLink{}) |
| 35 | + require.Equal(t, exp, must(db.GetUserLinkByLinkedID(context.Background(), exp.LinkedID))) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("WorkspaceResource", func(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + exp := gen.WorkspaceResource(context.Background(), database.WorkspaceResource{}) |
| 41 | + require.Equal(t, exp, must(db.GetWorkspaceResourceByID(context.Background(), exp.ID))) |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("Job", func(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + exp := gen.Job(context.Background(), database.ProvisionerJob{}) |
| 47 | + require.Equal(t, exp, must(db.GetProvisionerJobByID(context.Background(), exp.ID))) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("Group", func(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + exp := gen.Group(context.Background(), database.Group{}) |
| 53 | + require.Equal(t, exp, must(db.GetGroupByID(context.Background(), exp.ID))) |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("Organization", func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + exp := gen.Organization(context.Background(), database.Organization{}) |
| 59 | + require.Equal(t, exp, must(db.GetOrganizationByID(context.Background(), exp.ID))) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("Workspace", func(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + exp := gen.Workspace(context.Background(), database.Workspace{}) |
| 65 | + require.Equal(t, exp, must(db.GetWorkspaceByID(context.Background(), exp.ID))) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("Template", func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + exp := gen.Template(context.Background(), database.Template{}) |
| 71 | + require.Equal(t, exp, must(db.GetTemplateByID(context.Background(), exp.ID))) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("TemplateVersion", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + exp := gen.TemplateVersion(context.Background(), database.TemplateVersion{}) |
| 77 | + require.Equal(t, exp, must(db.GetTemplateVersionByID(context.Background(), exp.ID))) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("WorkspaceBuild", func(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + exp := gen.WorkspaceBuild(context.Background(), database.WorkspaceBuild{}) |
| 83 | + require.Equal(t, exp, must(db.GetWorkspaceBuildByID(context.Background(), exp.ID))) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("User", func(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + exp := gen.User(context.Background(), database.User{}) |
| 89 | + require.Equal(t, exp, must(db.GetUserByID(context.Background(), exp.ID))) |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func must[T any](value T, err error) T { |
| 94 | + if err != nil { |
| 95 | + panic(err) |
| 96 | + } |
| 97 | + return value |
| 98 | +} |
0 commit comments