-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
75 lines (68 loc) · 2.29 KB
/
logger_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
package tracer
import (
"github.com/icrowley/fake"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type mockLogWriter struct {
mock.Mock
}
func (mw mockLogWriter) Write(entry Entry) {
mw.Called(entry.Message, entry.TransactionId, entry.Level, entry.Args)
}
func TestLogger_Alert(t *testing.T) {
t.Parallel()
t.Run("but is not on trace mode", func(t *testing.T) {
t.Parallel()
is := assert.New(t)
expected := Entry{
Message: "this is a test",
TransactionId: "",
Level: Alert,
Args: []interface{}{"some-field"},
}
lw1 := mockLogWriter{}
lw1.On("Write", expected.Message, expected.TransactionId, expected.Level, expected.Args).Return().Once()
lw2 := mockLogWriter{}
lw2.On("Write", expected.Message, expected.TransactionId, expected.Level, expected.Args).Return().Once()
subject := DefaultContext.GetLogger(fake.Brand()).(*logger)
subject.RegisterWriter(lw1)
subject.RegisterWriter(lw2)
is.NotPanics(func() {
subject.Alert("this is a test", "some-field")
}, "it should not panics")
lw1.AssertExpectations(t)
lw2.AssertExpectations(t)
})
t.Run("and is on trace mode", func(t *testing.T) {
t.Parallel()
is := assert.New(t)
expected := Entry{
Message: "this is a test",
TransactionId: "some-id",
Level: Alert,
Args: []interface{}{"some-field"},
}
lw1 := mockLogWriter{}
lw1.On("Write", expected.Message, expected.TransactionId, expected.Level, expected.Args).Return().Once()
lw2 := mockLogWriter{}
lw2.On("Write", expected.Message, expected.TransactionId, expected.Level, expected.Args).Return().Once()
subject := DefaultContext.GetLogger(fake.Brand()).(*logger).Trace("some-id")
subject.RegisterWriter(lw1)
subject.RegisterWriter(lw2)
is.NotPanics(func() {
subject.Alert("this is a test", "some-field")
}, "it should not panics")
lw1.AssertExpectations(t)
lw2.AssertExpectations(t)
})
}
func TestLogger_Trace(t *testing.T) {
t.Parallel()
is := assert.New(t)
subject := &logger{}
newLogger := subject.Trace("some-id")
is.NotEqual(subject.transactionId, (newLogger.(*logger)).transactionId, "it should not have the same transaction id as the original logger")
is.Equal("some-id", (newLogger.(*logger)).transactionId, "it should set the transaction id")
}