-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_logger_test.go
More file actions
95 lines (93 loc) · 2.5 KB
/
step_logger_test.go
File metadata and controls
95 lines (93 loc) · 2.5 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package sqlslog
import (
"log/slog"
"testing"
"time"
)
func TestStepLoggerDurationAttr(t *testing.T) {
t.Parallel()
key := "duration"
testcases := []struct {
value time.Duration
durationType DurationType
expected func(t *testing.T, attr slog.Attr)
}{
{
value: time.Duration(1),
durationType: DurationNanoSeconds,
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if v, ok := attr.Value.Any().(int64); !ok {
t.Errorf("expected: %T, but got %T", int64(0), v)
} else if v != 1 {
t.Errorf("expected: %d, but got %d", 1, v)
}
},
},
{
value: time.Duration(2_000),
durationType: DurationMicroSeconds,
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if v, ok := attr.Value.Any().(int64); !ok {
t.Errorf("expected: %T, but got %T", int64(0), v)
} else if v != 2 {
t.Errorf("expected: %d, but got %d", 2, v)
}
},
},
{
value: time.Duration(3_000_000),
durationType: DurationMilliSeconds,
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if v, ok := attr.Value.Any().(int64); !ok {
t.Errorf("expected: %T, but got %T", int64(0), v)
} else if v != 3 {
t.Errorf("expected: %d, but got %d", 3, v)
}
},
},
{
value: time.Duration(4_000_000_000),
durationType: DurationGoDuration,
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if attr.Value.Duration() != time.Duration(4_000_000_000) {
t.Errorf("expected: %d, but got %d", 4_000_000_000, attr.Value.Duration())
}
},
},
{
value: time.Duration(567_000_000),
durationType: DurationString,
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if v, ok := attr.Value.Any().(string); !ok {
t.Errorf("expected: %T, but got %T", "", v)
} else if v != "567ms" {
t.Errorf("expected: %s, but got %s", "567ms", v)
}
},
},
{
value: time.Duration(890_000),
durationType: DurationType(-1),
expected: func(t *testing.T, attr slog.Attr) {
t.Helper()
if v, ok := attr.Value.Any().(int64); !ok {
t.Errorf("expected: %T, but got %T", int64(0), v)
} else if v != 890_000 {
t.Errorf("expected: %d, but got %d", 890_000, v)
}
},
},
}
for _, tc := range testcases {
t.Run(tc.value.String(), func(t *testing.T) {
t.Parallel()
attr := newStepLogger(slog.Default(), stepLoggerOptions{durationKey: key, durationType: tc.durationType}).durationAttr(tc.value)
tc.expected(t, attr)
})
}
}