|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 7 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParserWithDialectOption(t *testing.T) { |
| 11 | + p := NewParser(WithDialect("mysql")) |
| 12 | + if p.Dialect() != "mysql" { |
| 13 | + t.Errorf("expected mysql, got %s", p.Dialect()) |
| 14 | + } |
| 15 | + |
| 16 | + p2 := NewParser() |
| 17 | + if p2.Dialect() != "postgresql" { |
| 18 | + t.Errorf("expected postgresql default, got %s", p2.Dialect()) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestTokenizerWithDialect(t *testing.T) { |
| 23 | + tkz, err := tokenizer.NewWithDialect(keywords.DialectMySQL) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + if tkz.Dialect() != keywords.DialectMySQL { |
| 28 | + t.Errorf("expected mysql, got %s", tkz.Dialect()) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestTokenizerSetDialect(t *testing.T) { |
| 33 | + tkz := tokenizer.GetTokenizer() |
| 34 | + defer tokenizer.PutTokenizer(tkz) |
| 35 | + |
| 36 | + tkz.SetDialect(keywords.DialectMySQL) |
| 37 | + if tkz.Dialect() != keywords.DialectMySQL { |
| 38 | + t.Errorf("expected mysql, got %s", tkz.Dialect()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestTokenizerDefaultDialect(t *testing.T) { |
| 43 | + tkz, err := tokenizer.NewWithDialect("") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + if tkz.Dialect() != keywords.DialectPostgreSQL { |
| 48 | + t.Errorf("expected postgresql default, got %s", tkz.Dialect()) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestParseWithDialect(t *testing.T) { |
| 53 | + // Basic SQL should parse with any dialect |
| 54 | + for _, dialect := range []keywords.SQLDialect{ |
| 55 | + keywords.DialectPostgreSQL, |
| 56 | + keywords.DialectMySQL, |
| 57 | + keywords.DialectSQLServer, |
| 58 | + } { |
| 59 | + t.Run(string(dialect), func(t *testing.T) { |
| 60 | + ast, err := ParseWithDialect("SELECT 1", dialect) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("ParseWithDialect(%s) failed: %v", dialect, err) |
| 63 | + } |
| 64 | + if ast == nil { |
| 65 | + t.Fatal("expected non-nil AST") |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestValidateWithDialect(t *testing.T) { |
| 72 | + err := ValidateWithDialect("SELECT * FROM users WHERE id = 1", keywords.DialectMySQL) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("ValidateWithDialect(mysql) failed: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + err = ValidateWithDialect("SELECT * FROM users WHERE id = 1", keywords.DialectPostgreSQL) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("ValidateWithDialect(postgresql) failed: %v", err) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestDefaultBehaviorUnchanged(t *testing.T) { |
| 84 | + // Validate() without dialect should still work (backward compatibility) |
| 85 | + err := Validate("SELECT * FROM users") |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Validate() failed: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + ast, err := ParseBytes([]byte("SELECT 1")) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("ParseBytes() failed: %v", err) |
| 93 | + } |
| 94 | + if ast == nil { |
| 95 | + t.Fatal("expected non-nil AST") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestMySQLKeywordsRecognized(t *testing.T) { |
| 100 | + // UNSIGNED is a MySQL-specific keyword; tokenizer should recognize it |
| 101 | + tkz, err := tokenizer.NewWithDialect(keywords.DialectMySQL) |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + |
| 106 | + tokens, err := tkz.Tokenize([]byte("SELECT UNSIGNED")) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("tokenize failed: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + // Should have at least 2 tokens (SELECT, UNSIGNED) |
| 112 | + if len(tokens) < 2 { |
| 113 | + t.Fatalf("expected at least 2 tokens, got %d", len(tokens)) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestPostgreSQLKeywordsRecognized(t *testing.T) { |
| 118 | + tkz, err := tokenizer.NewWithDialect(keywords.DialectPostgreSQL) |
| 119 | + if err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + |
| 123 | + tokens, err := tkz.Tokenize([]byte("SELECT ILIKE")) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("tokenize failed: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + if len(tokens) < 2 { |
| 129 | + t.Fatalf("expected at least 2 tokens, got %d", len(tokens)) |
| 130 | + } |
| 131 | +} |
0 commit comments