| 347 | } |
| 348 | |
| 349 | func TestExpandSQLite(t *testing.T) { |
| 350 | ctx := context.Background() |
| 351 | |
| 352 | // Create an in-memory SQLite database using native API |
| 353 | conn, err := sqlite3.Open(":memory:") |
| 354 | if err != nil { |
| 355 | t.Fatalf("could not open SQLite: %v", err) |
| 356 | } |
| 357 | defer conn.Close() |
| 358 | |
| 359 | // Create a test table |
| 360 | err = conn.Exec(` |
| 361 | CREATE TABLE authors ( |
| 362 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 363 | name TEXT NOT NULL, |
| 364 | bio TEXT |
| 365 | ) |
| 366 | `) |
| 367 | if err != nil { |
| 368 | t.Fatalf("failed to create test table: %v", err) |
| 369 | } |
| 370 | |
| 371 | // Create the parser which also implements format.Dialect |
| 372 | parser := sqlite.NewParser() |
| 373 | |
| 374 | // Create the expander using native SQLite column getter |
| 375 | colGetter := &SQLiteColumnGetter{conn: conn} |
| 376 | exp := New(colGetter, parser, parser) |
| 377 | |
| 378 | tests := []struct { |
| 379 | name string |
| 380 | query string |
| 381 | expected string |
| 382 | }{ |
| 383 | { |
| 384 | name: "simple select star", |
| 385 | query: "SELECT * FROM authors", |
| 386 | expected: "SELECT id, name, bio FROM authors;", |
| 387 | }, |
| 388 | { |
| 389 | name: "select with no star", |
| 390 | query: "SELECT id, name FROM authors", |
| 391 | expected: "SELECT id, name FROM authors", // No change, returns original |
| 392 | }, |
| 393 | { |
| 394 | name: "select star with where clause", |
| 395 | query: "SELECT * FROM authors WHERE id = 1", |
| 396 | expected: "SELECT id, name, bio FROM authors WHERE id = 1;", |
| 397 | }, |
| 398 | { |
| 399 | name: "double star", |
| 400 | query: "SELECT *, * FROM authors", |
| 401 | expected: "SELECT id, name, bio, id, name, bio FROM authors;", |
| 402 | }, |
| 403 | { |
| 404 | name: "table qualified star", |
| 405 | query: "SELECT authors.* FROM authors", |
| 406 | expected: "SELECT authors.id, authors.name, authors.bio FROM authors;", |