|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestStripLeadingComments(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "no comments", |
| 15 | + input: "SELECT * FROM users", |
| 16 | + expected: "SELECT * FROM users", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "block comment at start", |
| 20 | + input: "/*Fivetran*/CREATE SCHEMA test", |
| 21 | + expected: "CREATE SCHEMA test", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "block comment with spaces", |
| 25 | + input: "/* comment */ SELECT 1", |
| 26 | + expected: "SELECT 1", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "multiple block comments", |
| 30 | + input: "/* first */ /* second */ INSERT INTO t", |
| 31 | + expected: "INSERT INTO t", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "line comment at start", |
| 35 | + input: "-- comment\nSELECT 1", |
| 36 | + expected: "SELECT 1", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "mixed comments", |
| 40 | + input: "/* block */ -- line\nUPDATE t SET x=1", |
| 41 | + expected: "UPDATE t SET x=1", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "whitespace before comment", |
| 45 | + input: " /* comment */ DELETE FROM t", |
| 46 | + expected: "DELETE FROM t", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "unclosed block comment", |
| 50 | + input: "/* unclosed SELECT", |
| 51 | + expected: "/* unclosed SELECT", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "line comment without newline", |
| 55 | + input: "-- only comment", |
| 56 | + expected: "", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty string", |
| 60 | + input: "", |
| 61 | + expected: "", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "only whitespace", |
| 65 | + input: " ", |
| 66 | + expected: "", |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := stripLeadingComments(tt.input) |
| 73 | + if result != tt.expected { |
| 74 | + t.Errorf("stripLeadingComments(%q) = %q, want %q", tt.input, result, tt.expected) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestGetCommandType(t *testing.T) { |
| 81 | + // Create a minimal clientConn for testing |
| 82 | + c := &clientConn{} |
| 83 | + |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + query string |
| 87 | + expected string |
| 88 | + }{ |
| 89 | + // Basic commands without comments |
| 90 | + { |
| 91 | + name: "SELECT", |
| 92 | + query: "SELECT * FROM users", |
| 93 | + expected: "SELECT", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "INSERT", |
| 97 | + query: "INSERT INTO users VALUES (1)", |
| 98 | + expected: "INSERT", |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "UPDATE", |
| 102 | + query: "UPDATE users SET name='test'", |
| 103 | + expected: "UPDATE", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "DELETE", |
| 107 | + query: "DELETE FROM users", |
| 108 | + expected: "DELETE", |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "CREATE TABLE", |
| 112 | + query: "CREATE TABLE users (id INT)", |
| 113 | + expected: "CREATE TABLE", |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "CREATE SCHEMA", |
| 117 | + query: "CREATE SCHEMA myschema", |
| 118 | + expected: "CREATE SCHEMA", |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "DROP TABLE", |
| 122 | + query: "DROP TABLE users", |
| 123 | + expected: "DROP TABLE", |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "DROP SCHEMA", |
| 127 | + query: "DROP SCHEMA myschema", |
| 128 | + expected: "DROP SCHEMA", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "DROP SCHEMA IF EXISTS CASCADE", |
| 132 | + query: "DROP SCHEMA IF EXISTS myschema CASCADE", |
| 133 | + expected: "DROP SCHEMA", |
| 134 | + }, |
| 135 | + |
| 136 | + // Commands with Fivetran-style comments |
| 137 | + { |
| 138 | + name: "CREATE SCHEMA with Fivetran comment", |
| 139 | + query: "/*Fivetran*/CREATE SCHEMA test_schema", |
| 140 | + expected: "CREATE SCHEMA", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "DROP SCHEMA with Fivetran comment", |
| 144 | + query: "/*Fivetran*/DROP SCHEMA IF EXISTS test_schema CASCADE", |
| 145 | + expected: "DROP SCHEMA", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "CREATE TABLE with Fivetran comment", |
| 149 | + query: "/*Fivetran*/CREATE TABLE test_table (id INT)", |
| 150 | + expected: "CREATE TABLE", |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "INSERT with Fivetran comment", |
| 154 | + query: "/*Fivetran*/INSERT INTO test_table VALUES (1)", |
| 155 | + expected: "INSERT", |
| 156 | + }, |
| 157 | + |
| 158 | + // Commands with other comment styles |
| 159 | + { |
| 160 | + name: "SELECT with block comment", |
| 161 | + query: "/* query */ SELECT 1", |
| 162 | + expected: "SELECT", |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "UPDATE with line comment", |
| 166 | + query: "-- update query\nUPDATE t SET x=1", |
| 167 | + expected: "UPDATE", |
| 168 | + }, |
| 169 | + |
| 170 | + // Transaction commands |
| 171 | + { |
| 172 | + name: "BEGIN", |
| 173 | + query: "BEGIN", |
| 174 | + expected: "BEGIN", |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "COMMIT", |
| 178 | + query: "COMMIT", |
| 179 | + expected: "COMMIT", |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "ROLLBACK", |
| 183 | + query: "ROLLBACK", |
| 184 | + expected: "ROLLBACK", |
| 185 | + }, |
| 186 | + |
| 187 | + // Other commands |
| 188 | + { |
| 189 | + name: "SET", |
| 190 | + query: "SET search_path TO myschema", |
| 191 | + expected: "SET", |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "TRUNCATE", |
| 195 | + query: "TRUNCATE TABLE users", |
| 196 | + expected: "TRUNCATE TABLE", |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "ALTER", |
| 200 | + query: "ALTER TABLE users ADD COLUMN name TEXT", |
| 201 | + expected: "ALTER TABLE", |
| 202 | + }, |
| 203 | + |
| 204 | + // Edge cases |
| 205 | + { |
| 206 | + name: "lowercase command", |
| 207 | + query: "select * from users", |
| 208 | + expected: "SELECT", |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "mixed case with comment", |
| 212 | + query: "/*Test*/Select * From Users", |
| 213 | + expected: "SELECT", |
| 214 | + }, |
| 215 | + } |
| 216 | + |
| 217 | + for _, tt := range tests { |
| 218 | + t.Run(tt.name, func(t *testing.T) { |
| 219 | + // getCommandType expects uppercase input |
| 220 | + result := c.getCommandType(tt.query) |
| 221 | + if result != tt.expected { |
| 222 | + t.Errorf("getCommandType(%q) = %q, want %q", tt.query, result, tt.expected) |
| 223 | + } |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments