Files
arbeitszeitmessung/Backend/helper/time_test.go
2025-12-03 00:20:11 +01:00

119 lines
2.9 KiB
Go

package helper
import (
"fmt"
"testing"
"time"
)
func TestGetMonday(t *testing.T) {
isMonday, err := time.Parse(time.DateOnly, "2025-07-14")
isSunday, err := time.Parse(time.DateOnly, "2025-07-20")
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 {
t.Error("Wrong date conversion!")
}
if GetMonday(notMonday) != isMonday {
t.Error("Wrong date conversion (notMonday)!")
}
if GetMonday(isSunday) != isMonday {
t.Error("Wrong date conversion (isSunday)!")
}
}
func TestFormatDuration(t *testing.T) {
testCases := []struct {
name string
duration time.Duration
fill bool
}{
{"2h", time.Duration(120 * time.Minute), true},
{"30min", time.Duration(30 * time.Minute), true},
{"1h 30min", time.Duration(90 * time.Minute), true},
{"-1h 30min", time.Duration(-90 * time.Minute), true},
{"0min", 0, true},
{"", 0, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if FormatDurationFill(tc.duration, tc.fill) != tc.name {
t.Error("Format missmatch in Formatduration.")
}
})
}
}
func TestIsSameDate(t *testing.T) {
testCases := []struct {
dateA string
dateB string
result bool
}{
{"2025-12-01 00:00:00", "2025-12-01 00:00:00", true},
{"2025-12-03 00:00:00", "2025-12-02 00:00:00", false},
{"2025-12-03 23:45:00", "2025-12-03 00:00:00", true},
{"2025-12-04 24:12:00", "2025-12-04 00:12:00", false},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("IsSameDateTest: %s date", tc.dateA), func(t *testing.T) {
dateA, _ := time.Parse(time.DateTime, tc.dateA)
dateB, _ := time.Parse(time.DateTime, tc.dateB)
if IsSameDate(dateA, dateB) != tc.result {
t.Errorf("Is SameDate did not match! Result %t", IsSameDate(dateA, dateB))
}
})
}
}
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")
}
})
}
}
func TestFormatGermanDayOfWeek(t *testing.T) {
testCases := []struct {
date string
result string
}{
{"2025-12-01", "Mo"},
{"2025-12-02", "Di"},
{"2025-12-03", "Mi"},
{"2025-12-04", "Do"},
{"2025-12-05", "Fr"},
{"2025-12-06", "Sa"},
{"2025-12-07", "So"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("FormatWeekDayTest: %s date", tc.date), func(t *testing.T) {
date, _ := time.Parse(time.DateOnly, tc.date)
if FormatGermanDayOfWeek(date) != tc.result {
t.Error("Formatted workday did not match!")
}
})
}
}