MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestSQLFormatter_WithClauses

Function TestSQLFormatter_WithClauses

cmd/gosqlx/cmd/sql_formatter_test.go:177–233  ·  view source on GitHub ↗

TestSQLFormatter_WithClauses tests CTE (WITH clause) formatting

(t *testing.T)

Source from the content-addressed store, hash-verified

175
176// TestSQLFormatter_WithClauses tests CTE (WITH clause) formatting
177func TestSQLFormatter_WithClauses(t *testing.T) {
178 tests := []struct {
179 name string
180 sql string
181 expectWords []string
182 }{
183 {
184 name: "simple WITH clause",
185 sql: "WITH temp AS (SELECT id FROM users) SELECT * FROM temp",
186 expectWords: []string{"with", "as", "select"},
187 },
188 {
189 name: "recursive CTE",
190 sql: "WITH RECURSIVE cte AS (SELECT 1 as n UNION ALL SELECT n + 1 FROM cte WHERE n < 10) SELECT * FROM cte",
191 expectWords: []string{"with", "recursive", "as", "union"},
192 },
193 }
194
195 for _, tt := range tests {
196 t.Run(tt.name, func(t *testing.T) {
197 tkz := tokenizer.GetTokenizer()
198 defer tokenizer.PutTokenizer(tkz)
199
200 tokens, err := tkz.Tokenize([]byte(tt.sql))
201 if err != nil {
202 t.Fatalf("Tokenization failed: %v", err)
203 }
204
205 p := parser.NewParser()
206 astObj := ast.NewAST()
207 defer ast.ReleaseAST(astObj)
208
209 result, err := p.ParseFromModelTokens(tokens)
210 if err != nil {
211 t.Skipf("Parsing failed (may not be supported yet): %v", err)
212 return
213 }
214 astObj.Statements = result.Statements
215
216 formatter := NewSQLFormatter(FormatterOptions{
217 Indent: " ",
218 UppercaseKw: false,
219 })
220
221 output, err := formatter.Format(astObj)
222 if err != nil {
223 t.Fatalf("Formatting failed: %v", err)
224 }
225
226 for _, word := range tt.expectWords {
227 if !strings.Contains(strings.ToLower(output), word) {
228 t.Errorf("Expected '%s' in output:\n%s", word, output)
229 }
230 }
231 })
232 }
233}
234

Callers

nothing calls this directly

Calls 12

ParseFromModelTokensMethod · 0.95
FormatMethod · 0.95
GetTokenizerFunction · 0.92
PutTokenizerFunction · 0.92
NewParserFunction · 0.92
NewASTFunction · 0.92
ReleaseASTFunction · 0.92
NewSQLFormatterFunction · 0.85
RunMethod · 0.80
TokenizeMethod · 0.80
FatalfMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected