Skip to content

Commit

Permalink
refactor: deduplicate filter test code and add util func
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Huck committed Nov 7, 2023
1 parent 84d8102 commit 458eb22
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 138 deletions.
23 changes: 2 additions & 21 deletions internal/filter/allDayEvents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,11 @@ func TestAllDayEventsFilter(t *testing.T) {
},
}

expectedSinkEvents := []models.Event{
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]}

filter := AllDayEvents{}

filteredEvents := []models.Event{}

for _, event := range sourceEvents {
if filter.Filter(event) {
filteredEvents = append(filteredEvents, event)
}
}
filteredEvents := FilterEvents(sourceEvents, filter)

assert.Equal(t, expectedSinkEvents, filteredEvents)
}
28 changes: 2 additions & 26 deletions internal/filter/declinedEvents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ func TestDeclinedEventsFilter(t *testing.T) {
ID: "testUid",
Title: "test",
Description: "bar",
AllDay: true,
Accepted: false,
},
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
AllDay: false,
Accepted: true,
},
{
Expand All @@ -35,32 +33,10 @@ func TestDeclinedEventsFilter(t *testing.T) {
},
}

expectedSinkEvents := []models.Event{
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
Accepted: true,
},
{
ICalUID: "testId3",
ID: "testUid3",
Title: "foo",
Description: "bar",
Accepted: true,
},
}
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]}

filter := DeclinedEvents{}

filteredEvents := []models.Event{}

for _, event := range sourceEvents {
if filter.Filter(event) {
filteredEvents = append(filteredEvents, event)
}
}
filteredEvents := FilterEvents(sourceEvents, filter)

assert.Equal(t, expectedSinkEvents, filteredEvents)
}
118 changes: 27 additions & 91 deletions internal/filter/regexTitle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,114 +7,50 @@ import (
"github.com/stretchr/testify/assert"
)

var sourceEvents = []models.Event{
{
ICalUID: "testId",
ID: "testUid",
Title: "test",
Description: "bar",
},
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}

// Some events should be filtered
func TestRegexTitleFilter(t *testing.T) {
sourceEvents := []models.Event{
{
ICalUID: "testId",
ID: "testUid",
Title: "test",
Description: "bar",
},
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}

expectedSinkEvents := []models.Event{
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]}

filter := RegexTitle{
Regexp: ".*test",
ExludeRegexp: ".*test",
}

filteredEvents := []models.Event{}

for _, event := range sourceEvents {
if filter.Filter(event) {
filteredEvents = append(filteredEvents, event)
}
}
filteredEvents := FilterEvents(sourceEvents, filter)

assert.Equal(t, expectedSinkEvents, filteredEvents)
}

// All Events should be there
func TestRegexTitleFilterEmptyRegex(t *testing.T) {
sourceEvents := []models.Event{
{
ICalUID: "testId",
ID: "testUid",
Title: "test",
Description: "bar",
},
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}

expectedSinkEvents := []models.Event{
{
ICalUID: "testId",
ID: "testUid",
Title: "test",
Description: "bar",
},
{
ICalUID: "testId2",
ID: "testUid2",
Title: "Test 2",
Description: "bar",
},
{
ICalUID: "testId3",
ID: "testUid2",
Title: "foo",
Description: "bar",
},
}
expectedSinkEvents := sourceEvents

filter := RegexTitle{
Regexp: "",
ExludeRegexp: "",
}

filteredEvents := []models.Event{}

for _, event := range sourceEvents {
if filter.Filter(event) {
filteredEvents = append(filteredEvents, event)
}
}
filteredEvents := FilterEvents(sourceEvents, filter)

assert.Equal(t, expectedSinkEvents, filteredEvents)
}
21 changes: 21 additions & 0 deletions internal/filter/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package filter

import (
"github.com/inovex/CalendarSync/internal/models"
)

// Need to declare another interface similar to the sync.Filter interface here, otherwise we would land in an import loop
type MockFilter interface {
Filter(models.Event) bool
}

// FilterEvents takes an array of events and a filter and executes the .Filter Method on each of the sourceEvents
// Not exluded events get returned in the filteredEvents
func FilterEvents(sourceEvents []models.Event, filter MockFilter) (filteredEvents []models.Event) {
for _, event := range sourceEvents {
if filter.Filter(event) {
filteredEvents = append(filteredEvents, event)
}
}
return filteredEvents
}

0 comments on commit 458eb22

Please sign in to comment.