Select Git revision
Monster.Data.Transformer.html
schedule-interval_test.go 1.68 KiB
// Copyright 2023 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package jobqueue
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
"time"
)
// TestUnmarshalYAML tests the unmarshalling of the SchedulerPersistence struct
func TestUnmarshalYAML(t *testing.T) {
yamlStr := `
time: "2023-12-06T15:04:05Z"
interval: "1h30m"
`
sp := SchedulerPersistence{}
err := yaml.Unmarshal([]byte(yamlStr), &sp)
assert.NoError(t, err)
expectedTime, _ := time.Parse(time.RFC3339, "2023-12-06T15:04:05Z")
assert.Equal(t, expectedTime, *sp.Time, "the time should be parsed correctly")
expectedInterval, _ := time.ParseDuration("1h30m")
assert.Equal(t, expectedInterval, sp.Interval, "the interval should be parsed correctly")
}
// TestUnmarshalJSON tests the UnmarshalJSON method
func TestUnmarshalJSON(t *testing.T) {
// Example JSON string representing SchedulerPersistence
jsonStr := `{
"time": "2023-12-06T15:04:05Z",
"interval": "1h30m"
}`
// Create an instance of SchedulerPersistence
sp := SchedulerPersistence{}
// Unmarshal the JSON string into the sp structure
err := json.Unmarshal([]byte(jsonStr), &sp)
assert.NoError(t, err, "Unmarshalling should not produce an error")
// Verify that the Time field is correctly parsed
expectedTime, _ := time.Parse(time.RFC3339, "2023-12-06T15:04:05Z")
if sp.Time != nil {
assert.Equal(t, expectedTime, *sp.Time, "Time should be correctly parsed")
} else {
t.Error("Time field is nil, but it should not be")
}
// Verify that the Delay field is correctly parsed
expectedInterval, _ := time.ParseDuration("1h30m")
assert.Equal(t, expectedInterval, sp.Interval, "Interval should be correctly parsed")
}