-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
57 lines (52 loc) · 1.16 KB
/
types_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package lambdaext_test
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
lambdaext "github.com/zakharovvi/aws-lambda-extensions"
)
func TestDurationMs_UnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
want lambdaext.DurationMs
json []byte
wantErr bool
}{
{
"float",
lambdaext.DurationMs(90100 * time.Microsecond),
[]byte("90.1"),
false,
},
{
"int",
lambdaext.DurationMs(694 * time.Millisecond),
[]byte("694"),
false,
},
{
"unsupported",
lambdaext.DurationMs(0),
[]byte(`"10s"`),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := lambdaext.DurationMs(0)
if err := json.Unmarshal(tt.json, &got); (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
require.Equal(t, tt.want, got)
})
}
}
func TestDurationMs_MarshalJSON(t *testing.T) {
d := lambdaext.DurationMs(1*time.Hour + 2*time.Minute + 23*time.Second + 387*time.Millisecond)
got, err := json.Marshal(d)
require.NoError(t, err)
require.Equal(t, `"1h2m23.387s"`, string(got))
}