Files
arbeitszeitmessung/Backend/helper/time_test.go

61 lines
1.4 KiB
Go

package helper
import (
"fmt"
"testing"
"time"
)
func TestGetMonday(t *testing.T) {
isMonday, err := time.Parse(time.DateOnly, "2025-07-14")
notMonday, err := time.Parse(time.DateOnly, "2025-07-16")
if err != nil || isMonday.Equal(notMonday) {
t.Errorf("U stupid? %e", err)
}
if GetMonday(isMonday) != isMonday || GetMonday(notMonday) != isMonday {
t.Error("Wrong date conversion!")
}
}
func TestFormatDuration(t *testing.T) {
durations := []struct {
name string
duration time.Duration
}{
{"2h", time.Duration(120 * time.Minute)},
{"30min", time.Duration(30 * time.Minute)},
{"1h 30min", time.Duration(90 * time.Minute)},
{"-1h 30min", time.Duration(-90 * time.Minute)},
{"", 0},
}
for _, d := range durations {
t.Run(d.name, func(t *testing.T) {
if FormatDuration(d.duration) != d.name {
t.Error("Format missmatch in Formatduration.")
}
})
}
}
func TestGetWorkingDays(t *testing.T) {
testCases := []struct {
start string
end string
days int
}{
{"2025-10-01", "2025-10-02", 2},
{"2025-10-02", "2025-10-01", 0},
{"2025-10-01", "2025-10-31", 23},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("WorkingDayTest: %d days", tc.days), func(t *testing.T) {
startDate, _ := time.Parse(time.DateOnly, tc.start)
endDate, _ := time.Parse(time.DateOnly, tc.end)
if GetWorkingDays(startDate, endDate) != tc.days {
t.Error("Calculated workdays do not match target")
}
})
}
}