Skip to content

Commit

Permalink
fix(events): add schedule to ToDatabase and improve tests (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Jan 17, 2024
1 parent 00dcbc6 commit 0e2e18b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 74 deletions.
14 changes: 13 additions & 1 deletion library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (e *Events) Allowed(event, action string) bool {
allowed = e.GetPullRequest().GetSynchronize()
case constants.EventPull + ":" + constants.ActionEdited:
allowed = e.GetPullRequest().GetEdited()
case constants.EventPull + ":" + constants.ActionReopened:
allowed = e.GetPullRequest().GetReopened()
case constants.EventTag:
allowed = e.GetPush().GetTag()
case constants.EventComment + ":" + constants.ActionCreated:
Expand Down Expand Up @@ -99,6 +101,10 @@ func (e *Events) List() []string {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionEdited)
}

if e.GetPullRequest().GetReopened() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionReopened)
}

if e.GetPush().GetTag() {
eventSlice = append(eventSlice, constants.EventTag)
}
Expand Down Expand Up @@ -132,7 +138,13 @@ func (e *Events) List() []string {

// ToDatabase is an Events method that converts a nested Events struct into an integer event mask.
func (e *Events) ToDatabase() int64 {
return 0 | e.GetPush().ToMask() | e.GetPullRequest().ToMask() | e.GetComment().ToMask() | e.GetDeployment().ToMask() | e.GetDelete().ToMask()
return 0 |
e.GetPush().ToMask() |
e.GetPullRequest().ToMask() |
e.GetComment().ToMask() |
e.GetDeployment().ToMask() |
e.GetSchedule().ToMask() |
e.GetDelete().ToMask()
}

// GetPush returns the Push field from the provided Events. If the object is nil,
Expand Down
232 changes: 164 additions & 68 deletions library/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ import (

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library/actions"
"github.com/google/go-cmp/cmp"
)

func TestLibrary_Events_Getters(t *testing.T) {
// setup types
eventsOne, eventsTwo := testEvents()

// setup tests
tests := []struct {
events *Events
want *Events
}{
{
events: testEvents(),
want: testEvents(),
events: eventsOne,
want: eventsOne,
},
{
events: eventsTwo,
want: eventsTwo,
},
{
events: new(Events),
Expand All @@ -44,6 +52,10 @@ func TestLibrary_Events_Getters(t *testing.T) {
t.Errorf("GetComment is %v, want %v", test.events.GetPush(), test.want.GetPush())
}

if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) {
t.Errorf("GetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule())
}

if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) {
t.Errorf("GetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete())
}
Expand All @@ -54,14 +66,20 @@ func TestLibrary_Events_Setters(t *testing.T) {
// setup types
var e *Events

eventsOne, eventsTwo := testEvents()

// setup tests
tests := []struct {
events *Events
want *Events
}{
{
events: testEvents(),
want: testEvents(),
events: eventsOne,
want: eventsOne,
},
{
events: eventsTwo,
want: eventsTwo,
},
{
events: e,
Expand All @@ -75,6 +93,7 @@ func TestLibrary_Events_Setters(t *testing.T) {
test.events.SetPullRequest(test.want.GetPullRequest())
test.events.SetDeployment(test.want.GetDeployment())
test.events.SetComment(test.want.GetComment())
test.events.SetSchedule(test.want.GetSchedule())
test.events.SetDelete(test.want.GetDelete())

if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) {
Expand All @@ -93,6 +112,10 @@ func TestLibrary_Events_Setters(t *testing.T) {
t.Errorf("SetComment is %v, want %v", test.events.GetComment(), test.want.GetComment())
}

if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) {
t.Errorf("SetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule())
}

if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) {
t.Errorf("SetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete())
}
Expand All @@ -101,108 +124,181 @@ func TestLibrary_Events_Setters(t *testing.T) {

func TestLibrary_Events_List(t *testing.T) {
// setup types
e := testEvents()
eventsOne, eventsTwo := testEvents()

wantOne := []string{
"push",
"pull_request:opened",
"pull_request:synchronize",
"pull_request:reopened",
"tag",
"comment:created",
"schedule",
"delete:branch",
}

want := []string{"push", "pull_request:opened", "pull_request:synchronize", "tag", "delete:branch", "delete:tag"}
wantTwo := []string{
"pull_request:edited",
"deployment",
"comment:edited",
"delete:tag",
}

// run test
got := e.List()
gotOne := eventsOne.List()

if diff := cmp.Diff(wantOne, gotOne); diff != "" {
t.Errorf("(List: -want +got):\n%s", diff)
}

gotTwo := eventsTwo.List()

if !reflect.DeepEqual(got, want) {
t.Errorf("List is %v, want %v", got, want)
if diff := cmp.Diff(wantTwo, gotTwo); diff != "" {
t.Errorf("(List Inverse: -want +got):\n%s", diff)
}
}

func TestLibrary_Events_NewEventsFromMask(t *testing.T) {
func TestLibrary_Events_NewEventsFromMask_ToDatabase(t *testing.T) {
// setup mask
mask := int64(
maskOne := int64(
constants.AllowPushBranch |
constants.AllowPushTag |
constants.AllowPullOpen |
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowDeleteBranch |
constants.AllowCommentCreate |
constants.AllowSchedule |
constants.AllowDeleteBranch,
)

maskTwo := int64(
constants.AllowPullEdit |
constants.AllowCommentEdit |
constants.AllowDeployCreate |
constants.AllowDeleteTag,
)

want := testEvents()
wantOne, wantTwo := testEvents()

// run test
got := NewEventsFromMask(mask)
gotOne := NewEventsFromMask(maskOne)

if diff := cmp.Diff(wantOne, gotOne); diff != "" {
t.Errorf("(NewEventsFromMask: -want +got):\n%s", diff)
}

gotTwo := NewEventsFromMask(maskTwo)

if !reflect.DeepEqual(got, want) {
t.Errorf("NewEventsFromMask is %v, want %v", got, want)
if diff := cmp.Diff(wantTwo, gotTwo); diff != "" {
t.Errorf("(NewEventsFromMask Inverse: -want +got):\n%s", diff)
}

// ensure ToDatabase maps back to masks
if gotOne.ToDatabase() != maskOne {
t.Errorf("ToDatabase returned %d, want %d", gotOne.ToDatabase(), maskOne)
}

if gotTwo.ToDatabase() != maskTwo {
t.Errorf("ToDatabase returned %d, want %d", gotTwo.ToDatabase(), maskTwo)
}
}

func TestLibrary_Events_Allowed(t *testing.T) {
// setup types
eventsOne, eventsTwo := testEvents()

// setup tests
tests := []struct {
events *Events
event string
action string
want bool
}{
{
events: testEvents(),
event: "pull_request",
action: "opened",
want: true,
},
{
events: testEvents(),
event: "deployment",
want: false,
},
{
events: testEvents(),
event: "push",
want: true,
},
{event: "push", want: true},
{event: "tag", want: true},
{event: "pull_request", action: "opened", want: true},
{event: "pull_request", action: "synchronize", want: true},
{event: "pull_request", action: "edited", want: false},
{event: "pull_request", action: "reopened", want: true},
{event: "deployment", want: false},
{event: "comment", action: "created", want: true},
{event: "comment", action: "edited", want: false},
{event: "schedule", want: true},
{event: "delete", action: "branch", want: true},
{event: "delete", action: "tag", want: false},
}

for _, test := range tests {
got := test.events.Allowed(test.event, test.action)
gotOne := eventsOne.Allowed(test.event, test.action)
gotTwo := eventsTwo.Allowed(test.event, test.action)

if gotOne != test.want {
t.Errorf("Allowed for %s/%s is %v, want %v", test.event, test.action, gotOne, test.want)
}

if got != test.want {
t.Errorf("Allowed is %v, want %v", got, test.want)
if gotTwo == test.want {
t.Errorf("Allowed Inverse for %s/%s is %v, want %v", test.event, test.action, gotTwo, !test.want)
}
}
}

func testEvents() *Events {
e := new(Events)

pr := new(actions.Pull)
pr.SetOpened(true)
pr.SetSynchronize(true)
pr.SetEdited(false)
pr.SetReopened(true)

push := new(actions.Push)
push.SetBranch(true)
push.SetTag(true)

deploy := new(actions.Deploy)
deploy.SetCreated(false)
// testEvents is a helper test function that returns an Events struct and its inverse for unit test coverage.
func testEvents() (*Events, *Events) {
tBool := true
fBool := false

comment := new(actions.Comment)
comment.SetCreated(false)
comment.SetEdited(false)

schedule := new(actions.Schedule)
schedule.SetRun(false)

deletion := new(actions.Delete)
deletion.SetBranch(true)
deletion.SetTag(true)
e1 := &Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
},
PullRequest: &actions.Pull{
Opened: &tBool,
Synchronize: &tBool,
Edited: &fBool,
Reopened: &tBool,
},
Deployment: &actions.Deploy{
Created: &fBool,
},
Comment: &actions.Comment{
Created: &tBool,
Edited: &fBool,
},
Schedule: &actions.Schedule{
Run: &tBool,
},
Delete: &actions.Delete{
Branch: &tBool,
Tag: &fBool,
},
}

e.SetPush(push)
e.SetPullRequest(pr)
e.SetDeployment(deploy)
e.SetComment(comment)
e.SetSchedule(schedule)
e.SetDelete(deletion)
e2 := &Events{
Push: &actions.Push{
Branch: &fBool,
Tag: &fBool,
},
PullRequest: &actions.Pull{
Opened: &fBool,
Synchronize: &fBool,
Edited: &tBool,
Reopened: &fBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
},
Comment: &actions.Comment{
Created: &fBool,
Edited: &tBool,
},
Schedule: &actions.Schedule{
Run: &fBool,
},
Delete: &actions.Delete{
Branch: &fBool,
Tag: &tBool,
},
}

return e
return e1, e2
}
Loading

0 comments on commit 0e2e18b

Please sign in to comment.