-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbetween_complex_test.go
More file actions
334 lines (287 loc) · 9.8 KB
/
between_complex_test.go
File metadata and controls
334 lines (287 loc) · 9.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Package gosqlx - between_complex_test.go
// End-to-end tests for BETWEEN with complex expressions using high-level API (Issue #180)
package gosqlx
import (
"strings"
"testing"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
)
// TestParse_BetweenWithArithmeticExpressions tests BETWEEN with arithmetic via high-level API
func TestParse_BetweenWithArithmeticExpressions(t *testing.T) {
sql := "SELECT * FROM products WHERE price BETWEEN price * 0.9 AND price * 1.1"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(astObj.Statements) != 1 {
t.Fatalf("expected 1 statement, got %d", len(astObj.Statements))
}
stmt, ok := astObj.Statements[0].(*ast.SelectStatement)
if !ok {
t.Fatalf("expected SelectStatement, got %T", astObj.Statements[0])
}
betweenExpr, ok := stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression, got %T", stmt.Where)
}
// Verify lower bound is multiplication
lowerBinary, ok := betweenExpr.Lower.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected lower bound to be BinaryExpression, got %T", betweenExpr.Lower)
}
if lowerBinary.Operator != "*" {
t.Errorf("expected operator '*', got '%s'", lowerBinary.Operator)
}
// Verify upper bound is multiplication
upperBinary, ok := betweenExpr.Upper.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected upper bound to be BinaryExpression, got %T", betweenExpr.Upper)
}
if upperBinary.Operator != "*" {
t.Errorf("expected operator '*', got '%s'", upperBinary.Operator)
}
}
// TestParse_BetweenWithIntervalArithmetic tests BETWEEN with INTERVAL expressions via high-level API
func TestParse_BetweenWithIntervalArithmetic(t *testing.T) {
sql := "SELECT * FROM orders WHERE created_at BETWEEN NOW() - INTERVAL '30 days' AND NOW()"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
stmt := astObj.Statements[0].(*ast.SelectStatement)
betweenExpr, ok := stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression, got %T", stmt.Where)
}
// Verify lower bound is subtraction (NOW() - INTERVAL)
lowerBinary, ok := betweenExpr.Lower.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected lower bound to be BinaryExpression, got %T", betweenExpr.Lower)
}
if lowerBinary.Operator != "-" {
t.Errorf("expected operator '-', got '%s'", lowerBinary.Operator)
}
// Verify INTERVAL expression in lower bound
intervalExpr, ok := lowerBinary.Right.(*ast.IntervalExpression)
if !ok {
t.Fatalf("expected IntervalExpression, got %T", lowerBinary.Right)
}
if intervalExpr.Value != "30 days" {
t.Errorf("expected interval '30 days', got '%s'", intervalExpr.Value)
}
}
// TestParse_BetweenWithSubqueries tests BETWEEN with subqueries via high-level API
func TestParse_BetweenWithSubqueries(t *testing.T) {
sql := "SELECT * FROM data WHERE value BETWEEN (SELECT min_val FROM limits) AND (SELECT max_val FROM limits)"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
stmt := astObj.Statements[0].(*ast.SelectStatement)
betweenExpr, ok := stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression, got %T", stmt.Where)
}
// Verify both bounds are subqueries
_, ok = betweenExpr.Lower.(*ast.SubqueryExpression)
if !ok {
t.Fatalf("expected lower bound to be SubqueryExpression, got %T", betweenExpr.Lower)
}
_, ok = betweenExpr.Upper.(*ast.SubqueryExpression)
if !ok {
t.Fatalf("expected upper bound to be SubqueryExpression, got %T", betweenExpr.Upper)
}
}
// TestParse_BetweenWithFunctionCalls tests BETWEEN with function calls via high-level API
func TestParse_BetweenWithFunctionCalls(t *testing.T) {
sql := "SELECT * FROM orders WHERE amount BETWEEN MIN(price) AND MAX(price) * 2"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
stmt := astObj.Statements[0].(*ast.SelectStatement)
betweenExpr, ok := stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression, got %T", stmt.Where)
}
// Verify lower bound is MIN function
lowerFunc, ok := betweenExpr.Lower.(*ast.FunctionCall)
if !ok {
t.Fatalf("expected lower bound to be FunctionCall, got %T", betweenExpr.Lower)
}
if lowerFunc.Name != "MIN" {
t.Errorf("expected function 'MIN', got '%s'", lowerFunc.Name)
}
// Verify upper bound is arithmetic with MAX function
upperBinary, ok := betweenExpr.Upper.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected upper bound to be BinaryExpression, got %T", betweenExpr.Upper)
}
upperFunc, ok := upperBinary.Left.(*ast.FunctionCall)
if !ok {
t.Fatalf("expected upper bound left to be FunctionCall, got %T", upperBinary.Left)
}
if upperFunc.Name != "MAX" {
t.Errorf("expected function 'MAX', got '%s'", upperFunc.Name)
}
}
// TestParse_BetweenComplexScenarios tests various complex BETWEEN scenarios
func TestParse_BetweenComplexScenarios(t *testing.T) {
tests := []struct {
name string
sql string
expectError bool
}{
{
name: "Arithmetic with addition",
sql: "SELECT * FROM t WHERE x BETWEEN a + b AND c + d",
expectError: false,
},
{
name: "Arithmetic with subtraction",
sql: "SELECT * FROM t WHERE x BETWEEN a - b AND c - d",
expectError: false,
},
{
name: "Mixed arithmetic",
sql: "SELECT * FROM t WHERE x BETWEEN a * b + c AND d / e - f",
expectError: false,
},
{
name: "Nested function calls",
sql: "SELECT * FROM t WHERE x BETWEEN ROUND(AVG(y)) AND CEIL(MAX(z))",
expectError: false,
},
{
name: "CAST expressions",
sql: "SELECT * FROM t WHERE x BETWEEN CAST(a AS INT) AND CAST(b AS INT)",
expectError: false,
},
{
name: "String concatenation",
sql: "SELECT * FROM t WHERE x BETWEEN a || 'low' AND b || 'high'",
expectError: false,
},
{
name: "Parenthesized expressions",
sql: "SELECT * FROM t WHERE x BETWEEN (a * 0.8) + discount AND (b * 1.2) - fee",
expectError: false,
},
{
name: "INTERVAL arithmetic multiple",
sql: "SELECT * FROM t WHERE ts BETWEEN NOW() - INTERVAL '7 days' AND NOW() - INTERVAL '1 day'",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
astObj, err := Parse(tt.sql)
if tt.expectError {
if err == nil {
t.Error("expected error, got none")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(astObj.Statements) != 1 {
t.Fatalf("expected 1 statement, got %d", len(astObj.Statements))
}
stmt, ok := astObj.Statements[0].(*ast.SelectStatement)
if !ok {
t.Fatalf("expected SelectStatement, got %T", astObj.Statements[0])
}
if stmt.Where == nil {
t.Fatal("expected WHERE clause, got nil")
}
_, ok = stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression in WHERE, got %T", stmt.Where)
}
})
}
}
// TestParse_BetweenWithNotOperator tests NOT BETWEEN with complex expressions
func TestParse_BetweenWithNotOperator(t *testing.T) {
sql := "SELECT * FROM products WHERE price NOT BETWEEN price * 0.5 AND price * 2"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
stmt := astObj.Statements[0].(*ast.SelectStatement)
betweenExpr, ok := stmt.Where.(*ast.BetweenExpression)
if !ok {
t.Fatalf("expected BetweenExpression, got %T", stmt.Where)
}
if !betweenExpr.Not {
t.Error("expected NOT BETWEEN, but Not flag is false")
}
// Verify both bounds are arithmetic expressions
_, ok = betweenExpr.Lower.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected lower bound to be BinaryExpression, got %T", betweenExpr.Lower)
}
_, ok = betweenExpr.Upper.(*ast.BinaryExpression)
if !ok {
t.Fatalf("expected upper bound to be BinaryExpression, got %T", betweenExpr.Upper)
}
}
// TestValidate_BetweenWithComplexExpressions tests Validate function with complex BETWEEN
func TestValidate_BetweenWithComplexExpressions(t *testing.T) {
sqls := []string{
"SELECT * FROM products WHERE price BETWEEN price * 0.9 AND price * 1.1",
"SELECT * FROM orders WHERE created_at BETWEEN NOW() - INTERVAL '30 days' AND NOW()",
"SELECT * FROM data WHERE value BETWEEN (SELECT MIN(x) FROM limits) AND (SELECT MAX(x) FROM limits)",
}
for _, sql := range sqls {
t.Run(sql, func(t *testing.T) {
err := Validate(sql)
if err != nil {
t.Errorf("unexpected validation error: %v", err)
}
})
}
}
// TestExtractMetadata_BetweenWithComplexExpressions tests ExtractMetadata function with complex BETWEEN
func TestExtractMetadata_BetweenWithComplexExpressions(t *testing.T) {
sql := "SELECT id, name, price FROM products WHERE price BETWEEN price * 0.9 AND price * 1.1 ORDER BY price"
astObj, err := Parse(sql)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
metadata := ExtractMetadata(astObj)
if metadata == nil {
t.Fatal("expected metadata, got nil")
}
// Verify tables
if len(metadata.Tables) != 1 {
t.Fatalf("expected 1 table, got %d", len(metadata.Tables))
}
if metadata.Tables[0] != "products" {
t.Errorf("expected table 'products', got '%s'", metadata.Tables[0])
}
// Verify columns (should include id, name, price)
// Price appears multiple times: in SELECT list, WHERE clause, and ORDER BY
expectedColumns := []string{"id", "name", "price"}
for _, col := range expectedColumns {
found := false
for _, metadataCol := range metadata.Columns {
if strings.Contains(strings.ToLower(metadataCol), col) {
found = true
break
}
}
if !found {
t.Errorf("expected column '%s' not found in metadata.Columns: %v", col, metadata.Columns)
}
}
// Verify the AST structure
stmt := astObj.Statements[0].(*ast.SelectStatement)
if stmt.Where == nil {
t.Error("expected WHERE clause, got nil")
}
if len(stmt.OrderBy) == 0 {
t.Error("expected ORDER BY clause")
}
}