github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/duration_serialization_test.go (about) 1 package ravendb 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestCanSerializeDuration(t *testing.T) { 11 tests := []struct { 12 d time.Duration 13 exp string 14 }{ 15 {time.Hour*24*5 + time.Hour*2, `"5.02:00:00"`}, 16 {time.Millisecond * 5, `"00:00:00.0050000"`}, 17 } 18 for _, test := range tests { 19 d2 := Duration(test.d) 20 d, err := jsonMarshal(d2) 21 assert.NoError(t, err) 22 got := string(d) 23 assert.Equal(t, test.exp, got) 24 } 25 } 26 27 func TestCanDeserializeDuration(t *testing.T) { 28 tests := []struct { 29 s string 30 exp time.Duration 31 }{ 32 {`"5.02:00:00"`, time.Hour*24*5 + time.Hour*2}, 33 {`"00:00:00.0050000"`, time.Millisecond * 5}, 34 {`"00:00:00.005000"`, time.Millisecond * 5}, 35 {`"00:00:00.00500"`, time.Millisecond * 5}, 36 {`"00:00:00.1"`, time.Millisecond * 100}, 37 } 38 for _, test := range tests { 39 var got Duration 40 d := []byte(test.s) 41 err := jsonUnmarshal(d, &got) 42 assert.NoError(t, err) 43 exp := Duration(test.exp) 44 assert.Equal(t, exp, got) 45 } 46 }