-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_more_test.go
More file actions
73 lines (65 loc) · 1.95 KB
/
Copy pathlogger_more_test.go
File metadata and controls
73 lines (65 loc) · 1.95 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
package norm
import (
"bytes"
"log"
"strings"
"testing"
"time"
)
func TestStdLoggerAndFormatHelpers(t *testing.T) {
oldWriter := log.Writer()
oldFlags := log.Flags()
defer log.SetOutput(oldWriter)
defer log.SetFlags(oldFlags)
var buf bytes.Buffer
log.SetOutput(&buf)
log.SetFlags(0)
StdLogger{}.Debug("query", Field{Key: "id", Value: 7}, Field{Key: "name", Value: "alice"})
out := strings.TrimSpace(buf.String())
if !strings.Contains(out, "[DEBUG] query id=7 name=alice") {
t.Fatalf("unexpected log output: %q", out)
}
buf.Reset()
StdLogger{}.Info("ignored", Field{Key: "stmt", Value: "SELECT 1;"})
if got := strings.TrimSpace(buf.String()); got != "SELECT 1;" {
t.Fatalf("stmt shortcut mismatch: %q", got)
}
if got := formatFields([]Field{{Key: "n", Value: 3}, {Key: "s", Value: "x"}}); got != "n=3 s=x" {
t.Fatalf("formatFields=%q", got)
}
if got := formatFields(nil); got != "" {
t.Fatalf("expected empty fields, got %q", got)
}
}
func TestInlineSQLAndSQLLiteral(t *testing.T) {
ts := time.Unix(1700000000, 0).UTC()
got := inlineSQL("INSERT INTO t VALUES ($1, $2, $3, $4, $5, $6)", []any{"O'Reilly", []byte{0xAB, 0xCD}, true, nil, ts, 7})
checks := []string{
"'O''Reilly'",
"decode('ABCD','hex')",
"TRUE",
"NULL",
ts.Format(time.RFC3339Nano),
"7",
}
for _, check := range checks {
if !strings.Contains(got, check) {
t.Fatalf("inlineSQL missing %q in %q", check, got)
}
}
if !strings.HasSuffix(got, ";") {
t.Fatalf("inlineSQL should end with semicolon: %q", got)
}
if got := inlineSQL("SELECT 1", nil); got != "SELECT 1;" {
t.Fatalf("inlineSQL no-args mismatch: %q", got)
}
if got := sqlLiteral(false); got != "FALSE" {
t.Fatalf("sqlLiteral false mismatch: %q", got)
}
if got := sqlLiteral(struct{ Name string }{Name: "bob"}); !strings.Contains(got, "bob") {
t.Fatalf("sqlLiteral struct mismatch: %q", got)
}
if got := escapeSQLString("a'b"); got != "a''b" {
t.Fatalf("escapeSQLString mismatch: %q", got)
}
}