-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuncs_test.go
More file actions
121 lines (104 loc) · 2.78 KB
/
funcs_test.go
File metadata and controls
121 lines (104 loc) · 2.78 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
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
package sqlite
import (
"context"
"errors"
"testing"
)
func TestFunc(t *testing.T) {
type MyString struct{ s string }
db, err := Open(":memory:",
RegisterFunc("greetings", func(v string) string { return v + " world" }),
RegisterFunc("count_bytes", func(v []byte) int { return len(v) }),
RegisterFunc("my_add", func(v, w int) int { return v + w }),
RegisterFunc("my_onlyerror", func() error { return errors.New("onlyerror") }),
RegisterFunc("my_nevererror", func() (int, error) { return 42, nil }),
RegisterFunc("my_alwayserror", func() (int, error) { return 0, errors.New("alwayserror") }),
RegisterFunc("readmypointer", func(v *MyString) { v.s = "yes sir!" }),
RegisterFunc("add_all", func(vals ...int) int {
sum := 0
for _, v := range vals {
sum += v
}
return sum
}),
)
if err != nil {
t.Fatal(err)
}
t.Run("greetings", func(t *testing.T) {
var out string
err := db.Exec(context.Background(), "select greetings('hello')").ScanOne(&out)
if err != nil {
t.Fatal(err)
}
if out != "hello world" {
t.Error("invalid string", out)
}
})
t.Run("count_bytes", func(t *testing.T) {
var count int
err := db.Exec(context.Background(), "select count_bytes(?)", []byte("hello")).ScanOne(&count)
if err != nil {
t.Fatal(err)
}
if count != 5 {
t.Error("invalid bytes count", count)
}
})
t.Run("my_add", func(t *testing.T) {
var add int
err := db.Exec(context.Background(), "select my_add(40, 2)").ScanOne(&add)
if err != nil {
t.Fatal(err)
}
if add != 42 {
t.Error("invalid bytes count", add)
}
})
t.Run("my_onlyerror", func(t *testing.T) {
err := db.Exec(context.Background(), "select my_onlyerror()").Err()
if !errors.Is(err, errors.New("onlyerror")) {
t.Error("invalid error", err)
}
})
t.Run("my_nevererror", func(t *testing.T) {
var val int
err := db.Exec(context.Background(), "select my_nevererror()").ScanOne(&val)
if err != nil {
t.Fatal(err)
}
if val != 42 {
t.Error("invalid value returned", val)
}
})
t.Run("my_alwayserror", func(t *testing.T) {
var val int
err := db.Exec(context.Background(), "select my_alwayserror()").ScanOne(&val)
if !errors.Is(err, errors.New("alwayserror")) {
t.Error("invalid error", err)
}
if val != 0 {
t.Error("invalid value returned", val)
}
})
t.Run("readmypointer", func(t *testing.T) {
var val MyString
err := db.Exec(context.Background(), "select readmypointer(?)", db.AsPointer(&val)).Err()
if err != nil {
t.Fatal(err)
}
if val.s != "yes sir!" {
t.Error("invalid read back", val.s)
}
})
t.Run("varargs", func(t *testing.T) {
var sum int
err := db.Exec(context.Background(), "select add_all(1, 2, 3, 4, 5)").ScanOne(&sum)
if err != nil {
t.Fatal(err)
}
if sum != 15 {
t.Error("bad sum", sum)
}
})
}