Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests for go/protoutil/duration #14965

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions go/protoutil/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

func TestDurationFromProto(t *testing.T) {
t.Parallel()

tests := []struct {
name string
Expand Down Expand Up @@ -59,13 +58,32 @@ func TestDurationFromProto(t *testing.T) {
isOk: true,
shouldErr: true,
},
{
name: "nanoseconds",
in: &vttime.Duration{
Seconds: 1,
Nanos: 500000000,
},
expected: time.Second + 500*time.Millisecond,
isOk: true,
shouldErr: false,
},
{
name: "out of range nanoseconds",
in: &vttime.Duration{
Seconds: -1,
Nanos: 500000000,
},
expected: 0,
isOk: true,
shouldErr: true,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual, ok, err := DurationFromProto(tt.in)
if tt.shouldErr {
Expand All @@ -80,3 +98,38 @@ func TestDurationFromProto(t *testing.T) {
})
}
}

func TestDurationToProto(t *testing.T) {

tests := []struct {
name string
in time.Duration
expected *vttime.Duration
}{
{
name: "success",
in: time.Second * 1000,
expected: &vttime.Duration{Seconds: 1000},
},
{
name: "zero duration",
in: 0,
expected: &vttime.Duration{},
},
{
name: "nanoseconds",
in: time.Second + 500*time.Millisecond,
expected: &vttime.Duration{Seconds: 1, Nanos: 500000000},
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {

actual := DurationToProto(tt.in)
assert.Equal(t, tt.expected, actual)
})
}
}
Loading