|
| 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 rules_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 22 | + "github.com/ajitpratap0/GoSQLX/pkg/transpiler" |
| 23 | +) |
| 24 | + |
| 25 | +// transpileMyToPg is a test helper that runs Transpile MySQL→PostgreSQL. |
| 26 | +func transpileMyToPg(t *testing.T, sql string) string { |
| 27 | + t.Helper() |
| 28 | + result, err := transpiler.Transpile(sql, keywords.DialectMySQL, keywords.DialectPostgreSQL) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Transpile MySQL→PG %q: %v", sql, err) |
| 31 | + } |
| 32 | + return result |
| 33 | +} |
| 34 | + |
| 35 | +func containsCI(s, sub string) bool { |
| 36 | + return strings.Contains(strings.ToUpper(s), strings.ToUpper(sub)) |
| 37 | +} |
| 38 | + |
| 39 | +func TestMySQLAutoIncrement_ToSerial(t *testing.T) { |
| 40 | + in := "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))" |
| 41 | + out := transpileMyToPg(t, in) |
| 42 | + if !containsCI(out, "SERIAL") && !containsCI(out, "BIGSERIAL") { |
| 43 | + t.Errorf("expected SERIAL in output, got: %s", out) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestMySQLBigintAutoIncrement_ToBigserial(t *testing.T) { |
| 48 | + in := "CREATE TABLE events (id BIGINT AUTO_INCREMENT PRIMARY KEY, name TEXT)" |
| 49 | + out := transpileMyToPg(t, in) |
| 50 | + if !containsCI(out, "BIGSERIAL") { |
| 51 | + t.Errorf("expected BIGSERIAL in output for BIGINT AUTO_INCREMENT, got: %s", out) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestMySQLTinyint1_ToBoolean(t *testing.T) { |
| 56 | + in := "CREATE TABLE flags (id INT PRIMARY KEY, active TINYINT(1))" |
| 57 | + out := transpileMyToPg(t, in) |
| 58 | + if !containsCI(out, "BOOLEAN") { |
| 59 | + t.Errorf("expected BOOLEAN in output for TINYINT(1), got: %s", out) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestMySQL_SelectPassthrough(t *testing.T) { |
| 64 | + in := "SELECT id, name FROM users WHERE id = 1" |
| 65 | + out := transpileMyToPg(t, in) |
| 66 | + if out == "" { |
| 67 | + t.Error("expected non-empty output for basic SELECT") |
| 68 | + } |
| 69 | + if !containsCI(out, "SELECT") { |
| 70 | + t.Errorf("output should contain SELECT, got: %s", out) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestMySQLLimitComma_ToOffset(t *testing.T) { |
| 75 | + // The parser normalises LIMIT offset, count → Limit/Offset AST fields. |
| 76 | + // The formatter then emits LIMIT n OFFSET m. |
| 77 | + in := "SELECT * FROM users LIMIT 10, 20" |
| 78 | + out := transpileMyToPg(t, in) |
| 79 | + if out == "" { |
| 80 | + t.Error("expected non-empty output") |
| 81 | + } |
| 82 | + // Either OFFSET appears (properly rewritten) or at minimum LIMIT is present. |
| 83 | + if !containsCI(out, "LIMIT") { |
| 84 | + t.Errorf("expected LIMIT keyword in output, got: %s", out) |
| 85 | + } |
| 86 | +} |
0 commit comments