-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_comments_test.go
More file actions
162 lines (139 loc) · 3.51 KB
/
scanner_comments_test.go
File metadata and controls
162 lines (139 loc) · 3.51 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package queries
import (
"bufio"
"strings"
"testing"
)
func TestScannerSkipsCommentsBeforeNameDirective(t *testing.T) {
testCases := []struct {
name string
fileName string
content string
expectedQueryName string
expectedQuery string
}{
{
name: "Comments before name",
fileName: "test.sql",
content: `-- comment 1
-- comment 2
-- name: my-query
SELECT 42;`,
expectedQueryName: "my-query",
expectedQuery: "SELECT 42;",
},
{
name: "Multiple comments",
fileName: "test.sql",
content: `-- comment 1
-- comment 2
-- comment 3
-- name: test-query
SELECT * FROM users;`,
expectedQueryName: "test-query",
expectedQuery: "SELECT * FROM users;",
},
{
name: "Empty lines before name",
fileName: "test.sql",
content: `
-- name: test-query
SELECT 1;`,
expectedQueryName: "test-query",
expectedQuery: "SELECT 1;",
},
{
name: "Mixed empty lines and comments",
fileName: "test.sql",
content: `-- comment 1
-- comment 2
-- name: test-query
SELECT 1;`,
expectedQueryName: "test-query",
expectedQuery: "SELECT 1;",
},
{
name: "No name directive",
fileName: "my_query.sql",
content: `-- comment
SELECT * FROM users;`,
expectedQueryName: "my_query",
expectedQuery: "SELECT * FROM users;",
},
{
name: "Metadata before name",
fileName: "test.sql",
content: `-- description: test
-- name: test-query
SELECT 1;`,
expectedQueryName: "test-query",
expectedQuery: "SELECT 1;",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scanner := &Scanner{}
reader := strings.NewReader(tc.content)
bufScanner := bufio.NewScanner(reader)
queries := scanner.Run(tc.fileName, bufScanner)
if len(queries) != 1 {
t.Errorf("Expected 1 query, got %d. Queries: %v", len(queries), queries)
return
}
scannedQuery, ok := queries[tc.expectedQueryName]
if !ok {
t.Errorf("Query '%s' not found. Available queries: %v", tc.expectedQueryName, queries)
return
}
if scannedQuery.Query != tc.expectedQuery {
t.Errorf("Query content mismatch:\ngot: %q\nwant: %q",
scannedQuery.Query, tc.expectedQuery)
}
})
}
}
func TestScannerDoesNotCreateEmptyQueries(t *testing.T) {
t.Run("Only comments", func(t *testing.T) {
content := `-- comment 1
-- comment 2
-- comment 3`
scanner := &Scanner{}
reader := strings.NewReader(content)
bufScanner := bufio.NewScanner(reader)
queries := scanner.Run("test.sql", bufScanner)
if len(queries) != 0 {
t.Errorf("Expected 0 queries, got %d", len(queries))
}
})
t.Run("Only name directive", func(t *testing.T) {
content := `-- comment
-- name: test-query`
scanner := &Scanner{}
reader := strings.NewReader(content)
bufScanner := bufio.NewScanner(reader)
queries := scanner.Run("test.sql", bufScanner)
if len(queries) != 0 {
t.Errorf("Expected 0 queries, got %d", len(queries))
}
})
t.Run("Comments after name directive included", func(t *testing.T) {
content := `-- comment before
-- name: test-query
-- comment after
SELECT 1;`
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
}
if q, ok := queries["test-query"]; ok {
expected := "-- comment after\nSELECT 1;"
if q.Query != expected {
t.Errorf("Expected %q, got %q", expected, q.Query)
}
}
})
}