-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorm_test.go
More file actions
158 lines (146 loc) · 4.22 KB
/
gorm_test.go
File metadata and controls
158 lines (146 loc) · 4.22 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
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
package main
import (
"context"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)
func TestNewLogLevel(t *testing.T) {
t.Run("POSITIVE-InfoLevel", func(t *testing.T) {
l := NewLogLevel("info")
assert.Equal(t, zerolog.InfoLevel, l.Level)
})
t.Run("POSITIVE-WarnLevel", func(t *testing.T) {
l := NewLogLevel("warn")
assert.Equal(t, zerolog.WarnLevel, l.Level)
})
t.Run("POSITIVE-DebugLevel", func(t *testing.T) {
l := NewLogLevel("debug")
assert.Equal(t, zerolog.DebugLevel, l.Level)
})
t.Run("POSITIVE-ErrorLevel", func(t *testing.T) {
l := NewLogLevel("error")
assert.Equal(t, zerolog.ErrorLevel, l.Level)
})
t.Run("POSITIVE-DefaultLevel", func(t *testing.T) {
l := NewLogLevel("unknown")
assert.Equal(t, zerolog.Disabled, l.Level)
})
}
func TestLogger_logWithCtx(t *testing.T) {
t.Run("POSITIVE-WithZerologContext", func(t *testing.T) {
l := NewLogLevel("info")
ctx := log.With().Logger().WithContext(context.Background())
evt := l.logWithCtx(ctx, zerolog.InfoLevel)
assert.NotNil(t, evt)
})
t.Run("POSITIVE-WithoutContext", func(t *testing.T) {
l := NewLogLevel("info")
evt := l.logWithCtx(nil, zerolog.InfoLevel)
assert.NotNil(t, evt)
})
t.Run("POSITIVE-CtxBackground_ReturnNil", func(t *testing.T) {
l := NewLogLevel("info")
ctx := context.Background()
evt := l.logWithCtx(ctx, zerolog.InfoLevel)
assert.Nil(t, evt)
})
}
func TestLogger_safeLogMsgf(t *testing.T) {
t.Run("POSITIVE-WithZerologContext", func(t *testing.T) {
l := NewLogLevel("info")
ctx := log.With().Logger().WithContext(context.Background())
l.logMsgf(ctx, "test")
})
t.Run("POSITIVE-WithoutContext", func(t *testing.T) {
l := NewLogLevel("info")
l.logMsgf(nil, "test")
})
t.Run("POSITIVE-CtxBackground_NoError", func(t *testing.T) {
l := NewLogLevel("info")
ctx := context.Background()
l.logMsgf(ctx, "test")
})
}
func TestLogger_Trace(t *testing.T) {
t.Run("POSITIVE-TraceNoError", func(t *testing.T) {
l := NewLogLevel("info")
begin := time.Now()
f := func() (string, int64) { return "SELECT 1", 1 }
l.Trace(context.Background(), begin, f, nil)
// no panic, no assert needed
})
t.Run("POSITIVE-TraceWithError", func(t *testing.T) {
l := NewLogLevel("info")
begin := time.Now()
f := func() (string, int64) { return "SELECT 1", 1 }
err := context.DeadlineExceeded
l.Trace(context.Background(), begin, f, err)
// no panic, no assert needed
})
}
func TestLogger_LogMode(t *testing.T) {
l := NewLogLevel("info")
ret := l.LogMode(0)
assert.Equal(t, l, ret)
}
func TestLogger_Error(t *testing.T) {
t.Run("POSITIVE-WithZerologContext", func(t *testing.T) {
l := NewLogLevel("info")
ctx := log.With().Logger().WithContext(context.Background())
l.Error(ctx, "error message")
})
t.Run("POSITIVE-WithNilContext", func(t *testing.T) {
l := NewLogLevel("info")
l.Error(nil, "error message")
})
t.Run("POSITIVE-WithBackgroundContext", func(t *testing.T) {
l := NewLogLevel("info")
l.Error(context.Background(), "error message")
})
}
func TestLogger_Warn(t *testing.T) {
l := NewLogLevel("warn")
t.Run("POSITIVE-WithZerologContext", func(t *testing.T) {
ctx := log.With().Logger().WithContext(context.Background())
l.Warn(ctx, "warn message")
})
t.Run("POSITIVE-WithNilContext", func(t *testing.T) {
l.Warn(nil, "warn message")
})
t.Run("POSITIVE-WithBackgroundContext", func(t *testing.T) {
l.Warn(context.Background(), "warn message")
})
}
func TestLogger_Info(t *testing.T) {
t.Run("POSITIVE-WithZerologContext", func(t *testing.T) {
l := NewLogLevel("info")
ctx := log.With().Logger().WithContext(context.Background())
l.Info(ctx, "info message")
})
t.Run("POSITIVE-WithNilContext", func(t *testing.T) {
l := NewLogLevel("info")
l.Info(nil, "info message")
})
t.Run("POSITIVE-WithBackgroundContext", func(t *testing.T) {
l := NewLogLevel("info")
l.Info(context.Background(), "info message")
})
}
func TestNewGORM(t *testing.T) {
t.Run("NEGATIVE-InvalidDataSourceName", func(t *testing.T) {
cfg := &Config{
Host: "invalid",
Port: 1,
User: "invalid",
Password: "invalid",
Name: "invalid",
LogLevel: "info",
}
db, err := NewGORM(cfg)
assert.Error(t, err)
assert.Nil(t, db)
})
}