package migrate import ( "strings" "testing" ) func TestPostgresDialect(t *testing.T) { pg := Postgres() t.Run("CreateTable", func(t *testing.T) { sql := pg.CreateTable("dbversion") if !strings.Contains(sql, "SERIAL PRIMARY KEY") { t.Errorf("Expected SERIAL PRIMARY KEY, got: %s", sql) } if !strings.Contains(sql, `"dbversion"`) { t.Errorf("Expected quoted table name, got: %s", sql) } }) t.Run("TableExists", func(t *testing.T) { sql := pg.TableExists("dbversion") if !strings.Contains(sql, `"dbversion"`) { t.Errorf("Expected quoted table name, got: %s", sql) } if !strings.Contains(sql, "SELECT 1") { t.Errorf("Expected SELECT 1 for existence check, got: %s", sql) } }) t.Run("CheckVersion", func(t *testing.T) { sql := pg.CheckVersion("dbversion") if !strings.Contains(sql, "ORDER BY id DESC") { t.Errorf("Expected ORDER BY id DESC, got: %s", sql) } if !strings.Contains(sql, "LIMIT 1") { t.Errorf("Expected LIMIT 1, got: %s", sql) } }) t.Run("InsertVersion", func(t *testing.T) { sql := pg.InsertVersion("dbversion") // PostgreSQL uses $1, $2 placeholders if !strings.Contains(sql, "$1") || !strings.Contains(sql, "$2") { t.Errorf("Expected PostgreSQL placeholders ($1, $2), got: %s", sql) } if !strings.Contains(sql, `"dbversion"`) { t.Errorf("Expected quoted table name, got: %s", sql) } }) t.Run("QuoteIdentifier", func(t *testing.T) { pg := postgres{} // Test normal identifier quoted := pg.quoteIdentifier("tablename") if quoted != `"tablename"` { t.Errorf("Expected quoted identifier, got: %s", quoted) } // Test identifier with quotes (SQL injection attempt) quoted = pg.quoteIdentifier(`table"; DROP TABLE users; --`) if !strings.Contains(quoted, `""`) { t.Errorf("Expected escaped quotes, got: %s", quoted) } }) }