-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_demo_test.go
More file actions
66 lines (52 loc) · 1.31 KB
/
scanner_demo_test.go
File metadata and controls
66 lines (52 loc) · 1.31 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
package queries
import (
"bufio"
"strings"
"testing"
)
func TestScannerCommentsBeforeName(t *testing.T) {
t.Run("Comments before name directive", func(t *testing.T) {
content := `-- comment line 1
-- comment line 2
-- name: test-query
SELECT 42;`
scanner := &Scanner{}
reader := strings.NewReader(content)
bufScanner := bufio.NewScanner(reader)
queries := scanner.Run("test.sql", bufScanner)
if len(queries) != 1 {
t.Errorf("Expected 1 query, got %d", len(queries))
return
}
q, ok := queries["test-query"]
if !ok {
t.Errorf("Query 'test-query' not found")
return
}
if q.Query != "SELECT 42;" {
t.Errorf("Expected 'SELECT 42;', got %q", q.Query)
}
})
t.Run("Multiple queries with comments", func(t *testing.T) {
content := `-- comment 1
-- name: get-user
SELECT * FROM users WHERE id = :id;
-- comment 2
-- name: list-users
SELECT * FROM users;`
scanner := &Scanner{}
reader := strings.NewReader(content)
bufScanner := bufio.NewScanner(reader)
queries := scanner.Run("test.sql", bufScanner)
if len(queries) != 2 {
t.Errorf("Expected 2 queries, got %d", len(queries))
return
}
if _, ok := queries["get-user"]; !ok {
t.Errorf("Query 'get-user' not found")
}
if _, ok := queries["list-users"]; !ok {
t.Errorf("Query 'list-users' not found")
}
})
}