Skip to content

Commit

Permalink
PC-15140 Add at least second time resolution for firstEventStart fiel…
Browse files Browse the repository at this point in the history
…d validation
  • Loading branch information
kubaceg committed Dec 11, 2024
1 parent d4b7802 commit 744f1c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion manifest/v1alpha/budgetadjustment/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var specValidation = govy.New[Spec](
Rules(validationV1Alpha.StringDescription()),
govy.For(func(s Spec) time.Time { return s.FirstEventStart }).
WithName("firstEventStart").
Required(),
Required().
Rules(atLeastSecondTimeResolution),
govy.Transform(func(s Spec) string { return s.Duration }, time.ParseDuration).
WithName("duration").
Required().
Expand Down Expand Up @@ -98,3 +99,11 @@ var atLeastHourlyFreq = govy.NewRule(func(rule *rrule.RRule) error {

return nil
})

var atLeastSecondTimeResolution = govy.NewRule(func(t time.Time) error {
if t.Nanosecond() != 0 {
return errors.New("time must be defined with second resolution")
}

return nil
})
40 changes: 40 additions & 0 deletions manifest/v1alpha/budgetadjustment/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,43 @@ func TestAtLeastHourlyFreq(t *testing.T) {
})
}
}

func TestAtLeastSecondTimeResolution(t *testing.T) {
tests := []struct {
name string
time time.Time
expectedError string
}{
{
name: "time with nanosecond returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 1, time.UTC),
expectedError: "time must be defined with second resolution",
},
{
name: "time with second resolution returns no error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedError: "",
},
{
name: "time with millisecond resolution returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 1000000, time.UTC),
expectedError: "time must be defined with second resolution",
},
{
name: "time with microsecond resolution returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 526, time.UTC),
expectedError: "time must be defined with second resolution",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := atLeastSecondTimeResolution.Validate(tt.time)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedError)
}
})
}
}

0 comments on commit 744f1c1

Please sign in to comment.