| 9 | ) |
| 10 | |
| 11 | func TestAnalyzer_Analyze(t *testing.T) { |
| 12 | db := config.Database{ |
| 13 | Managed: true, |
| 14 | } |
| 15 | a := New(db) |
| 16 | defer a.Close(context.Background()) |
| 17 | |
| 18 | ctx := context.Background() |
| 19 | |
| 20 | migrations := []string{ |
| 21 | `CREATE TABLE users ( |
| 22 | id INTEGER PRIMARY KEY, |
| 23 | name TEXT NOT NULL, |
| 24 | email TEXT |
| 25 | )`, |
| 26 | } |
| 27 | |
| 28 | query := `SELECT id, name, email FROM users WHERE id = ?` |
| 29 | node := &ast.TODO{} |
| 30 | |
| 31 | result, err := a.Analyze(ctx, node, query, migrations, nil) |
| 32 | if err != nil { |
| 33 | t.Fatalf("Analyze failed: %v", err) |
| 34 | } |
| 35 | |
| 36 | if len(result.Columns) != 3 { |
| 37 | t.Errorf("Expected 3 columns, got %d", len(result.Columns)) |
| 38 | } |
| 39 | |
| 40 | expectedCols := []struct { |
| 41 | name string |
| 42 | dataType string |
| 43 | }{ |
| 44 | {"id", "integer"}, |
| 45 | {"name", "text"}, |
| 46 | {"email", "text"}, |
| 47 | } |
| 48 | |
| 49 | for i, expected := range expectedCols { |
| 50 | if i >= len(result.Columns) { |
| 51 | break |
| 52 | } |
| 53 | col := result.Columns[i] |
| 54 | if col.Name != expected.name { |
| 55 | t.Errorf("Column %d: expected name %q, got %q", i, expected.name, col.Name) |
| 56 | } |
| 57 | if col.DataType != expected.dataType { |
| 58 | t.Errorf("Column %d: expected dataType %q, got %q", i, expected.dataType, col.DataType) |
| 59 | } |
| 60 | if col.Table == nil || col.Table.Name != "users" { |
| 61 | t.Errorf("Column %d: expected table 'users', got %v", i, col.Table) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if len(result.Params) != 1 { |
| 66 | t.Errorf("Expected 1 parameter, got %d", len(result.Params)) |
| 67 | } |
| 68 | } |