-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_test.go
More file actions
302 lines (291 loc) · 7.02 KB
/
Copy pathfunctions_test.go
File metadata and controls
302 lines (291 loc) · 7.02 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
package where_test
import (
"testing"
"github.com/pseudomuto/where"
"github.com/stretchr/testify/require"
)
func TestGetFunctionDef(t *testing.T) {
tests := []struct {
name string
functionName string
expectFound bool
expectedDef where.FunctionDef
}{
{
name: "LOWER function",
functionName: "LOWER",
expectFound: true,
expectedDef: where.FunctionDef{
Name: "LOWER",
Type: "string",
MinArgs: 1,
MaxArgs: 1,
Description: "Converts string to lowercase",
},
},
{
name: "UPPER function",
functionName: "UPPER",
expectFound: true,
expectedDef: where.FunctionDef{
Name: "UPPER",
Type: "string",
MinArgs: 1,
MaxArgs: 1,
Description: "Converts string to uppercase",
},
},
{
name: "COALESCE function",
functionName: "COALESCE",
expectFound: true,
expectedDef: where.FunctionDef{
Name: "COALESCE",
Type: "scalar",
MinArgs: 2,
MaxArgs: -1,
Description: "Returns first non-null value",
},
},
{
name: "NOW function",
functionName: "NOW",
expectFound: true,
expectedDef: where.FunctionDef{
Name: "NOW",
Type: "date",
MinArgs: 0,
MaxArgs: 0,
Description: "Returns current timestamp",
},
},
{
name: "case insensitive lookup",
functionName: "lower",
expectFound: true,
expectedDef: where.FunctionDef{
Name: "LOWER",
Type: "string",
MinArgs: 1,
MaxArgs: 1,
Description: "Converts string to lowercase",
},
},
{
name: "nonexistent function",
functionName: "NONEXISTENT",
expectFound: false,
},
{
name: "empty function name",
functionName: "",
expectFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
def, found := where.GetFunctionDef(tt.functionName)
require.Equal(t, tt.expectFound, found)
if tt.expectFound {
require.Equal(t, tt.expectedDef.Name, def.Name)
require.Equal(t, tt.expectedDef.Type, def.Type)
require.Equal(t, tt.expectedDef.MinArgs, def.MinArgs)
require.Equal(t, tt.expectedDef.MaxArgs, def.MaxArgs)
require.Equal(t, tt.expectedDef.Description, def.Description)
}
})
}
}
func TestValidateFunctionArgs(t *testing.T) {
tests := []struct {
name string
functionName string
argCount int
expectValid bool
}{
// Fixed argument count functions
{
name: "LOWER with correct args",
functionName: "LOWER",
argCount: 1,
expectValid: true,
},
{
name: "LOWER with too few args",
functionName: "LOWER",
argCount: 0,
expectValid: false,
},
{
name: "LOWER with too many args",
functionName: "LOWER",
argCount: 2,
expectValid: false,
},
// No argument functions
{
name: "NOW with correct args",
functionName: "NOW",
argCount: 0,
expectValid: true,
},
{
name: "NOW with extra args",
functionName: "NOW",
argCount: 1,
expectValid: false,
},
// Variable argument functions (MaxArgs = -1)
{
name: "COALESCE with minimum args",
functionName: "COALESCE",
argCount: 2,
expectValid: true,
},
{
name: "COALESCE with more args",
functionName: "COALESCE",
argCount: 5,
expectValid: true,
},
{
name: "COALESCE with too few args",
functionName: "COALESCE",
argCount: 1,
expectValid: false,
},
{
name: "CONCAT with minimum args",
functionName: "CONCAT",
argCount: 2,
expectValid: true,
},
{
name: "CONCAT with many args",
functionName: "CONCAT",
argCount: 10,
expectValid: true,
},
{
name: "CONCAT with too few args",
functionName: "CONCAT",
argCount: 1,
expectValid: false,
},
// Range argument functions
{
name: "SUBSTRING with min args",
functionName: "SUBSTRING",
argCount: 2,
expectValid: true,
},
{
name: "SUBSTRING with max args",
functionName: "SUBSTRING",
argCount: 3,
expectValid: true,
},
{
name: "SUBSTRING with too few args",
functionName: "SUBSTRING",
argCount: 1,
expectValid: false,
},
{
name: "SUBSTRING with too many args",
functionName: "SUBSTRING",
argCount: 4,
expectValid: false,
},
{
name: "ROUND with min args",
functionName: "ROUND",
argCount: 1,
expectValid: true,
},
{
name: "ROUND with max args",
functionName: "ROUND",
argCount: 2,
expectValid: true,
},
{
name: "ROUND with too many args",
functionName: "ROUND",
argCount: 3,
expectValid: false,
},
// Case insensitive
{
name: "lowercase function name",
functionName: "lower",
argCount: 1,
expectValid: true,
},
{
name: "mixed case function name",
functionName: "Lower",
argCount: 1,
expectValid: true,
},
// Nonexistent function
{
name: "nonexistent function",
functionName: "NONEXISTENT",
argCount: 1,
expectValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid := where.ValidateFunctionArgs(tt.functionName, tt.argCount)
require.Equal(t, tt.expectValid, valid)
})
}
}
func TestAllStandardFunctions(t *testing.T) {
// Test that all functions in StandardFunctions can be retrieved
expectedFunctions := []string{
"LOWER", "UPPER", "LENGTH", "TRIM", "LTRIM", "RTRIM", "SUBSTRING", "CONCAT",
"COALESCE", "GREATEST", "LEAST", "NOW", "CURRENT_DATE", "CURRENT_TIME",
"CURRENT_TIMESTAMP", "DATE", "TIME", "YEAR", "MONTH", "DAY", "HOUR",
"MINUTE", "SECOND", "EXTRACT", "IF", "CAST", "ABS", "ROUND", "FLOOR",
"CEIL", "SQRT", "POWER",
}
for _, funcName := range expectedFunctions {
t.Run(funcName, func(t *testing.T) {
def, found := where.GetFunctionDef(funcName)
require.True(t, found, "Function %s should be found", funcName)
require.Equal(t, funcName, def.Name)
require.NotEmpty(t, def.Type)
require.NotEmpty(t, def.Description)
require.GreaterOrEqual(t, def.MinArgs, 0)
require.True(t, def.MaxArgs >= def.MinArgs || def.MaxArgs == -1,
"MaxArgs should be >= MinArgs or -1 for unlimited")
})
}
}
func TestFunctionTypes(t *testing.T) {
tests := []struct {
functionName string
expectedType where.FunctionType
}{
{"LOWER", where.FunctionTypeString},
{"UPPER", where.FunctionTypeString},
{"LENGTH", where.FunctionTypeString},
{"COALESCE", where.FunctionTypeScalar},
{"GREATEST", where.FunctionTypeScalar},
{"NOW", where.FunctionTypeDate},
{"DATE", where.FunctionTypeDate},
{"CAST", where.FunctionTypeConversion},
{"ABS", where.FunctionTypeMath},
{"ROUND", where.FunctionTypeMath},
}
for _, tt := range tests {
t.Run(tt.functionName, func(t *testing.T) {
def, found := where.GetFunctionDef(tt.functionName)
require.True(t, found)
require.Equal(t, tt.expectedType, def.Type)
})
}
}