|
| 1 | +package compiler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/kyleconroy/sqlc/internal/config" |
| 9 | + "github.com/kyleconroy/sqlc/internal/engine/dolphin" |
| 10 | + "github.com/kyleconroy/sqlc/internal/engine/postgresql" |
| 11 | + "github.com/kyleconroy/sqlc/internal/engine/sqlite" |
| 12 | + "github.com/kyleconroy/sqlc/internal/opts" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_ParseQueryErrors(t *testing.T) { |
| 16 | + for _, tc := range []struct { |
| 17 | + name string |
| 18 | + engine config.Engine |
| 19 | + createStmt string |
| 20 | + selectStmt string |
| 21 | + parser Parser |
| 22 | + wantErr error |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "unreferenced order column postgresql", |
| 26 | + engine: config.EnginePostgreSQL, |
| 27 | + createStmt: `CREATE TABLE authors (id INT);`, |
| 28 | + selectStmt: `SELECT id FROM authors ORDER BY foo;`, |
| 29 | + parser: postgresql.NewParser(), |
| 30 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "unreferenced order column mysql", |
| 34 | + engine: config.EngineMySQL, |
| 35 | + createStmt: `CREATE TABLE authors (id INT);`, |
| 36 | + selectStmt: `SELECT id FROM authors ORDER BY foo;`, |
| 37 | + parser: dolphin.NewParser(), |
| 38 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "unreferenced order column sqlite", |
| 42 | + engine: config.EngineSQLite, |
| 43 | + createStmt: `CREATE TABLE authors (id INT);`, |
| 44 | + selectStmt: `SELECT id FROM authors ORDER BY foo;`, |
| 45 | + parser: sqlite.NewParser(), |
| 46 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "unreferenced group column postgresql", |
| 50 | + engine: config.EnginePostgreSQL, |
| 51 | + createStmt: `CREATE TABLE authors ( id INT );`, |
| 52 | + selectStmt: `SELECT id FROM authors GROUP BY foo;`, |
| 53 | + parser: postgresql.NewParser(), |
| 54 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "unreferenced group column mysql", |
| 58 | + engine: config.EngineMySQL, |
| 59 | + createStmt: `CREATE TABLE authors (id INT);`, |
| 60 | + selectStmt: `SELECT id FROM authors GROUP BY foo;`, |
| 61 | + parser: dolphin.NewParser(), |
| 62 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "unreferenced group column sqlite", |
| 66 | + engine: config.EngineSQLite, |
| 67 | + createStmt: `CREATE TABLE authors (id INT);`, |
| 68 | + selectStmt: `SELECT id FROM authors GROUP BY foo;`, |
| 69 | + parser: sqlite.NewParser(), |
| 70 | + wantErr: fmt.Errorf(`column reference "foo" not found`), |
| 71 | + }, |
| 72 | + } { |
| 73 | + t.Run(tc.name, func(t *testing.T) { |
| 74 | + conf := config.SQL{ |
| 75 | + Engine: tc.engine, |
| 76 | + } |
| 77 | + combo := config.CombinedSettings{} |
| 78 | + comp := NewCompiler(conf, combo) |
| 79 | + stmts, err := tc.parser.Parse(strings.NewReader(tc.createStmt)) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("cannot parse test catalog: %v", err) |
| 82 | + } |
| 83 | + err = comp.catalog.Update(stmts[0], comp) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("cannot update test catalog: %v", err) |
| 86 | + } |
| 87 | + stmts, err = tc.parser.Parse(strings.NewReader(tc.selectStmt)) |
| 88 | + if err != nil { |
| 89 | + t.Errorf("Parse failed: %v", err) |
| 90 | + } |
| 91 | + if len(stmts) != 1 { |
| 92 | + t.Errorf("expected one statement, got %d", len(stmts)) |
| 93 | + } |
| 94 | + |
| 95 | + _, err = comp.parseQuery(stmts[0].Raw, tc.selectStmt, opts.Parser{}) |
| 96 | + if err == nil { |
| 97 | + t.Fatalf("expected parseQuery to return an error, got nil") |
| 98 | + } |
| 99 | + if err.Error() != tc.wantErr.Error() { |
| 100 | + t.Errorf("error message: want %s, got %s", tc.wantErr, err.Error()) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments