-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
79 lines (57 loc) · 2.41 KB
/
context_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
package logwrap
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
func TestLogger_AddOptionsToContext(t *testing.T) {
t.Run("adds an option to the context which is used in following logs", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedMessage := "message"
expectedLevel := Fatal
logger := New(mockImpl.Impl)
ctx := logger.AddOptionsToContext(context.Background(), Level(Fatal))
logger.Log(ctx, 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("multiple descendant contexts correctly build upon options", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Once()
expectedMessage := "message"
expectedLevel := Fatal
expectedKey := "key"
expectedValue := "value"
logger := New(mockImpl.Impl)
ctx := logger.AddOptionsToContext(context.Background(), Level(Fatal))
cctx := logger.AddOptionsToContext(ctx, Datum(expectedKey, expectedValue))
logger.Log(cctx, 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)
assert.Equal(t, expectedValue, capturedMessage.Data[expectedKey])
})
t.Run("two separate loggers do not conflict within the same context", func(t *testing.T) {
mockImpl := MockImpl{}
mockImpl.On("Impl", mock.Anything, mock.Anything).Twice()
expectedMessage := "message"
expectedLevelOne := Fatal
expectedLevelTwo := Fatal
logger := New(mockImpl.Impl)
loggerTwo := New(mockImpl.Impl)
ctx := loggerTwo.AddOptionsToContext(context.Background(), Level(expectedLevelTwo))
cctx := logger.AddOptionsToContext(ctx, Level(expectedLevelOne))
logger.Log(cctx, expectedMessage)
loggerTwo.Log(cctx, expectedMessage)
assert.True(t, mockImpl.AssertExpectations(t))
capturedMessage := mockImpl.Calls[0].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevelOne, capturedMessage.Level)
capturedMessageTwo := mockImpl.Calls[1].Arguments.Get(1).(Message)
assert.Equal(t, expectedLevelTwo, capturedMessageTwo.Level)
})
}