-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
54 lines (49 loc) · 1.44 KB
/
scanner_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
package yesql
import (
"context"
"testing"
)
func TestPrepareScan(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")
scanner := NewPrepareScanner(NewPrepareHook(nil))
err := scanner.ScanContext(context.Background(), &q, queries)
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 = scanner.ScanContext(context.Background(), &q2, queries); err == nil {
t.Error("[q2] expected to fail at non-existent query 'does-not-exist' but didn't")
}
if err = scanner.ScanContext(context.Background(), &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)
}
}