-
Notifications
You must be signed in to change notification settings - Fork 0
/
zapmixin_test.go
108 lines (85 loc) · 2.25 KB
/
zapmixin_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
package zapmixin
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/fox-one/mixin-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var conversations = []string{"06602963-d86d-3df3-ac06-000000000000"}
type Suite struct {
suite.Suite
client *mixin.Client
conversations []string
}
type MockedMixinClient struct {
mock.Mock
msgs []*mixin.MessageRequest
}
func (m *MockedMixinClient) SendMessage(ctx context.Context, message *mixin.MessageRequest) error {
args := m.Called(ctx, message)
m.msgs = append(m.msgs, message)
return args.Error(0)
}
func TestLevels(t *testing.T) {
client := new(MockedMixinClient)
client.On("SendMessage", mock.Anything, mock.Anything).Return(nil)
{
h, _ := newHandler(client, conversations, WithSync())
logger := getLogger().WithOptions(zap.Hooks(h.Hook()))
// default level: warn
logger.Info("1")
logger.Warn("2")
logger.Error("3")
assert.Equal(t, 2, len(client.msgs))
}
{
client.msgs = []*mixin.MessageRequest{}
h, _ := newHandler(client, conversations, WithSync(), WithThresholdLevel(zapcore.ErrorLevel))
logger := getLogger().WithOptions(zap.Hooks(h.Hook()))
logger.Info("1")
logger.Warn("2")
logger.Error("3")
assert.Equal(t, 1, len(client.msgs))
}
}
func TestSuite(t *testing.T) {
keyPath := os.Getenv("ZAPMIXIN_KEY_PATH")
if keyPath == "" {
t.Skip()
}
data, err := ioutil.ReadFile(keyPath)
require.NoError(t, err)
var store mixin.Keystore
require.NoError(t, json.Unmarshal(data, &store))
client, err := mixin.NewFromKeystore(&store)
require.NoError(t, err)
suite.Run(t, &Suite{
client: client,
conversations: strings.Split(os.Getenv("ZAPMIXIN_CONVERSATIONS"), ","),
})
}
func (s *Suite) TestBasic() {
h, err := New(s.client, s.conversations, WithSync())
s.NoError(err)
logger := getLogger().WithOptions(zap.Hooks(h.Hook()))
logger.Warn("test event")
}
func getLogger() *zap.Logger {
cfg := zap.NewProductionConfig()
core := zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zapcore.AddSync(io.Discard),
zapcore.InfoLevel,
)
return zap.New(core)
}