85 lines
2.0 KiB
Go
85 lines
2.0 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) {
|
|
testCases := []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 _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if FormatDuration(tc.duration) != tc.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")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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!")
|
|
}
|
|
})
|
|
}
|
|
}
|