//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format. //Copyright (C) 2024 HTWKalender support@htwkalender.de //This program is free software: you can redistribute it and/or modify //it under the terms of the GNU Affero General Public License as published by //the Free Software Foundation, either version 3 of the License, or //(at your option) any later version. //This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Affero General Public License for more details. //You should have received a copy of the GNU Affero General Public License //along with this program. If not, see . package test import ( "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/tests" "htwkalender/data-manager/model" "htwkalender/data-manager/service/feed" mockTime "htwkalender/data-manager/service/functions/time" "reflect" "testing" "time" ) const testDataDir = "../mockData" func setupTestApp(t testing.TB) *tests.TestApp { testApp, err := tests.NewTestApp(testDataDir) if err != nil { t.Fatal(err) } return testApp } func TestClearFeeds(t *testing.T) { type args struct { months int mockClock mockTime.MockClock } testCases := []struct { name string args args want int }{ { name: "Clear feeds older than 6 months", args: args{ months: 6, mockClock: mockTime.MockClock{ NowTime: time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC), }, }, want: 1, }, { name: "Clear all feeds - recent clock", args: args{ months: 1, mockClock: mockTime.MockClock{ NowTime: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), }, }, want: 0, }, { name: "No clearing - very old clock", args: args{ months: 1, mockClock: mockTime.MockClock{ NowTime: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC), }, }, want: 3, }, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { app := setupTestApp(t) base := &pocketbase.PocketBase{App: app} feed.ClearFeeds(base, tt.args.months, tt.args.mockClock) var feeds []*model.Feed err := app.DB().Select("id").From("feeds").All(&feeds) if err != nil { t.Fatal(err) } if got := len(feeds); got != tt.want { t.Errorf("ClearFeeds() = %v, want %v", got, tt.want) } }) } } func TestCombineEventsInFeed(t *testing.T) { type args struct { events model.Events } testCases := []struct { name string args args want model.Events }{ { name: "Combine duplicate events with different rooms and notes", args: args{ events: model.Events{ { Name: "Modellierung", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI001", Notes: "Gruppe 2", }, { Name: "Modellierung", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI002", Notes: "Gruppe 1", }, }, }, want: model.Events{ { Name: "Modellierung", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI001, LI002", Notes: "LI001 - Gruppe 2 (Prof. Bunt)\nLI002 - Gruppe 1 (Prof. Bunt)", }, }, }, { name: "Do not combine different events", args: args{ events: model.Events{ { Name: "Modellierung", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI001", Notes: "Gruppe 2", }, { Name: "Modellierung - 2", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI002", Notes: "Gruppe 1", }, }, }, want: model.Events{ { Name: "Modellierung", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI001", Notes: "Gruppe 2", }, { Name: "Modellierung - 2", Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)), End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)), Prof: "Prof. Bunt", Rooms: "LI002", Notes: "Gruppe 1", }, }, }, { name: "Empty events input", args: args{ events: model.Events{}, }, want: model.Events{}, }, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { if got := feed.CombineEventsInFeed(tt.args.events); !reflect.DeepEqual(got, tt.want) { t.Errorf("CombineEventsInFeed() = %v, want %v", got, tt.want) } }) } }