-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorm_logging_test.go
More file actions
53 lines (46 loc) · 1.3 KB
/
norm_logging_test.go
File metadata and controls
53 lines (46 loc) · 1.3 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
package norm
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func TestMakeLogFieldsAndPoolHelpers(t *testing.T) {
pool := &pgxpool.Pool{}
readPool := &pgxpool.Pool{}
kn := &KintsNorm{
pool: pool,
readPool: readPool,
logContextFields: func(context.Context) []Field {
return []Field{{Key: "req_id", Value: "r1"}}
},
}
fields := kn.makeLogFields(context.Background(), "SELECT $1", []any{"x"})
if len(fields) != 4 {
t.Fatalf("unexpected field count: %d", len(fields))
}
if fields[0].Key != "req_id" || fields[1].Key != "sql" || fields[2].Key != "args" || fields[3].Key != "stmt" {
t.Fatalf("unexpected fields: %#v", fields)
}
kn.maskParams = true
fields = kn.makeLogFields(context.Background(), "SELECT $1", []any{"x"})
if len(fields) != 3 || fields[2].Value != "[masked]" {
t.Fatalf("masked fields mismatch: %#v", fields)
}
if kn.Pool() != pool {
t.Fatalf("pool accessor mismatch")
}
if kn.ReadPool() != readPool {
t.Fatalf("read pool accessor mismatch")
}
if qb := kn.QueryRead(); qb == nil || qb.exec == nil {
t.Fatalf("query read builder not initialized")
}
kn2 := &KintsNorm{}
if err := kn2.Close(); err != nil {
t.Fatalf("close nil pools: %v", err)
}
if defaultIfZeroDuration(0, time.Second) != time.Second {
t.Fatalf("helper regression")
}
}