Sybren A. Stüvel ddced5a823 Transition from ex-GORM structs to sqlc structs (4/5)
Replace old used-to-be-GORM datastructures (#104305) with sqlc-generated
structs. This also makes it possible to use more specific structs that
are more taylored to the specific queries, increasing efficiency.

This commit deals with the worker sleep schedule.

Functional changes are kept to a minimum, as the API still serves the
same data.

Because this work covers so much of Flamenco's code, it's been split up
into different commits. Each commit brings Flamenco to a state where it
compiles and unit tests pass. Only the result of the final commit has
actually been tested properly.

Ref: #104343
2024-12-04 14:00:19 +01:00

119 lines
4.9 KiB
Go

package sleep_scheduler
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"testing"
"github.com/stretchr/testify/assert"
"projects.blender.org/studio/flamenco/internal/manager/persistence"
"projects.blender.org/studio/flamenco/pkg/api"
"projects.blender.org/studio/flamenco/pkg/time_of_day"
)
func TestCalculateNextCheck(t *testing.T) {
_, mocks, _ := testFixtures(t)
var sched persistence.SleepSchedule
empty := time_of_day.Empty()
// Below, S, N, and E respectively mean Start, Now, and End times.
// Their order shows their relation to "Now". Lower-case letters mean "no value".
// Note that N can never be before 's' or after 'e'.
// S N E -> E
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0)}
assert.Equal(t, mocks.todayAt(18, 0), calculateNextCheck(mocks.todayAt(11, 16), sched))
// S E N -> end of day
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0)}
assert.Equal(t, mocks.endOfDay(), calculateNextCheck(mocks.todayAt(19, 16), sched))
// N S E -> S
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0)}
assert.Equal(t, mocks.todayAt(9, 0), calculateNextCheck(mocks.todayAt(8, 47), sched))
// s N e -> end of day
sched = persistence.SleepSchedule{StartTime: empty, EndTime: empty}
assert.Equal(t, mocks.endOfDay(), calculateNextCheck(mocks.todayAt(7, 47), sched))
// S N e -> end of day
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: empty}
assert.Equal(t, mocks.endOfDay(), calculateNextCheck(mocks.todayAt(10, 47), sched))
// s N E -> E
sched = persistence.SleepSchedule{StartTime: empty, EndTime: time_of_day.New(18, 0)}
assert.Equal(t, mocks.todayAt(18, 0), calculateNextCheck(mocks.todayAt(7, 47), sched))
}
func TestScheduledWorkerStatus(t *testing.T) {
_, mocks, _ := testFixtures(t)
var sched persistence.SleepSchedule
empty := time_of_day.Empty()
// No schedule means 'awake'.
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(11, 16), nil))
// Below, S, N, and E respectively mean Start, Now, and End times.
// Their order shows their relation to "Now". Lower-case letters mean "no value".
// Note that N can never be before 's' or after 'e'.
// Test time logic without any DaysOfWeek set, i.e. the scheduled times apply
// to each day.
// S N E -> asleep
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0), IsActive: true}
assert.Equal(t, api.WorkerStatusAsleep, scheduledWorkerStatus(mocks.todayAt(11, 16), &sched))
// S E N -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(19, 16), &sched))
// N S E -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(8, 47), &sched))
// s N e -> asleep
sched = persistence.SleepSchedule{StartTime: empty, EndTime: empty, IsActive: true}
assert.Equal(t, api.WorkerStatusAsleep, scheduledWorkerStatus(mocks.todayAt(7, 47), &sched))
// S N e -> asleep
sched = persistence.SleepSchedule{StartTime: time_of_day.New(9, 0), EndTime: empty, IsActive: true}
assert.Equal(t, api.WorkerStatusAsleep, scheduledWorkerStatus(mocks.todayAt(10, 47), &sched))
// s N E -> asleep
sched = persistence.SleepSchedule{StartTime: empty, EndTime: time_of_day.New(18, 0), IsActive: true}
assert.Equal(t, api.WorkerStatusAsleep, scheduledWorkerStatus(mocks.todayAt(7, 47), &sched))
// Test DaysOfWeek logic, but only with explicit start & end times. The logic
// for missing start/end is already covered above.
// The mocked "today" is a Tuesday.
// S N E unmentioned day -> awake
sched = persistence.SleepSchedule{DaysOfWeek: "mo we", StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0), IsActive: true}
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(11, 16), &sched))
// S E N unmentioned day -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(19, 16), &sched))
// N S E unmentioned day -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(8, 47), &sched))
// S N E mentioned day -> asleep
sched = persistence.SleepSchedule{DaysOfWeek: "tu th fr", StartTime: time_of_day.New(9, 0), EndTime: time_of_day.New(18, 0), IsActive: true}
assert.Equal(t, api.WorkerStatusAsleep, scheduledWorkerStatus(mocks.todayAt(11, 16), &sched))
// S E N mentioned day -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(19, 16), &sched))
// N S E mentioned day -> awake
assert.Equal(t, api.WorkerStatusAwake, scheduledWorkerStatus(mocks.todayAt(8, 47), &sched))
}
func TestCleanupDaysOfWeek(t *testing.T) {
assert.Equal(t, "", cleanupDaysOfWeek(""))
assert.Equal(t, "mo tu we", cleanupDaysOfWeek("mo tu we"))
assert.Equal(t, "mo tu we", cleanupDaysOfWeek(" mo tu we \n"))
assert.Equal(t, "mo tu we", cleanupDaysOfWeek("monday tuesday wed"))
}