fix: handle the zero cases for oidc.Time

This commit is contained in:
Tim Möhlmann 2023-03-20 14:56:06 +02:00 committed by Tim Möhlmann
parent 890a7f3ed4
commit 115813ee38
2 changed files with 57 additions and 0 deletions

View file

@ -173,10 +173,16 @@ func NewEncoder() *schema.Encoder {
type Time int64
func (ts Time) AsTime() time.Time {
if ts == 0 {
return time.Time{}
}
return time.Unix(int64(ts), 0)
}
func FromTime(tt time.Time) Time {
if tt.IsZero() {
return 0
}
return Time(tt.Unix())
}

View file

@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"testing"
"time"
"github.com/gorilla/schema"
"github.com/stretchr/testify/assert"
@ -467,6 +468,56 @@ func TestNewEncoder(t *testing.T) {
assert.Equal(t, a, b)
}
func TestTime_AsTime(t *testing.T) {
tests := []struct {
name string
ts Time
want time.Time
}{
{
name: "unset",
ts: 0,
want: time.Time{},
},
{
name: "set",
ts: 1,
want: time.Unix(1, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.ts.AsTime()
assert.Equal(t, tt.want, got)
})
}
}
func TestTime_FromTime(t *testing.T) {
tests := []struct {
name string
tt time.Time
want Time
}{
{
name: "zero",
tt: time.Time{},
want: 0,
},
{
name: "set",
tt: time.Unix(1, 0),
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FromTime(tt.tt)
assert.Equal(t, tt.want, got)
})
}
}
func TestTime_UnmarshalJSON(t *testing.T) {
type dst struct {
UpdatedAt Time `json:"updated_at"`