|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package formatter_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/ajitpratap0/GoSQLX/pkg/formatter" |
| 22 | + "github.com/ajitpratap0/GoSQLX/pkg/gosqlx" |
| 23 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 24 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 25 | +) |
| 26 | + |
| 27 | +// TestFormat_CreateSequence verifies that CREATE SEQUENCE statements are rendered |
| 28 | +// by the dedicated formatter arm rather than falling back to stmtSQL/TokenLiteral. |
| 29 | +func TestFormat_CreateSequence(t *testing.T) { |
| 30 | + sql := "CREATE SEQUENCE user_id_seq START WITH 1 INCREMENT BY 1" |
| 31 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Parse error: %v", err) |
| 34 | + } |
| 35 | + if len(tree.Statements) == 0 { |
| 36 | + t.Fatal("expected at least one statement") |
| 37 | + } |
| 38 | + stmt, ok := tree.Statements[0].(*ast.CreateSequenceStatement) |
| 39 | + if !ok { |
| 40 | + t.Fatalf("expected *ast.CreateSequenceStatement, got %T", tree.Statements[0]) |
| 41 | + } |
| 42 | + |
| 43 | + opts := ast.FormatOptions{} |
| 44 | + result := formatter.FormatStatement(stmt, opts) |
| 45 | + upper := strings.ToUpper(result) |
| 46 | + if !strings.Contains(upper, "CREATE") { |
| 47 | + t.Errorf("expected CREATE in output, got: %q", result) |
| 48 | + } |
| 49 | + if !strings.Contains(upper, "SEQUENCE") { |
| 50 | + t.Errorf("expected SEQUENCE in output, got: %q", result) |
| 51 | + } |
| 52 | + if !strings.Contains(result, "user_id_seq") { |
| 53 | + t.Errorf("expected sequence name in output, got: %q", result) |
| 54 | + } |
| 55 | + if !strings.Contains(upper, "START WITH") { |
| 56 | + t.Errorf("expected START WITH in output, got: %q", result) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestFormat_CreateSequence_IfNotExists verifies IF NOT EXISTS is rendered. |
| 61 | +func TestFormat_CreateSequence_IfNotExists(t *testing.T) { |
| 62 | + sql := "CREATE SEQUENCE IF NOT EXISTS s2 START WITH 10" |
| 63 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Parse error: %v", err) |
| 66 | + } |
| 67 | + stmt, ok := tree.Statements[0].(*ast.CreateSequenceStatement) |
| 68 | + if !ok { |
| 69 | + t.Fatalf("expected *ast.CreateSequenceStatement, got %T", tree.Statements[0]) |
| 70 | + } |
| 71 | + opts := ast.FormatOptions{} |
| 72 | + result := formatter.FormatStatement(stmt, opts) |
| 73 | + upper := strings.ToUpper(result) |
| 74 | + if !strings.Contains(upper, "IF NOT EXISTS") { |
| 75 | + t.Errorf("expected IF NOT EXISTS in output, got: %q", result) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// TestFormat_AlterSequence verifies ALTER SEQUENCE statements are formatted correctly. |
| 80 | +func TestFormat_AlterSequence(t *testing.T) { |
| 81 | + sql := "ALTER SEQUENCE user_id_seq RESTART WITH 100" |
| 82 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Parse error: %v", err) |
| 85 | + } |
| 86 | + stmt, ok := tree.Statements[0].(*ast.AlterSequenceStatement) |
| 87 | + if !ok { |
| 88 | + t.Fatalf("expected *ast.AlterSequenceStatement, got %T", tree.Statements[0]) |
| 89 | + } |
| 90 | + opts := ast.FormatOptions{} |
| 91 | + result := formatter.FormatStatement(stmt, opts) |
| 92 | + upper := strings.ToUpper(result) |
| 93 | + if !strings.Contains(upper, "ALTER") { |
| 94 | + t.Errorf("expected ALTER in output, got: %q", result) |
| 95 | + } |
| 96 | + if !strings.Contains(upper, "SEQUENCE") { |
| 97 | + t.Errorf("expected SEQUENCE in output, got: %q", result) |
| 98 | + } |
| 99 | + if !strings.Contains(result, "user_id_seq") { |
| 100 | + t.Errorf("expected sequence name in output, got: %q", result) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// TestFormat_DropSequence verifies DROP SEQUENCE statements are formatted correctly. |
| 105 | +func TestFormat_DropSequence(t *testing.T) { |
| 106 | + sql := "DROP SEQUENCE IF EXISTS user_id_seq" |
| 107 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("Parse error: %v", err) |
| 110 | + } |
| 111 | + stmt, ok := tree.Statements[0].(*ast.DropSequenceStatement) |
| 112 | + if !ok { |
| 113 | + t.Fatalf("expected *ast.DropSequenceStatement, got %T", tree.Statements[0]) |
| 114 | + } |
| 115 | + opts := ast.FormatOptions{} |
| 116 | + result := formatter.FormatStatement(stmt, opts) |
| 117 | + upper := strings.ToUpper(result) |
| 118 | + if !strings.Contains(upper, "DROP") { |
| 119 | + t.Errorf("expected DROP in output, got: %q", result) |
| 120 | + } |
| 121 | + if !strings.Contains(upper, "SEQUENCE") { |
| 122 | + t.Errorf("expected SEQUENCE in output, got: %q", result) |
| 123 | + } |
| 124 | + if !strings.Contains(upper, "IF EXISTS") { |
| 125 | + t.Errorf("expected IF EXISTS in output, got: %q", result) |
| 126 | + } |
| 127 | + if !strings.Contains(result, "user_id_seq") { |
| 128 | + t.Errorf("expected sequence name in output, got: %q", result) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// TestFormat_ShowStatement verifies SHOW statements are formatted correctly. |
| 133 | +func TestFormat_ShowStatement(t *testing.T) { |
| 134 | + sql := "SHOW TABLES" |
| 135 | + tree, err := gosqlx.Parse(sql) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Parse error: %v", err) |
| 138 | + } |
| 139 | + stmt, ok := tree.Statements[0].(*ast.ShowStatement) |
| 140 | + if !ok { |
| 141 | + t.Fatalf("expected *ast.ShowStatement, got %T", tree.Statements[0]) |
| 142 | + } |
| 143 | + opts := ast.FormatOptions{} |
| 144 | + result := formatter.FormatStatement(stmt, opts) |
| 145 | + upper := strings.ToUpper(result) |
| 146 | + if !strings.Contains(upper, "SHOW") { |
| 147 | + t.Errorf("expected SHOW in output, got: %q", result) |
| 148 | + } |
| 149 | + if !strings.Contains(upper, "TABLES") { |
| 150 | + t.Errorf("expected TABLES in output, got: %q", result) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// TestFormat_DescribeStatement verifies DESCRIBE statements are formatted correctly. |
| 155 | +func TestFormat_DescribeStatement(t *testing.T) { |
| 156 | + sql := "DESCRIBE users" |
| 157 | + tree, err := gosqlx.Parse(sql) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Parse error: %v", err) |
| 160 | + } |
| 161 | + stmt, ok := tree.Statements[0].(*ast.DescribeStatement) |
| 162 | + if !ok { |
| 163 | + t.Fatalf("expected *ast.DescribeStatement, got %T", tree.Statements[0]) |
| 164 | + } |
| 165 | + opts := ast.FormatOptions{} |
| 166 | + result := formatter.FormatStatement(stmt, opts) |
| 167 | + upper := strings.ToUpper(result) |
| 168 | + if !strings.Contains(upper, "DESCRIBE") { |
| 169 | + t.Errorf("expected DESCRIBE in output, got: %q", result) |
| 170 | + } |
| 171 | + if !strings.Contains(result, "users") { |
| 172 | + t.Errorf("expected table name 'users' in output, got: %q", result) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// TestFormat_DDL_KeywordCase verifies that DDL formatter respects keyword casing options. |
| 177 | +func TestFormat_DDL_KeywordCase(t *testing.T) { |
| 178 | + sql := "DROP SEQUENCE myseq" |
| 179 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Parse error: %v", err) |
| 182 | + } |
| 183 | + stmt := tree.Statements[0].(*ast.DropSequenceStatement) |
| 184 | + |
| 185 | + upperOpts := ast.FormatOptions{KeywordCase: ast.KeywordUpper} |
| 186 | + result := formatter.FormatStatement(stmt, upperOpts) |
| 187 | + if !strings.Contains(result, "DROP SEQUENCE") { |
| 188 | + t.Errorf("expected uppercase keywords, got: %q", result) |
| 189 | + } |
| 190 | + |
| 191 | + lowerOpts := ast.FormatOptions{KeywordCase: ast.KeywordLower} |
| 192 | + result = formatter.FormatStatement(stmt, lowerOpts) |
| 193 | + if !strings.Contains(result, "drop sequence") { |
| 194 | + t.Errorf("expected lowercase keywords, got: %q", result) |
| 195 | + } |
| 196 | +} |
0 commit comments