Files
seaweedfs/weed/util/sqlutil/splitter_test.go

148 lines
4.5 KiB
Go
Raw Normal View History

2025-09-03 21:54:41 -07:00
package sqlutil
2025-09-03 17:47:24 -07:00
import (
"reflect"
"testing"
)
2025-09-03 21:54:41 -07:00
func TestSplitStatements(t *testing.T) {
2025-09-03 17:47:24 -07:00
tests := []struct {
name string
input string
expected []string
}{
{
name: "Simple single statement",
input: "SELECT * FROM users",
expected: []string{"SELECT * FROM users"},
},
{
name: "Multiple statements",
input: "SELECT * FROM users; SELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
},
{
name: "Semicolon in single quotes",
input: "SELECT 'hello;world' FROM users; SELECT * FROM orders;",
expected: []string{"SELECT 'hello;world' FROM users", "SELECT * FROM orders"},
},
{
name: "Semicolon in double quotes",
input: `SELECT "column;name" FROM users; SELECT * FROM orders;`,
expected: []string{`SELECT "column;name" FROM users`, "SELECT * FROM orders"},
},
{
name: "Escaped quotes in strings",
input: `SELECT 'don''t split; here' FROM users; SELECT * FROM orders;`,
expected: []string{`SELECT 'don''t split; here' FROM users`, "SELECT * FROM orders"},
},
{
name: "Escaped quotes in identifiers",
input: `SELECT "column""name" FROM users; SELECT * FROM orders;`,
expected: []string{`SELECT "column""name" FROM users`, "SELECT * FROM orders"},
},
{
name: "Single line comment",
input: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
2025-09-03 17:47:24 -07:00
},
{
name: "Single line comment with semicolon",
input: "SELECT * FROM users; -- Comment with; semicolon\nSELECT * FROM orders;",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
2025-09-03 17:47:24 -07:00
},
{
name: "Multi-line comment",
input: "SELECT * FROM users; /* Multi-line\ncomment */ SELECT * FROM orders;",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
2025-09-03 17:47:24 -07:00
},
{
name: "Multi-line comment with semicolon",
input: "SELECT * FROM users; /* Comment with; semicolon */ SELECT * FROM orders;",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
2025-09-03 17:47:24 -07:00
},
{
2025-09-03 17:57:06 -07:00
name: "Complex mixed case",
input: `SELECT 'test;string', "quoted;id" FROM users; -- Comment; here
2025-09-03 17:47:24 -07:00
/* Another; comment */
INSERT INTO users VALUES ('name''s value', "id""field");`,
expected: []string{
`SELECT 'test;string', "quoted;id" FROM users`,
2025-09-03 17:57:06 -07:00
`INSERT INTO users VALUES ('name''s value', "id""field")`,
2025-09-03 17:47:24 -07:00
},
},
{
name: "Empty statements filtered",
input: "SELECT * FROM users;;; SELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
},
{
name: "Whitespace handling",
input: " SELECT * FROM users ; SELECT * FROM orders ; ",
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
},
{
name: "Single statement without semicolon",
input: "SELECT * FROM users",
expected: []string{"SELECT * FROM users"},
},
{
name: "Empty query",
input: "",
expected: []string{},
},
{
name: "Only whitespace",
input: " \n\t ",
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-09-03 21:54:41 -07:00
result := SplitStatements(tt.input)
2025-09-03 17:47:24 -07:00
if !reflect.DeepEqual(result, tt.expected) {
2025-09-03 21:54:41 -07:00
t.Errorf("SplitStatements() = %v, expected %v", result, tt.expected)
2025-09-03 17:47:24 -07:00
}
})
}
}
2025-09-03 21:54:41 -07:00
func TestSplitStatements_EdgeCases(t *testing.T) {
2025-09-03 17:47:24 -07:00
tests := []struct {
name string
input string
expected []string
}{
{
name: "Nested comments are not supported but handled gracefully",
input: "SELECT * FROM users; /* Outer /* inner */ comment */ SELECT * FROM orders;",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users", "comment */ SELECT * FROM orders"},
2025-09-03 17:47:24 -07:00
},
{
name: "Unterminated string (malformed SQL)",
input: "SELECT 'unterminated string; SELECT * FROM orders;",
expected: []string{"SELECT 'unterminated string; SELECT * FROM orders;"},
},
{
name: "Unterminated comment (malformed SQL)",
input: "SELECT * FROM users; /* unterminated comment",
2025-09-03 17:57:06 -07:00
expected: []string{"SELECT * FROM users"},
2025-09-03 17:47:24 -07:00
},
{
name: "Multiple semicolons in quotes",
input: "SELECT ';;;' FROM users; SELECT ';;;' FROM orders;",
expected: []string{"SELECT ';;;' FROM users", "SELECT ';;;' FROM orders"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-09-03 21:54:41 -07:00
result := SplitStatements(tt.input)
2025-09-03 17:47:24 -07:00
if !reflect.DeepEqual(result, tt.expected) {
2025-09-03 21:54:41 -07:00
t.Errorf("SplitStatements() = %v, expected %v", result, tt.expected)
2025-09-03 17:47:24 -07:00
}
})
}
}