-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerobun_test.go
69 lines (55 loc) · 1.21 KB
/
zerobun_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
package zerobun
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rzajac/zltest"
"github.com/uptrace/bun"
)
func TestQueryHookDebug(t *testing.T) {
zlt := zltest.New(t)
logger := zerolog.New(zlt).With().Timestamp().Logger()
qh := NewQueryHook(QueryHookOptions{Logger: &logger})
event := &bun.QueryEvent{
StartTime: time.Now(),
Query: "SELECT * FROM products",
Err: nil,
}
qh.AfterQuery(
context.Background(),
event,
)
ent := zlt.LastEntry()
bunInfo := map[string]interface{}{
OperationQueryName: event.Query,
OperationFieldName: event.Operation(),
}
tBunInfo, _ := ent.Map(BunInfoFieldName)
for k, v := range bunInfo {
if v != tBunInfo[k] {
t.Errorf(
"key ['%s'] returned '%s', expected '%s'",
k,
bunInfo[k],
v,
)
}
}
ent.ExpLevel(zerolog.DebugLevel)
}
func TestQueryHookError(t *testing.T) {
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
qh := NewQueryHook(QueryHookOptions{Logger: &logger})
event := &bun.QueryEvent{
StartTime: time.Now(),
Query: "SELECT * FROM products WHERE ID = 5",
Err: errors.New("database error"),
}
qh.AfterQuery(
context.Background(),
event,
)
}