Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Dec 15, 2023
1 parent 2da3863 commit c43cbf0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 152 deletions.
64 changes: 28 additions & 36 deletions database/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ var (

// Hook is the database representation of a webhook for a repo.
type Hook struct {
ID sql.NullInt64 `sql:"id"`
RepoID sql.NullInt64 `sql:"repo_id"`
BuildID sql.NullInt64 `sql:"build_id"`
Number sql.NullInt32 `sql:"number"`
SourceID sql.NullString `sql:"source_id"`
Created sql.NullInt64 `sql:"created"`
Host sql.NullString `sql:"host"`
Event sql.NullString `sql:"event"`
EventAction sql.NullString `sql:"event_action"`
Branch sql.NullString `sql:"branch"`
Error sql.NullString `sql:"error"`
Status sql.NullString `sql:"status"`
Link sql.NullString `sql:"link"`
WebhookID sql.NullInt64 `sql:"webhook_id"`
DeploymentID sql.NullInt64 `sql:"deployment_id"`
ID sql.NullInt64 `sql:"id"`
RepoID sql.NullInt64 `sql:"repo_id"`
BuildID sql.NullInt64 `sql:"build_id"`
Number sql.NullInt32 `sql:"number"`
SourceID sql.NullString `sql:"source_id"`
Created sql.NullInt64 `sql:"created"`
Host sql.NullString `sql:"host"`
Event sql.NullString `sql:"event"`
EventAction sql.NullString `sql:"event_action"`
Branch sql.NullString `sql:"branch"`
Error sql.NullString `sql:"error"`
Status sql.NullString `sql:"status"`
Link sql.NullString `sql:"link"`
WebhookID sql.NullInt64 `sql:"webhook_id"`
}

// Nullify ensures the valid flag for
Expand Down Expand Up @@ -127,11 +126,6 @@ func (h *Hook) Nullify() *Hook {
h.WebhookID.Valid = false
}

// check if the DeploymentID field should be false
if h.DeploymentID.Int64 == 0 {
h.DeploymentID.Valid = false
}

return h
}

