From 458eb222a1c5934de3e25738770a4ead1e741de0 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Mon, 6 Nov 2023 17:10:37 +0100 Subject: [PATCH] refactor: deduplicate filter test code and add util func --- internal/filter/allDayEvents_test.go | 23 +---- internal/filter/declinedEvents_test.go | 28 +----- internal/filter/regexTitle_test.go | 118 ++++++------------------- internal/filter/util.go | 21 +++++ 4 files changed, 52 insertions(+), 138 deletions(-) create mode 100644 internal/filter/util.go diff --git a/internal/filter/allDayEvents_test.go b/internal/filter/allDayEvents_test.go index e7812b5..c9fc50f 100644 --- a/internal/filter/allDayEvents_test.go +++ b/internal/filter/allDayEvents_test.go @@ -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) } diff --git a/internal/filter/declinedEvents_test.go b/internal/filter/declinedEvents_test.go index cdd882c..cd7531d 100644 --- a/internal/filter/declinedEvents_test.go +++ b/internal/filter/declinedEvents_test.go @@ -15,7 +15,6 @@ func TestDeclinedEventsFilter(t *testing.T) { ID: "testUid", Title: "test", Description: "bar", - AllDay: true, Accepted: false, }, { @@ -23,7 +22,6 @@ func TestDeclinedEventsFilter(t *testing.T) { ID: "testUid2", Title: "Test 2", Description: "bar", - AllDay: false, Accepted: true, }, { @@ -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) } diff --git a/internal/filter/regexTitle_test.go b/internal/filter/regexTitle_test.go index 7957a7a..ad8f00d 100644 --- a/internal/filter/regexTitle_test.go +++ b/internal/filter/regexTitle_test.go @@ -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) } diff --git a/internal/filter/util.go b/internal/filter/util.go new file mode 100644 index 0000000..6e1fe23 --- /dev/null +++ b/internal/filter/util.go @@ -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 +}