-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstreamsql_validation_test.go
More file actions
78 lines (71 loc) · 2.14 KB
/
streamsql_validation_test.go
File metadata and controls
78 lines (71 loc) · 2.14 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
package streamsql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFunctionValidationIntegration(t *testing.T) {
tests := []struct {
name string
sql string
expectError bool
errorContains string
}{
{
name: "Valid builtin function in SELECT",
sql: "SELECT abs(temperature) FROM stream",
expectError: false,
},
{
name: "Unknown function in SELECT",
sql: "SELECT unknown_func(temperature) FROM stream",
expectError: true,
errorContains: "unknown_func",
},
{
name: "Unknown function in WHERE",
sql: "SELECT temperature FROM stream WHERE unknown_func(temperature) > 0",
expectError: true,
errorContains: "unknown_func",
},
{
name: "Unknown function in HAVING",
sql: "SELECT temperature FROM stream GROUP BY device HAVING unknown_func(temperature) > 0",
expectError: true,
errorContains: "unknown_func",
},
{
name: "Valid nested functions",
sql: "SELECT sqrt(abs(temperature)) FROM stream",
expectError: false,
},
{
name: "Mixed valid and invalid functions",
sql: "SELECT abs(temperature), unknown_func(humidity) FROM stream",
expectError: true,
errorContains: "unknown_func",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ssql := New()
err := ssql.Execute(tt.sql)
if tt.expectError {
assert.Error(t, err, "Expected error for SQL: %s", tt.sql)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains, "Error should contain: %s", tt.errorContains)
}
} else {
assert.NoError(t, err, "Expected no error for SQL: %s", tt.sql)
}
})
}
}
func TestFunctionValidationWithCustomFunctions(t *testing.T) {
// 测试自定义函数注册后的验证
sql := "SELECT custom_func(temperature) FROM stream"
// 在没有注册自定义函数时应该报错
ssql := New()
err := ssql.Execute(sql)
assert.Error(t, err, "Should error when custom function is not registered")
assert.Contains(t, err.Error(), "custom_func", "Error should mention the unknown function")
}