Expand All @@ -154,7 +148,6 @@ func (h *Hook) ToLibrary() *library.Hook {
hook.SetStatus(h.Status.String)
hook.SetLink(h.Link.String)
hook.SetWebhookID(h.WebhookID.Int64)
hook.SetDeploymentID(h.DeploymentID.Int64)

return hook
}
Expand Down Expand Up @@ -201,21 +194,20 @@ func (h *Hook) Validate() error {
// to a library Hook type.
func HookFromLibrary(h *library.Hook) *Hook {
hook := &Hook{
ID: sql.NullInt64{Int64: h.GetID(), Valid: true},
RepoID: sql.NullInt64{Int64: h.GetRepoID(), Valid: true},
BuildID: sql.NullInt64{Int64: h.GetBuildID(), Valid: true},
Number: sql.NullInt32{Int32: int32(h.GetNumber()), Valid: true},
SourceID: sql.NullString{String: h.GetSourceID(), Valid: true},
Created: sql.NullInt64{Int64: h.GetCreated(), Valid: true},
Host: sql.NullString{String: h.GetHost(), Valid: true},
Event: sql.NullString{String: h.GetEvent(), Valid: true},
EventAction: sql.NullString{String: h.GetEventAction(), Valid: true},
Branch: sql.NullString{String: h.GetBranch(), Valid: true},
Error: sql.NullString{String: h.GetError(), Valid: true},
Status: sql.NullString{String: h.GetStatus(), Valid: true},
Link: sql.NullString{String: h.GetLink(), Valid: true},
WebhookID: sql.NullInt64{Int64: h.GetWebhookID(), Valid: true},
DeploymentID: sql.NullInt64{Int64: h.GetDeploymentID(), Valid: true},
ID: sql.NullInt64{Int64: h.GetID(), Valid: true},
RepoID: sql.NullInt64{Int64: h.GetRepoID(), Valid: true},
BuildID: sql.NullInt64{Int64: h.GetBuildID(), Valid: true},
Number: sql.NullInt32{Int32: int32(h.GetNumber()), Valid: true},
SourceID: sql.NullString{String: h.GetSourceID(), Valid: true},
Created: sql.NullInt64{Int64: h.GetCreated(), Valid: true},
Host: sql.NullString{String: h.GetHost(), Valid: true},
Event: sql.NullString{String: h.GetEvent(), Valid: true},
EventAction: sql.NullString{String: h.GetEventAction(), Valid: true},
Branch: sql.NullString{String: h.GetBranch(), Valid: true},
Error: sql.NullString{String: h.GetError(), Valid: true},
Status: sql.NullString{String: h.GetStatus(), Valid: true},
Link: sql.NullString{String: h.GetLink(), Valid: true},
WebhookID: sql.NullInt64{Int64: h.GetWebhookID(), Valid: true},
}

return hook.Nullify()
Expand Down
118 changes: 56 additions & 62 deletions database/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ func TestDatabase_Hook_Nullify(t *testing.T) {
var h *Hook

want := &Hook{
ID: sql.NullInt64{Int64: 0, Valid: false},
RepoID: sql.NullInt64{Int64: 0, Valid: false},
BuildID: sql.NullInt64{Int64: 0, Valid: false},
Number: sql.NullInt32{Int32: 0, Valid: false},
SourceID: sql.NullString{String: "", Valid: false},
Created: sql.NullInt64{Int64: 0, Valid: false},
Host: sql.NullString{String: "", Valid: false},
Event: sql.NullString{String: "", Valid: false},
EventAction: sql.NullString{String: "", Valid: false},
Branch: sql.NullString{String: "", Valid: false},
Error: sql.NullString{String: "", Valid: false},
Status: sql.NullString{String: "", Valid: false},
Link: sql.NullString{String: "", Valid: false},
WebhookID: sql.NullInt64{Int64: 0, Valid: false},
DeploymentID: sql.NullInt64{Int64: 0, Valid: false},
ID: sql.NullInt64{Int64: 0, Valid: false},
RepoID: sql.NullInt64{Int64: 0, Valid: false},
BuildID: sql.NullInt64{Int64: 0, Valid: false},
Number: sql.NullInt32{Int32: 0, Valid: false},
SourceID: sql.NullString{String: "", Valid: false},
Created: sql.NullInt64{Int64: 0, Valid: false},
Host: sql.NullString{String: "", Valid: false},
Event: sql.NullString{String: "", Valid: false},
EventAction: sql.NullString{String: "", Valid: false},
Branch: sql.NullString{String: "", Valid: false},
Error: sql.NullString{String: "", Valid: false},
Status: sql.NullString{String: "", Valid: false},
Link: sql.NullString{String: "", Valid: false},
WebhookID: sql.NullInt64{Int64: 0, Valid: false},
}

// setup tests
Expand Down Expand Up @@ -79,24 +78,22 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) {
want.SetStatus("success")
want.SetLink("https://github.com/github/octocat/settings/hooks/1")
want.SetWebhookID(123456)
want.SetDeploymentID(0)

h := &Hook{
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
DeploymentID: sql.NullInt64{Int64: 0, Valid: false},
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
}

// run test
Expand Down Expand Up @@ -176,21 +173,20 @@ func TestDatabase_Hook_Validate(t *testing.T) {
func TestDatabase_HookFromLibrary(t *testing.T) {
// setup types
want := &Hook{
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
DeploymentID: sql.NullInt64{Int64: 0, Valid: false},
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
}

h := new(library.Hook)
Expand All @@ -208,7 +204,6 @@ func TestDatabase_HookFromLibrary(t *testing.T) {
h.SetStatus("success")
h.SetLink("https://github.com/github/octocat/settings/hooks/1")
h.SetWebhookID(123456)
h.SetDeploymentID(0)

// run test
got := HookFromLibrary(h)
Expand All @@ -222,20 +217,19 @@ func TestDatabase_HookFromLibrary(t *testing.T) {
// type with all fields set to a fake value.
func testHook() *Hook {
return &Hook{
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
DeploymentID: sql.NullInt64{Int64: 0, Valid: false},
ID: sql.NullInt64{Int64: 1, Valid: true},
RepoID: sql.NullInt64{Int64: 1, Valid: true},
BuildID: sql.NullInt64{Int64: 1, Valid: true},
Number: sql.NullInt32{Int32: 1, Valid: true},
SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true},
Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true},
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: "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},
WebhookID: sql.NullInt64{Int64: 123456, Valid: true},
}
}
57 changes: 14 additions & 43 deletions library/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import (
//
// swagger:model Webhook
type Hook struct {
ID *int64 `json:"id,omitempty"`
RepoID *int64 `json:"repo_id,omitempty"`
BuildID *int64 `json:"build_id,omitempty"`
Number *int `json:"number,omitempty"`
SourceID *string `json:"source_id,omitempty"`
Created *int64 `json:"created,omitempty"`
Host *string `json:"host,omitempty"`
Event *string `json:"event,omitempty"`
EventAction *string `json:"event_action,omitempty"`
Branch *string `json:"branch,omitempty"`
Error *string `json:"error,omitempty"`
Status *string `json:"status,omitempty"`
Link *string `json:"link,omitempty"`
WebhookID *int64 `json:"webhook_id,omitempty"`
DeploymentID *int64 `json:"deployment,omitempty"`
ID *int64 `json:"id,omitempty"`
RepoID *int64 `json:"repo_id,omitempty"`
BuildID *int64 `json:"build_id,omitempty"`
Number *int `json:"number,omitempty"`
SourceID *string `json:"source_id,omitempty"`
Created *int64 `json:"created,omitempty"`
Host *string `json:"host,omitempty"`
Event *string `json:"event,omitempty"`
EventAction *string `json:"event_action,omitempty"`
Branch *string `json:"branch,omitempty"`
Error *string `json:"error,omitempty"`
Status *string `json:"status,omitempty"`
Link *string `json:"link,omitempty"`
WebhookID *int64 `json:"webhook_id,omitempty"`
}

// GetID returns the ID field.
Expand Down Expand Up @@ -209,19 +208,6 @@ func (h *Hook) GetWebhookID() int64 {
return *h.WebhookID
}

// GetDeploymentID returns the DeploymentID field.
//
// When the provided Hook type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (h *Hook) GetDeploymentID() int64 {
// return zero value if Hook type or WebhookID field is nil
if h == nil || h.DeploymentID == nil {
return 0
}

return *h.DeploymentID
}

// SetID sets the ID field.
//
// When the provided Hook type is nil, it
Expand Down Expand Up @@ -404,19 +390,6 @@ func (h *Hook) SetWebhookID(v int64) {
h.WebhookID = &v
}

// SetDeploymentID sets the DeploymentID field.
//
// When the provided Hook type is nil, it
// will set nothing and immediately return.
func (h *Hook) SetDeploymentID(v int64) {
// return if Hook type is nil
if h == nil {
return
}

h.DeploymentID = &v
}

// String implements the Stringer interface for the Hook type.
func (h *Hook) String() string {
return fmt.Sprintf(`{
Expand All @@ -434,7 +407,6 @@ func (h *Hook) String() string {
SourceID: %s,
Status: %s,
WebhookID: %d,
DeploymentID: %d,
}`,
h.GetBranch(),
h.GetBuildID(),
Expand All @@ -450,6 +422,5 @@ func (h *Hook) String() string {
h.GetSourceID(),
h.GetStatus(),
h.GetWebhookID(),
h.GetDeploymentID(),
)
}
7 changes: 0 additions & 7 deletions library/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func TestLibrary_Hook_Setters(t *testing.T) {
test.hook.SetStatus(test.want.GetStatus())
test.hook.SetLink(test.want.GetLink())
test.hook.SetWebhookID(test.want.GetWebhookID())
test.hook.SetDeploymentID(test.want.GetDeploymentID())

if test.hook.GetID() != test.want.GetID() {
t.Errorf("SetID is %v, want %v", test.hook.GetID(), test.want.GetID())
Expand Down Expand Up @@ -178,9 +177,6 @@ func TestLibrary_Hook_Setters(t *testing.T) {
t.Errorf("SetWebhookID is %v, want %v", test.hook.GetWebhookID(), test.want.GetWebhookID())
}

Check failure on line 179 in library/hook_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] library/hook_test.go#L179

unnecessary trailing newline (whitespace)
Raw output
library/hook_test.go:179: unnecessary trailing newline (whitespace)

	}
if test.hook.GetDeploymentID() != test.want.GetDeploymentID() {
t.Errorf("SetDeploymentID is %v, want %v", test.hook.GetDeploymentID(), test.want.GetDeploymentID())
}
}

Check failure on line 180 in library/hook_test.go

View workflow job for this annotation

GitHub Actions / full-review

block should not end with a whitespace (or comment) (wsl)

Check failure on line 180 in library/hook_test.go

View workflow job for this annotation

GitHub Actions / diff-review

block should not end with a whitespace (or comment) (wsl)

Check failure on line 180 in library/hook_test.go

View workflow job for this annotation

GitHub Actions / diff-review

block should not end with a whitespace (or comment) (wsl)

Check failure on line 180 in library/hook_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] library/hook_test.go#L180

block should not end with a whitespace (or comment) (wsl)
Raw output
library/hook_test.go:180:2: block should not end with a whitespace (or comment) (wsl)
	}
	^
}

Expand All @@ -203,7 +199,6 @@ func TestLibrary_Hook_String(t *testing.T) {
SourceID: %s,
Status: %s,
WebhookID: %d,
DeploymentID: %d,
}`,
h.GetBranch(),
h.GetBuildID(),
Expand All @@ -219,7 +214,6 @@ func TestLibrary_Hook_String(t *testing.T) {
h.GetSourceID(),
h.GetStatus(),
h.GetWebhookID(),
h.GetDeploymentID(),
)

// run test
Expand Down Expand Up @@ -249,7 +243,6 @@ func testHook() *Hook {
h.SetStatus("success")
h.SetLink("https://github.com/github/octocat/settings/hooks/1")
h.SetWebhookID(123456)
h.SetWebhookID(0)

return h
}
9 changes: 5 additions & 4 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ type PullRequest struct {
// the required data when processing webhook event
// a for a source provider event.
type Webhook struct {
Hook *library.Hook
Repo *library.Repo
Build *library.Build
PullRequest PullRequest
Hook *library.Hook
Repo *library.Repo
Build *library.Build
PullRequest PullRequest
DeploymentID *int64
}

// ShouldSkip uses the build information
Expand Down

0 comments on commit c43cbf0

Please sign in to comment.