-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogger_test.go
154 lines (146 loc) · 3.93 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
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
package sq
import (
"bytes"
"context"
"database/sql"
"fmt"
"log"
"testing"
"time"
"github.com/bokwoon95/sq/internal/testutil"
)
func TestLogger(t *testing.T) {
type TT struct {
description string
ctx context.Context
stats QueryStats
config LoggerConfig
wantOutput string
}
assert := func(t *testing.T, tt TT) {
if tt.ctx == nil {
tt.ctx = context.Background()
}
buf := &bytes.Buffer{}
logger := sqLogger{
logger: log.New(buf, "", 0),
config: tt.config,
}
logger.SqLogQuery(tt.ctx, tt.stats)
if diff := testutil.Diff(buf.String(), tt.wantOutput); diff != "" {
t.Error(testutil.Callers(), diff)
}
}
t.Run("Log VerboseLog", func(t *testing.T) {
t.Parallel()
var logSettings LogSettings
Log(nil).SqLogSettings(context.Background(), &logSettings)
diff := testutil.Diff(logSettings, LogSettings{
LogAsynchronously: false,
IncludeTime: true,
IncludeCaller: true,
IncludeResults: 0,
})
if diff != "" {
t.Error(testutil.Callers(), diff)
}
VerboseLog(nil).SqLogSettings(context.Background(), &logSettings)
diff = testutil.Diff(logSettings, LogSettings{
LogAsynchronously: false,
IncludeTime: true,
IncludeCaller: true,
IncludeResults: 5,
})
if diff != "" {
t.Error(testutil.Callers(), diff)
}
})
t.Run("no color", func(t *testing.T) {
var tt TT
tt.config.NoColor = true
tt.stats.Query = "SELECT 1"
tt.wantOutput = "[OK] SELECT 1;\n"
assert(t, tt)
})
tests := []TT{{
description: "err",
stats: QueryStats{
Query: "SELECT 1",
Err: fmt.Errorf("lorem ipsum"),
},
wantOutput: "\x1b[91m[FAIL]\x1b[0m SELECT 1;\x1b[94m err\x1b[0m={lorem ipsum}\n",
}, {
description: "HideArgs",
config: LoggerConfig{HideArgs: true},
stats: QueryStats{
Query: "SELECT ?", Args: []any{1},
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT ?;\n",
}, {
description: "RowCount",
stats: QueryStats{
Query: "SELECT 1",
RowCount: sql.NullInt64{Valid: true, Int64: 3},
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT 1;\x1b[94m rowCount\x1b[0m=3\n",
}, {
description: "RowsAffected",
stats: QueryStats{
Query: "SELECT 1",
RowsAffected: sql.NullInt64{Valid: true, Int64: 5},
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT 1;\x1b[94m rowsAffected\x1b[0m=5\n",
}, {
description: "LastInsertId",
stats: QueryStats{
Query: "SELECT 1",
LastInsertId: sql.NullInt64{Valid: true, Int64: 7},
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT 1;\x1b[94m lastInsertId\x1b[0m=7\n",
}, {
description: "Exists",
stats: QueryStats{
Query: "SELECT EXISTS (SELECT 1)",
Exists: sql.NullBool{Valid: true, Bool: true},
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT EXISTS (SELECT 1);\x1b[94m exists\x1b[0m=true\n",
}, {
description: "ShowCaller",
config: LoggerConfig{ShowCaller: true},
stats: QueryStats{
Query: "SELECT 1",
CallerFile: "file.go",
CallerLine: 22,
CallerFunction: "someFunc",
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT 1;\x1b[94m caller\x1b[0m=file.go:22:someFunc\n",
}, {
description: "Verbose",
config: LoggerConfig{InterpolateVerbose: true, ShowTimeTaken: true},
stats: QueryStats{
Query: "SELECT ?, ?", Args: []any{1, "bob"},
TimeTaken: 300 * time.Millisecond,
},
wantOutput: "\x1b[92m[OK]\x1b[0m\x1b[94m timeTaken\x1b[0m=300ms" +
"\n\x1b[95m----[ Executing query ]----\x1b[0m" +
"\nSELECT ?, ?; []interface {}{1, \"bob\"}" +
"\n\x1b[95m----[ with bind values ]----\x1b[0m" +
"\nSELECT 1, 'bob';\n",
}, {
description: "ShowResults",
config: LoggerConfig{ShowResults: 1},
stats: QueryStats{
Query: "SELECT 1",
Results: "\nlorem ipsum dolor sit amet",
},
wantOutput: "\x1b[92m[OK]\x1b[0m SELECT 1;" +
"\n\x1b[95m----[ Fetched result ]----\x1b[0m" +
"\nlorem ipsum dolor sit amet\n",
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
assert(t, tt)
})
}
}