-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsections_test.go
More file actions
190 lines (157 loc) · 4.92 KB
/
sections_test.go
File metadata and controls
190 lines (157 loc) · 4.92 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package queries
import (
"reflect"
"strings"
"testing"
)
func TestSectionNoMarkers(t *testing.T) {
raw := "SELECT * FROM users WHERE id = :id"
q, _ := NewQuery("test", "test.sql", raw, nil)
// No markers = no sections
if q.Section("verify") != "" {
t.Error("Section(\"verify\") should be empty")
}
if q.HasSection("verify") {
t.Error("HasSection(\"verify\") should be false")
}
if names := q.SectionNames(); len(names) != 0 {
t.Errorf("SectionNames() = %v, want []", names)
}
}
func TestSectionSimpleVerify(t *testing.T) {
raw := `INSERT INTO orders (user_id, total) VALUES (:user_id, 100);
-- @verify
SELECT * FROM orders WHERE user_id = :user_id;`
q, _ := NewQuery("insert-order", "test.sql", raw, nil)
wantVerify := "SELECT * FROM orders WHERE user_id = :user_id;"
if got := q.Section("verify"); got != wantVerify {
t.Errorf("Section(\"verify\") = %q, want %q", got, wantVerify)
}
if !q.HasSection("verify") {
t.Error("HasSection(\"verify\") should be true")
}
if names := q.SectionNames(); !reflect.DeepEqual(names, []string{"verify"}) {
t.Errorf("SectionNames() = %v, want [\"verify\"]", names)
}
}
func TestSectionMultipleMarkers(t *testing.T) {
raw := `-- @setup
DELETE FROM orders WHERE user_id = 99;
-- @execute
INSERT INTO orders (user_id, total) VALUES (99, 50);
-- @verify
SELECT * FROM orders WHERE user_id = 99;`
q, _ := NewQuery("full-test", "test.sql", raw, nil)
tests := []struct {
tag string
want string
}{
{"", ""},
{"setup", "DELETE FROM orders WHERE user_id = 99;"},
{"execute", "INSERT INTO orders (user_id, total) VALUES (99, 50);"},
{"verify", "SELECT * FROM orders WHERE user_id = 99;"},
}
for _, tt := range tests {
if got := q.Section(tt.tag); got != tt.want {
t.Errorf("Section(%q) = %q, want %q", tt.tag, got, tt.want)
}
}
want := []string{"setup", "execute", "verify"}
if got := q.SectionNames(); !reflect.DeepEqual(got, want) {
t.Errorf("SectionNames() = %v, want %v", got, want)
}
}
func TestSectionFlexibleWhitespace(t *testing.T) {
markers := []string{
"-- @verify",
"-- @verify",
"-- @verify",
"--\t@verify",
"-- \t @verify",
}
for _, marker := range markers {
raw := "INSERT INTO foo VALUES (1);\n" + marker + "\nSELECT * FROM foo;"
q, _ := NewQuery("test", "test.sql", raw, nil)
if !q.HasSection("verify") {
t.Errorf("marker %q: HasSection(\"verify\") = false", marker)
}
if got := q.Section("verify"); got != "SELECT * FROM foo;" {
t.Errorf("marker %q: Section(\"verify\") = %q", marker, got)
}
}
}
func TestSectionCaseInsensitive(t *testing.T) {
raw := "INSERT INTO foo VALUES (1);\n-- @VERIFY\nSELECT * FROM foo;"
q, _ := NewQuery("test", "test.sql", raw, nil)
if !q.HasSection("verify") {
t.Error("HasSection should be case insensitive")
}
}
func TestSectionEmpty(t *testing.T) {
raw := "SELECT * FROM foo;\n-- @verify"
q, _ := NewQuery("test", "test.sql", raw, nil)
if q.Section("verify") != "" {
t.Error("empty section should return empty string")
}
if q.HasSection("verify") {
t.Error("HasSection should be false for empty section")
}
}
func TestSectionQuery(t *testing.T) {
raw := `INSERT INTO orders (user_id, total) VALUES (:user_id, :total);
-- @verify
SELECT user_id, total FROM orders WHERE user_id = :user_id;`
q, _ := NewQuery("insert-order", "test.sql", raw, nil)
verifyQ := q.SectionQuery("verify")
if verifyQ == nil {
t.Fatal("SectionQuery(\"verify\") = nil")
}
if verifyQ.Name != "insert-order@verify" {
t.Errorf("verify.Name = %q", verifyQ.Name)
}
if !reflect.DeepEqual(verifyQ.Args, []string{"user_id"}) {
t.Errorf("verify.Args = %v", verifyQ.Args)
}
if !strings.Contains(verifyQ.OrdinalQuery, "$1") {
t.Error("verify.OrdinalQuery should contain $1")
}
}
func TestSectionQueryCaching(t *testing.T) {
raw := "SELECT 1;\n-- @verify\nSELECT 2;"
q, _ := NewQuery("test", "test.sql", raw, nil)
q1 := q.SectionQuery("verify")
q2 := q.SectionQuery("verify")
if q1 != q2 {
t.Error("SectionQuery should return cached result")
}
}
func TestSectionQueryNil(t *testing.T) {
q, _ := NewQuery("test", "test.sql", "SELECT 1;", nil)
if q.SectionQuery("verify") != nil {
t.Error("non-existent section should return nil")
}
q2, _ := NewQuery("test", "test.sql", "SELECT 1;\n-- @verify", nil)
if q2.SectionQuery("verify") != nil {
t.Error("empty section should return nil")
}
}
func TestSectionNamesImmutable(t *testing.T) {
q, _ := NewQuery("test", "test.sql", "SELECT 1;\n-- @verify\nSELECT 2;", nil)
names := q.SectionNames()
names[0] = "modified"
if q.SectionNames()[0] != "verify" {
t.Error("SectionNames should return a copy")
}
}
func TestBackwardCompatibility(t *testing.T) {
raw := `INSERT INTO orders VALUES (1);
-- @verify
SELECT * FROM orders;`
q, _ := NewQuery("test", "test.sql", raw, nil)
if q.RawQuery() != raw {
t.Error("RawQuery() should return full query including markers")
}
if !strings.Contains(q.Query(), "@verify") {
t.Error("Query() should include @verify marker")
}
}