-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from inovex/regexTitleFilter
Add regexTitle Filter
- Loading branch information
Showing
8 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package filter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/inovex/CalendarSync/internal/filter" | ||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
// All Day Events should be filtered | ||
func TestAllDayEventsFilter(t *testing.T) { | ||
sourceEvents := []models.Event{ | ||
{ | ||
ICalUID: "testId", | ||
ID: "testUid", | ||
Title: "test", | ||
Description: "bar", | ||
AllDay: true, | ||
}, | ||
{ | ||
ICalUID: "testId2", | ||
ID: "testUid2", | ||
Title: "Test 2", | ||
Description: "bar", | ||
AllDay: false, | ||
}, | ||
{ | ||
ICalUID: "testId3", | ||
ID: "testUid2", | ||
Title: "foo", | ||
Description: "bar", | ||
}, | ||
} | ||
|
||
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]} | ||
|
||
eventFilter := filter.AllDayEvents{} | ||
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package filter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/inovex/CalendarSync/internal/filter" | ||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
// Declined Events should be filtered | ||
func TestDeclinedEventsFilter(t *testing.T) { | ||
sourceEvents := []models.Event{ | ||
{ | ||
ICalUID: "testId", | ||
ID: "testUid", | ||
Title: "test", | ||
Description: "bar", | ||
Accepted: false, | ||
}, | ||
{ | ||
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]} | ||
|
||
eventFilter := filter.DeclinedEvents{} | ||
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package filter | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/charmbracelet/log" | ||
|
||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
type RegexTitle struct { | ||
ExcludeRegexp string | ||
} | ||
|
||
func (a RegexTitle) Name() string { | ||
return "RegexTitle" | ||
} | ||
|
||
func (a RegexTitle) Filter(event models.Event) bool { | ||
|
||
if len(a.ExcludeRegexp) == 0 { | ||
log.Debugf("Regular Expression is empty, skipping Filter %s for event: %s", a.Name(), event.Title) | ||
return true | ||
} | ||
|
||
log.Debugf("Running Regexp %s on event title: %s", a.ExcludeRegexp, event.Title) | ||
|
||
r, err := regexp.Compile(a.ExcludeRegexp) | ||
if err != nil { | ||
log.Fatalf("Regular expression of Filter %s is not valid, please check", a.Name()) | ||
} | ||
|
||
// if the title matches the Regexp, return false (filter the event) | ||
if r.MatchString(event.Title) { | ||
log.Debugf("Regular Expression %s matches the events title: %s, gets filtered", a.Name(), event.Title) | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package filter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/inovex/CalendarSync/internal/filter" | ||
"github.com/inovex/CalendarSync/internal/models" | ||
) | ||
|
||
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) { | ||
|
||
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]} | ||
|
||
eventFilter := filter.RegexTitle{ | ||
ExcludeRegexp: ".*test", | ||
} | ||
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) | ||
} | ||
|
||
// All Events should be there | ||
func TestRegexTitleFilterEmptyRegex(t *testing.T) { | ||
expectedSinkEvents := sourceEvents | ||
|
||
eventFilter := filter.RegexTitle{ | ||
ExcludeRegexp: "", | ||
} | ||
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package filter_test | ||
|
||
import ( | ||
"github.com/inovex/CalendarSync/internal/models" | ||
"github.com/inovex/CalendarSync/internal/sync" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// FilterEvents takes an array of events and a filter and executes the .Filter Method on each of the sourceEvents | ||
// Not excluded events get returned in the filteredEvents | ||
func FilterEvents(sourceEvents []models.Event, filter sync.Filter) (filteredEvents []models.Event) { | ||
for _, event := range sourceEvents { | ||
if filter.Filter(event) { | ||
filteredEvents = append(filteredEvents, event) | ||
} | ||
} | ||
return filteredEvents | ||
} | ||
|
||
func checkEventFilter(t assert.TestingT, filter sync.Filter, sourceEvents []models.Event, expectedEvents []models.Event) { | ||
filteredEvents := FilterEvents(sourceEvents, filter) | ||
assert.Equal(t, expectedEvents, filteredEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters