Files
arbeitszeitmessung/Backend/models/workDay_test.go
Tom Tröger f21ce9a3c3
All checks were successful
Tests / Run Go Tests (push) Successful in 1m44s
fixed: pause time calculation from work instead of presence time
2026-02-24 20:05:08 +01:00

254 lines
7.7 KiB
Go

package models_test
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
"log"
"testing"
"time"
)
func CatchError[T any](val T, err error) T {
if err != nil {
log.Fatalln(err)
}
return val
}
var testWorkDay = models.WorkDay{
Day: time.Date(2025, 01, 01, 0, 0, 0, 0, time.Local),
Bookings: testBookings8hrs,
TimeFrom: time.Date(2025, 01, 01, 8, 0, 0, 0, time.Local),
TimeTo: time.Date(2025, 01, 01, 16, 30, 0, 0, time.Local),
}
func TestWorkdayWorktimeDay(t *testing.T) {
testCases := []struct {
testName string
bookings []models.Booking
expectedTime time.Duration
}{
{
testName: "Bookings6hrs",
bookings: testBookings6hrs, //work 6h
expectedTime: time.Hour * 6, //pause 0
},
{
testName: "Bookings8hrs",
bookings: testBookings8hrs, //work 8 pause 0
expectedTime: time.Hour*7 + time.Minute*30, //pause 30 --> corrected
},
{
testName: "Bookings10hrs",
bookings: testBookings10hrs, //work 10 pause 0
expectedTime: time.Hour*9 + time.Minute*15, //pause 45 --> corrected
},
{
testName: "Booking 6h with 30 min Break",
bookings: testBookings6hrsBreak30min, //work 6 pause 30
expectedTime: time.Hour * 6, //pause 30 --> bc real pause
},
{
testName: "Booking 6h 10min with 30 min Break",
bookings: testBookings610hrsBreak30min, //work 6 10 pause 30
expectedTime: time.Hour*6 + time.Minute*10, //pause 30 --> real pause
},
{
testName: "Booking 9h with 30 min Break",
bookings: testBookings9hrsBreak30min, //work 9 pause 30
expectedTime: time.Hour * 9, //pause 30 --> real pause
},
{
testName: "Booking 9h 30min",
bookings: testBookings930hrs, //work 9 30 pause 0
expectedTime: time.Hour * 9, //pause 30 --> corrected
},
{
testName: "Booking 9h 40min with 30min Break",
bookings: testBookings910hrsBreak30min, //work 9 10 pause 30
expectedTime: time.Hour*8 + time.Minute*55, //pause 45 --> real + corrected
},
{
testName: "Booking 9h 40min with 35min Break",
bookings: testBookings910hrsBreak35min, //work 9 10 pause 35
expectedTime: time.Hour * 9, //pause 45 --> real + corrected
},
{
testName: "Booking 9h 45min",
bookings: testBookings945hrs, //work 9 45 pause 0
expectedTime: time.Hour * 9, //pause 45 --> corrected
},
{
testName: "Booking 10h Break 45min",
bookings: testBookings10hrsBreak45min, //work 9 15 pause 45
expectedTime: time.Hour*9 + time.Minute*15, //pause 45 --> real
},
{
testName: "Booking 10h 30min Break 45min",
bookings: testBookings1030hrsBreak45min, //work 9 45 pause 45
expectedTime: time.Hour*9 + time.Minute*45, //pause 45 --> real
},
}
for _, tc := range testCases {
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
var testCase = testWorkDay
testCase.Bookings = tc.bookings
workTime := testCase.GetWorktime(testUser, models.WorktimeBaseDay, false)
if workTime != tc.expectedTime {
t.Errorf("GetWorktimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
}
})
}
}
func TestWorkdayWorktimeWeek(t *testing.T) {
testCases := []struct {
testName string
bookings []models.Booking
expectedTime time.Duration
}{
{
testName: "Bookings6hrs",
bookings: testBookings6hrs,
expectedTime: time.Hour * 6,
},
{
testName: "Bookings8hrs",
bookings: testBookings8hrs,
expectedTime: time.Hour*7 + time.Minute*30,
},
{
testName: "Bookings10hrs",
bookings: testBookings10hrs,
expectedTime: time.Hour*9 + time.Minute*15,
},
}
for _, tc := range testCases {
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
var testCase = testWorkDay
testCase.Bookings = tc.bookings
workTime := testCase.GetWorktime(testUser, models.WorktimeBaseWeek, false)
if workTime != tc.expectedTime {
t.Errorf("GetWorktimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
}
})
}
}
func TestWorkdayPausetimeDay(t *testing.T) {
testCases := []struct {
testName string
bookings []models.Booking
expectedTime time.Duration
}{
{
testName: "Bookings6hrs",
bookings: testBookings6hrs,
expectedTime: 0,
},
{
testName: "Bookings8hrs",
bookings: testBookings8hrs,
expectedTime: time.Minute * 30,
},
{
testName: "Bookings10hrs",
bookings: testBookings10hrs,
expectedTime: time.Minute * 45,
},
{
testName: "Booking 6h with 30 min Break",
bookings: testBookings6hrsBreak30min, //work 6 pause 30
expectedTime: time.Minute * 30, //pause 30 --> bc real pause
},
{
testName: "Booking 6h 10min with 30 min Break",
bookings: testBookings610hrsBreak30min, //work 6 10 pause 30
expectedTime: time.Minute * 30, //pause 30 --> real pause
},
{
testName: "Booking 9h with 30 min Break",
bookings: testBookings9hrsBreak30min, //work 9 pause 30
expectedTime: time.Minute * 30, //pause 30 --> real pause
},
{
testName: "Booking 9h 30min",
bookings: testBookings930hrs, //work 9 30 pause 0
expectedTime: time.Minute * 30, //pause 30 --> corrected
},
{
testName: "Booking 9h 40min with 30min Break",
bookings: testBookings910hrsBreak30min, //work 9 10 pause 30
expectedTime: time.Minute * 45, //pause 45 --> real + corrected
},
{
testName: "Booking 9h 40min with 35min Break",
bookings: testBookings910hrsBreak35min, //work 9 10 pause 35
expectedTime: time.Minute * 45, //pause 45 --> real + corrected
},
{
testName: "Booking 9h 45min",
bookings: testBookings945hrs, //work 9 45 pause 0
expectedTime: time.Minute * 45, //pause 45 --> corrected
},
{
testName: "Booking 10h Break 45min",
bookings: testBookings10hrsBreak45min, //work 9 15 pause 45
expectedTime: time.Minute * 45, //pause 45 --> real
},
{
testName: "Booking 10h 30min Break 45min",
bookings: testBookings1030hrsBreak45min, //work 9 45 pause 45
expectedTime: time.Minute * 45, //pause 45 --> real
},
}
for _, tc := range testCases {
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
var testCase = testWorkDay
testCase.Bookings = tc.bookings
workTime := testCase.GetPausetime(testUser, models.WorktimeBaseDay, false)
if workTime != tc.expectedTime {
t.Errorf("GetPausetimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
}
})
}
}
func TestWorkdayPausetimeWeek(t *testing.T) {
testCases := []struct {
testName string
bookings []models.Booking
expectedTime time.Duration
}{
{
testName: "Bookings6hrs",
bookings: testBookings6hrs,
expectedTime: 0,
},
{
testName: "Bookings8hrs",
bookings: testBookings8hrs,
expectedTime: time.Minute * 30,
},
{
testName: "Bookings10hrs",
bookings: testBookings10hrs,
expectedTime: time.Minute * 45,
},
}
for _, tc := range testCases {
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
var testCase = testWorkDay
testCase.Bookings = tc.bookings
workTime := testCase.GetPausetime(testUser, models.WorktimeBaseWeek, false)
if workTime != tc.expectedTime {
t.Errorf("GetPausetimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
}
})
}
}