-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyesql_test.go
98 lines (86 loc) · 2.41 KB
/
yesql_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package yesql
import (
"testing"
"github.com/bitbus/sqlx"
)
func TestMustParseFilePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("MustParseFile should panic if an error occurs, got '%s'", r)
}
}()
MustParseFile("testdata/missing.sql")
}
func TestMustParseFileNoPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("MustParseFile should not panic if no error occurs, got '%s'", r)
}
}()
MustParseFile("testdata/valid.sql")
}
func TestMustParseBytesPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("MustParseBytes should panic if an error occurs, got '%s'", r)
}
}()
MustParseBytes([]byte("I won't work"))
}
func TestMustParseBytesNoPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("MustParseBytes should not panic if an error occurs, got '%s'", r)
}
}()
MustParseBytes([]byte("-- name: byte-me\nSELECT * FROM bytes;"))
}
func TestScan(t *testing.T) {
type Q struct {
Namespace `yesql:"namespace"`
Ignore string
SimpleQuery string `yesql:"$simple"`
RawQuery string `yesql:"multiline"`
}
type Q2 struct {
RawQuery string `yesql:"does-not-exist"`
}
type Q3 struct {
SimpleQuery string `yesql:"$simple"`
CommentsQuery string `yesql:"comments"`
}
var (
q Q
q2 Q2
q3 Q3
)
queries := MustParseFile("testdata/valid.sql")
err := Scan(&q, queries, NewPrepareHook(nil))
if err != nil {
t.Errorf("[q] failed to scan raw query to struct: %s", err)
}
if q.SimpleQuery != `SELECT * FROM simple;` {
t.Errorf("[q] want simple query but got %s", q.SimpleQuery)
}
if q.RawQuery != `SELECT * FROM multiline WHERE line = 42;` {
t.Errorf("[q] want raw query but got %s", q.RawQuery)
}
if err = Scan(&q2, queries, NewPrepareHook(nil)); err == nil {
t.Error("[q2] expected to fail at non-existent query 'does-not-exist' but didn't")
}
SetDefaultPrepareHook(NewSqlxPrepareHook[*sqlx.Stmt, *sqlx.NamedStmt](nil))
if err = Scan(&q3, queries); err != nil {
t.Errorf("[q3] failed to scan raw query to struct: %s", err)
}
if q3.SimpleQuery != `SELECT * FROM simple;` {
t.Errorf("[q3] want simple query but got %s", q3.SimpleQuery)
}
if q3.CommentsQuery != `SELECT * FROM comments;` {
t.Errorf("[q3] want simple query but got %s", q3.CommentsQuery)
}
}
func BenchmarkMustParseFile(b *testing.B) {
for i := 0; i < b.N; i++ {
MustParseFile("testdata/valid.sql")
}
}