-
Notifications
You must be signed in to change notification settings - Fork 4
/
debug_test.go
79 lines (63 loc) · 1.79 KB
/
debug_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
package scan
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"
)
type NoopQueryer struct{}
func (n NoopQueryer) QueryContext(ctx context.Context, query string, args ...any) (Rows, error) {
return nil, nil
}
func TestDebugQueryerDefaultWriter(t *testing.T) {
d, ok := Debug(NoopQueryer{}, nil).(debugQueryer)
if !ok {
t.Fatal("DebugQueryer does not return an instance of debugQueryer")
}
debugFile, ok := d.w.(*os.File)
if !ok {
t.Fatal("writer for debugQueryer is not an *os.File")
}
if debugFile != os.Stdout {
t.Fatal("writer for debugQueryer is not os.Stdout")
}
}
func TestDebugQueryer(t *testing.T) {
dest := &bytes.Buffer{}
exec := Debug(NoopQueryer{}, dest)
sql := "A QUERY"
args := []any{"arg1", "arg2", "arg3"}
_, err := exec.QueryContext(context.Background(), sql, args...)
if err != nil {
t.Fatal("error running QueryContext")
}
debugsql, debugArgsStr, found := strings.Cut(dest.String(), "\n")
if !found {
t.Fatalf("arg delimiter not found in\n%s", dest.String())
}
if strings.TrimSpace(debugsql) != sql {
t.Fatalf("wrong debug sql.\nExpected: %s\nGot: %s", sql, strings.TrimSpace(debugsql))
}
debugArgsStr = strings.TrimSpace(debugArgsStr)
debugArgsStr = strings.Trim(debugArgsStr, "[]")
var debugArgs []string //nolint:prealloc
for _, s := range strings.Fields(debugArgsStr) {
s := strings.TrimSpace(s)
if s == "" {
continue
}
debugArgs = append(debugArgs, s)
}
if len(debugArgs) != len(args) {
t.Fatalf("wrong length of debug args.\nExpected: %d\nGot: %d\n\n%s", len(args), len(debugArgs), debugArgs)
}
for i := range args {
argStr := strings.TrimSpace(fmt.Sprint(args[i]))
debugStr := strings.TrimSpace(debugArgs[i])
if argStr != debugStr {
t.Fatalf("wrong debug arg %d.\nExpected: %s\nGot: %s", i, argStr, debugStr)
}
}
}