-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_test.go
185 lines (132 loc) · 5.53 KB
/
log_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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package logwrap
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
func TestLogger_Log(t *testing.T) {
t.Run("log sends a message to the implementation with a message and default level of info", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedMessage := "message"
expectedLevel := Info
logger := New(mockImpl.Impl)
logger.Log(context.Background(), expectedMessage)
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
assert.Equal(t, expectedMessage, capturedMessage.Message)
})
t.Run("log sends a message to the implementation with an increasing sequence number", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Twice()
logger := New(mockImpl.Impl)
logger.Log(context.Background(), "anything")
logger.Log(context.Background(), "anything")
assert.True(t, mockImpl.AssertExpectations(t))
var capturedMessage [2]Message
capturedMessage[0] = mockImpl.Calls[0].Arguments.Get(1).(Message)
capturedMessage[1] = mockImpl.Calls[1].Arguments.Get(1).(Message)
assert.Equal(t, uint64(1), capturedMessage[0].Sequence)
assert.Equal(t, uint64(2), capturedMessage[1].Sequence)
})
t.Run("log sends a message to the implementation with an inserted timestamp", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
beforeTime := time.Now()
logger := New(mockImpl.Impl)
logger.Log(context.Background(), "anything")
afterTime := time.Now()
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.True(t, beforeTime.Before(capturedMessage.Timestamp) || beforeTime.Equal(capturedMessage.Timestamp))
assert.True(t, afterTime.After(capturedMessage.Timestamp) || beforeTime.Equal(capturedMessage.Timestamp))
})
}
func TestLogger_LogPanic(t *testing.T) {
t.Run("log sends a message to the implementation with level of panic", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Panic
logger := New(mockImpl.Impl)
logger.LogPanic(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogFatal(t *testing.T) {
t.Run("log sends a message to the implementation with level of fatal", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Fatal
logger := New(mockImpl.Impl)
logger.LogFatal(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogError(t *testing.T) {
t.Run("log sends a message to the implementation with level of error", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Error
logger := New(mockImpl.Impl)
logger.LogError(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogWarn(t *testing.T) {
t.Run("log sends a message to the implementation with level of warn", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Warn
logger := New(mockImpl.Impl)
logger.LogWarn(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogInfo(t *testing.T) {
t.Run("log sends a message to the implementation with level of info", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Info
logger := New(mockImpl.Impl)
ctx := logger.AddOptionsToContext(context.Background(), Level(Fatal))
logger.LogInfo(ctx, "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogDebug(t *testing.T) {
t.Run("log sends a message to the implementation with level of debug", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Debug
logger := New(mockImpl.Impl)
logger.LogDebug(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}
func TestLogger_LogTrace(t *testing.T) {
t.Run("log sends a message to the implementation with level of trace", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedLevel := Trace
logger := New(mockImpl.Impl)
logger.LogTrace(context.Background(), "message")
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevel, capturedMessage.Level)
})
}