| 20 | ) |
| 21 | |
| 22 | func TestFormat_BasicStatements(t *testing.T) { |
| 23 | tests := []struct { |
| 24 | name string |
| 25 | input string |
| 26 | wantErr bool |
| 27 | }{ |
| 28 | {"simple select", "SELECT id, name FROM users", false}, |
| 29 | {"select with where", "SELECT * FROM users WHERE id = 1", false}, |
| 30 | {"insert", "INSERT INTO users (id, name) VALUES (1, 'test')", false}, |
| 31 | {"update", "UPDATE users SET name = 'new' WHERE id = 1", false}, |
| 32 | {"delete", "DELETE FROM users WHERE id = 1", false}, |
| 33 | {"empty string", "", false}, |
| 34 | {"whitespace only", " ", false}, |
| 35 | {"invalid SQL", "SELEC BOGUS FROM", true}, |
| 36 | } |
| 37 | |
| 38 | f := New(Options{IndentSize: 2}) |
| 39 | |
| 40 | for _, tt := range tests { |
| 41 | t.Run(tt.name, func(t *testing.T) { |
| 42 | result, err := f.Format(tt.input) |
| 43 | if (err != nil) != tt.wantErr { |
| 44 | t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr) |
| 45 | return |
| 46 | } |
| 47 | if !tt.wantErr && tt.input != "" && tt.input != " " && result == "" { |
| 48 | t.Error("Format() returned empty string for valid non-empty SQL") |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestFormatString(t *testing.T) { |
| 55 | result, err := FormatString("SELECT 1") |