github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/binding/decoders/null_time_test.go (about) 1 package decoders 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gobuffalo/nulls" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func Test_NullTimeCustomDecoder_Decode(t *testing.T) { 12 r := require.New(t) 13 14 testCases := []struct { 15 input string 16 expected time.Time 17 expectErr bool 18 }{ 19 { 20 input: "2017-01-01", 21 expected: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC), 22 }, 23 { 24 input: "2018-07-13T15:34", 25 expected: time.Date(2018, time.July, 13, 15, 34, 0, 0, time.UTC), 26 }, 27 { 28 input: "2018-20-10T30:15", 29 expected: time.Time{}, 30 }, 31 { 32 input: "", 33 expected: time.Time{}, 34 }, 35 } 36 37 for _, testCase := range testCases { 38 39 tt, err := NullTimeDecoderFn()([]string{testCase.input}) 40 r.IsType(tt, nulls.Time{}) 41 nt := tt.(nulls.Time) 42 43 if testCase.expectErr { 44 r.Error(err) 45 r.Equal(nt.Valid, false) 46 continue 47 } 48 49 r.Equal(testCase.expected, nt.Time) 50 } 51 }