diff --git a/database/deployment_test.go b/database/deployment_test.go new file mode 100644 index 00000000..60f9d375 --- /dev/null +++ b/database/deployment_test.go @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: Apache-2.0 + +package database + +import ( + "database/sql" + "reflect" + "testing" + + "github.com/go-vela/types/library" + "github.com/go-vela/types/raw" + "github.com/google/go-cmp/cmp" + "github.com/lib/pq" +) + +func TestDatabase_Deployment_Nullify(t *testing.T) { + // setup types + var d *Deployment + + want := &Deployment{ + ID: sql.NullInt64{Int64: 0, Valid: false}, + Number: sql.NullInt64{Int64: 0, Valid: false}, + RepoID: sql.NullInt64{Int64: 0, Valid: false}, + URL: sql.NullString{String: "", Valid: false}, + User: sql.NullString{String: "", Valid: false}, + Commit: sql.NullString{String: "", Valid: false}, + Ref: sql.NullString{String: "", Valid: false}, + Task: sql.NullString{String: "", Valid: false}, + Target: sql.NullString{String: "", Valid: false}, + Description: sql.NullString{String: "", Valid: false}, + Payload: nil, + Builds: nil, + } + + // setup tests + tests := []struct { + deployment *Deployment + want *Deployment + }{ + { + deployment: testDeployment(), + want: testDeployment(), + }, + { + deployment: d, + want: nil, + }, + { + deployment: new(Deployment), + want: want, + }, + } + + // run tests + for _, test := range tests { + got := test.deployment.Nullify() + + if !reflect.DeepEqual(got, test.want) { + t.Errorf("Nullify is %v, want %v", got, test.want) + } + } +} + +func TestDatabase_Deployment_ToLibrary(t *testing.T) { + want := new(library.Deployment) + want.SetID(1) + want.SetNumber(1) + want.SetRepoID(1) + want.SetURL("https://github.com/github/octocat/deployments/1") + want.SetUser("octocat") + want.SetCommit("1234") + want.SetRef("refs/heads/main") + want.SetTask("deploy:vela") + want.SetTarget("production") + want.SetDescription("Deployment request from Vela") + want.SetPayload(raw.StringSliceMap{"foo": "test1"}) + want.SetBuilds(nil) + + got := testDeployment().ToLibrary(nil) + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("(ToLibrary: -want +got):\n%s", diff) + } +} + +func TestDatabase_Deployment_Validate(t *testing.T) { + // setup types + tests := []struct { + failure bool + deployment *Deployment + }{ + { + failure: false, + deployment: testDeployment(), + }, + { // no number set for secret + failure: true, + deployment: &Deployment{ + ID: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + }, + }, + { // no repoID set for secret + failure: true, + deployment: &Deployment{ + ID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt64{Int64: 1, Valid: true}, + }, + }, + } + + // run tests + for _, test := range tests { + err := test.deployment.Validate() + + if test.failure { + if err == nil { + t.Errorf("Validate should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("Validate returned err: %v", err) + } + } +} + +func TestDatabase_DeploymentFromLibrary(t *testing.T) { + d := new(library.Deployment) + d.SetID(1) + d.SetNumber(1) + d.SetRepoID(1) + d.SetURL("https://github.com/github/octocat/deployments/1") + d.SetUser("octocat") + d.SetCommit("1234") + d.SetRef("refs/heads/main") + d.SetTask("deploy:vela") + d.SetTarget("production") + d.SetDescription("Deployment request from Vela") + d.SetPayload(raw.StringSliceMap{"foo": "test1"}) + d.SetBuilds(nil) + + want := testDeployment() + + // run test + got := DeploymentFromLibrary(d) + + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("(-want +got):\n%s", diff) + } +} + +// testDeployment is a test helper function to create a Deployment type with all fields set to a fake value. +func testDeployment() *Deployment { + return &Deployment{ + ID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + URL: sql.NullString{String: "https://github.com/github/octocat/deployments/1", Valid: true}, + User: sql.NullString{String: "octocat", Valid: true}, + Commit: sql.NullString{String: "1234", Valid: true}, + Ref: sql.NullString{String: "refs/heads/main", Valid: true}, + Task: sql.NullString{String: "deploy:vela", Valid: true}, + Target: sql.NullString{String: "production", Valid: true}, + Description: sql.NullString{String: "Deployment request from Vela", Valid: true}, + Payload: raw.StringSliceMap{"foo": "test1"}, + Builds: pq.StringArray{}, + } +} diff --git a/database/hook_test.go b/database/hook_test.go index 222c7c6b..a137022c 100644 --- a/database/hook_test.go +++ b/database/hook_test.go @@ -91,7 +91,7 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) { Host: sql.NullString{String: "github.com", Valid: true}, Event: sql.NullString{String: "push", Valid: true}, EventAction: sql.NullString{String: "", Valid: true}, - Branch: sql.NullString{String: "master", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, Error: sql.NullString{String: "", Valid: true}, Status: sql.NullString{String: "success", Valid: true}, Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, @@ -185,7 +185,7 @@ func TestDatabase_HookFromLibrary(t *testing.T) { Host: sql.NullString{String: "github.com", Valid: true}, Event: sql.NullString{String: "pull_request", Valid: true}, EventAction: sql.NullString{String: "opened", Valid: true}, - Branch: sql.NullString{String: "master", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, Error: sql.NullString{String: "", Valid: false}, Status: sql.NullString{String: "success", Valid: true}, Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, @@ -231,7 +231,7 @@ func testHook() *Hook { Host: sql.NullString{String: "github.com", Valid: true}, Event: sql.NullString{String: "push", Valid: true}, EventAction: sql.NullString{String: "", Valid: false}, - Branch: sql.NullString{String: "master", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, Error: sql.NullString{String: "", Valid: false}, Status: sql.NullString{String: "success", Valid: true}, Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, diff --git a/go.mod b/go.mod index 764f3775..161f50a7 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( require ( github.com/aymerick/douceur v0.2.0 // indirect + github.com/google/go-cmp v0.6.0 github.com/gorilla/css v1.0.0 // indirect github.com/kr/pretty v0.2.0 // indirect golang.org/x/net v0.17.0 // indirect diff --git a/go.sum b/go.sum index 30a8143f..8dd372a4 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=