Füge Tests für date und config hinzu

This commit is contained in:
Gonne 2022-11-06 15:48:37 +01:00
parent fe54d76ab2
commit 3db1627680
3 changed files with 78 additions and 1 deletions

15
config/config_test.go Normal file
View file

@ -0,0 +1,15 @@
package config
import (
"testing"
)
// Verify that the sample config in config/config.json can be read to a config struct
// and passes the validation
func TestSampleConfig(t *testing.T) {
var conf Config
err := ReadConfigFile("config.json", &conf)
if err != nil {
t.Errorf("Unable to use sample config file: %s", err)
}
}

View file

@ -59,7 +59,7 @@ func GetEndDate(date Date, duration int, ignoreWeek bool) Date {
endDate = Date{date.Week, date.Day, date.Hour, date.Minute} endDate = Date{date.Week, date.Day, date.Hour, date.Minute}
} }
endDate.Day = (endDate.Day + (endDate.Hour*60+endDate.Minute+duration)/(60*24)) % 7 endDate.Day = (endDate.Day + (endDate.Hour*60+endDate.Minute+duration)/(60*24)) % 7
endDate.Hour = endDate.Hour + (endDate.Minute+duration)/60 endDate.Hour = (endDate.Hour + (endDate.Minute+duration)/60) % 24
endDate.Minute = (endDate.Minute + duration) % 60 endDate.Minute = (endDate.Minute + duration) % 60
return endDate return endDate
} }

62
models/date_test.go Normal file
View file

@ -0,0 +1,62 @@
package models
import (
"fmt"
"testing"
)
func TestEndDate(t *testing.T) {
testCases := []struct {
startDate Date
duration int
endDate Date
}{
{Date{Week: 0, Day: 0, Hour: 8, Minute: 0}, 10, Date{Week: 0, Day: 0, Hour: 8, Minute: 10}},
{Date{Week: 0, Day: 0, Hour: 8, Minute: 0}, 90, Date{Week: 0, Day: 0, Hour: 9, Minute: 30}}, // More than one hour
{Date{Week: 0, Day: 0, Hour: 8, Minute: 0}, 130, Date{Week: 0, Day: 0, Hour: 10, Minute: 10}}, // More than two hours
{Date{Week: 0, Day: 0, Hour: 8, Minute: 15}, 90, Date{Week: 0, Day: 0, Hour: 9, Minute: 45}}, // More than one hour and start not 0
{Date{Week: 0, Day: 0, Hour: 23, Minute: 15}, 90, Date{Week: 0, Day: 1, Hour: 0, Minute: 45}}, // More than one day
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%d:%d, Dauer %d", tc.startDate.Hour, tc.startDate.Minute, tc.duration), func(t *testing.T) {
got := GetEndDate(tc.startDate, tc.duration, false)
if got != tc.endDate {
t.Errorf("got %v; want %v", got, tc.endDate)
}
})
}
}
func TestDateLess(t *testing.T) {
testCases := []struct {
name string
first Date
second Date
less bool
}{
{"earlier day",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 0, Hour: 7, Minute: 10}, false},
{"earlier hour",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 7, Minute: 10}, false},
{"earlier minute",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 8, Minute: 0}, false},
{"same date",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, false},
{"later minute",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 8, Minute: 20}, true},
{"later hour",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 9, Minute: 10}, true},
{"later day",
Date{Week: 0, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 2, Hour: 8, Minute: 10}, true},
{"week should not matter",
Date{Week: 1, Day: 1, Hour: 8, Minute: 10}, Date{Week: 0, Day: 1, Hour: 9, Minute: 30}, true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := DateLess(tc.first, tc.second)
if got != tc.less {
t.Errorf("got %t; want %t", got, tc.less)
}
})
}
}