173 lines
4.0 KiB
Go
173 lines
4.0 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 TestFormatDurationFill(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 TestFormatDuration(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
duration time.Duration
|
|
}{
|
|
{"", 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 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!")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetKW(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
date time.Time
|
|
want int
|
|
}{
|
|
{
|
|
name: "First week of year",
|
|
date: time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC), // Monday
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "Middle of year",
|
|
date: time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC),
|
|
want: 24,
|
|
},
|
|
{
|
|
name: "Last week of year",
|
|
date: time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC),
|
|
want: 52,
|
|
},
|
|
{
|
|
name: "ISO week crossing into next year",
|
|
date: time.Date(2020, 12, 31, 0, 0, 0, 0, time.UTC),
|
|
want: 53,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := GetKW(tt.date)
|
|
if got != tt.want {
|
|
t.Errorf("GetKW(%v) = %d, want %d", tt.date, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|