|
| 1 | +package filter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/inovex/CalendarSync/internal/filter" |
| 8 | + "github.com/inovex/CalendarSync/internal/models" |
| 9 | +) |
| 10 | + |
| 11 | +// Events which match the start and end hour should be kept |
| 12 | +func TestTimeFilterEventsFilter(t *testing.T) { |
| 13 | + const timeFormat string = "2006-01-02T15:04" |
| 14 | + |
| 15 | + t1, err := time.Parse(timeFormat, "2024-01-01T08:00") |
| 16 | + if err != nil { |
| 17 | + t.Error(err) |
| 18 | + } |
| 19 | + |
| 20 | + t2, err := time.Parse(timeFormat, "2024-01-01T12:15") |
| 21 | + if err != nil { |
| 22 | + t.Error(err) |
| 23 | + } |
| 24 | + |
| 25 | + t3, err := time.Parse(timeFormat, "2024-01-01T12:00") |
| 26 | + if err != nil { |
| 27 | + t.Error(err) |
| 28 | + } |
| 29 | + |
| 30 | + t4, err := time.Parse(timeFormat, "2024-01-01T13:00") |
| 31 | + if err != nil { |
| 32 | + t.Error(err) |
| 33 | + } |
| 34 | + |
| 35 | + sourceEvents := []models.Event{ |
| 36 | + // Should be kept (all-day event) |
| 37 | + { |
| 38 | + ICalUID: "testId", |
| 39 | + ID: "testUid", |
| 40 | + Title: "all-day", |
| 41 | + Description: "bar", |
| 42 | + AllDay: true, |
| 43 | + StartTime: t1, |
| 44 | + EndTime: t1.Add(time.Hour * 1), |
| 45 | + }, |
| 46 | + // Should be kept, as Start AND Endtime are outside the excluded times |
| 47 | + { |
| 48 | + ICalUID: "testId", |
| 49 | + ID: "testUid", |
| 50 | + Title: "outside frame", |
| 51 | + Description: "bar", |
| 52 | + AllDay: false, |
| 53 | + StartTime: t1, |
| 54 | + EndTime: t1.Add(time.Hour * 1), |
| 55 | + }, |
| 56 | + // Should be filtered, as the Start and EndTime are inside the excluded times |
| 57 | + { |
| 58 | + ICalUID: "testId2", |
| 59 | + ID: "testUid2", |
| 60 | + Title: "inside frame", |
| 61 | + Description: "bar", |
| 62 | + AllDay: false, |
| 63 | + StartTime: t2, |
| 64 | + EndTime: t2.Add(time.Minute * 30), |
| 65 | + }, |
| 66 | + // Should be filtered, as the end time is part of the timeframe |
| 67 | + { |
| 68 | + ICalUID: "testId4", |
| 69 | + ID: "inside frame 2", |
| 70 | + Title: "foo", |
| 71 | + Description: "bar", |
| 72 | + StartTime: t2, |
| 73 | + EndTime: t2.Add(time.Minute * 45), |
| 74 | + }, |
| 75 | + // Should be filtered, as start and end times are at exclusion borders |
| 76 | + { |
| 77 | + ICalUID: "testId5", |
| 78 | + ID: "testUid4", |
| 79 | + Title: "inside frame 3", |
| 80 | + Description: "bar", |
| 81 | + StartTime: t3, |
| 82 | + EndTime: t3.Add(time.Hour * 1), |
| 83 | + }, |
| 84 | + // Should be kept, as the start time is at the end of the exclusion border |
| 85 | + { |
| 86 | + ICalUID: "testId5", |
| 87 | + ID: "testUid4", |
| 88 | + Title: "outside frame 2", |
| 89 | + Description: "bar", |
| 90 | + StartTime: t4, |
| 91 | + EndTime: t4.Add(time.Hour * 1), |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[1], sourceEvents[5]} |
| 96 | + |
| 97 | + eventFilter := filter.TimeFilterEvents{ |
| 98 | + // Events from 12 to 13 should be filtered |
| 99 | + HourStart: 12, |
| 100 | + HourEnd: 13, |
| 101 | + } |
| 102 | + checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) |
| 103 | +} |
0 commit comments