|
| 1 | +package plan |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/src-d/go-mysql-server/memory" |
| 7 | + "github.com/src-d/go-mysql-server/sql" |
| 8 | + "github.com/src-d/go-mysql-server/sql/expression" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// Generates a database with a single table called mytable and a catalog with |
| 14 | +// the view that is also returned. The context returned is the one used to |
| 15 | +// create the view. |
| 16 | +func mockData(require *require.Assertions) (sql.Database, *sql.Catalog, *sql.Context, sql.View) { |
| 17 | + table := memory.NewTable("mytable", sql.Schema{ |
| 18 | + {Name: "i", Source: "mytable", Type: sql.Int32}, |
| 19 | + {Name: "s", Source: "mytable", Type: sql.Text}, |
| 20 | + }) |
| 21 | + |
| 22 | + db := memory.NewDatabase("db") |
| 23 | + db.AddTable("db", table) |
| 24 | + |
| 25 | + catalog := sql.NewCatalog() |
| 26 | + catalog.AddDatabase(db) |
| 27 | + |
| 28 | + subqueryAlias := NewSubqueryAlias("myview", |
| 29 | + NewProject( |
| 30 | + []sql.Expression{ |
| 31 | + expression.NewGetFieldWithTable(1, sql.Int32, table.Name(), "i", true), |
| 32 | + }, |
| 33 | + NewUnresolvedTable("dual", ""), |
| 34 | + ), |
| 35 | + ) |
| 36 | + |
| 37 | + createView := NewCreateView(db, subqueryAlias.Name(), nil, subqueryAlias, false) |
| 38 | + createView.Catalog = catalog |
| 39 | + |
| 40 | + ctx := sql.NewEmptyContext() |
| 41 | + |
| 42 | + _, err := createView.RowIter(ctx) |
| 43 | + require.NoError(err) |
| 44 | + |
| 45 | + return db, catalog, ctx, createView.View() |
| 46 | +} |
| 47 | + |
| 48 | +// Tests that DropView works as expected and that the view is dropped in |
| 49 | +// the catalog when RowIter is called, regardless of the value of ifExists |
| 50 | +func TestDropExistingView(t *testing.T) { |
| 51 | + require := require.New(t) |
| 52 | + |
| 53 | + test := func(ifExists bool) { |
| 54 | + db, catalog, ctx, view := mockData(require) |
| 55 | + |
| 56 | + singleDropView := NewSingleDropView(db, view.Name()) |
| 57 | + dropView := NewDropView([]sql.Node{singleDropView}, ifExists) |
| 58 | + dropView.Catalog = catalog |
| 59 | + |
| 60 | + _, err := dropView.RowIter(ctx) |
| 61 | + require.NoError(err) |
| 62 | + |
| 63 | + require.False(catalog.ViewRegistry.Exists(db.Name(), view.Name())) |
| 64 | + } |
| 65 | + |
| 66 | + test(false) |
| 67 | + test(true) |
| 68 | +} |
| 69 | + |
| 70 | +// Tests that DropView errors when trying to delete a non-existing view if and |
| 71 | +// only if the flag ifExists is set to false |
| 72 | +func TestDropNonExistingView(t *testing.T) { |
| 73 | + require := require.New(t) |
| 74 | + |
| 75 | + test := func(ifExists bool) error { |
| 76 | + db, catalog, ctx, view := mockData(require) |
| 77 | + |
| 78 | + singleDropView := NewSingleDropView(db, "non-existing-view") |
| 79 | + dropView := NewDropView([]sql.Node{singleDropView}, ifExists) |
| 80 | + dropView.Catalog = catalog |
| 81 | + |
| 82 | + _, err := dropView.RowIter(ctx) |
| 83 | + |
| 84 | + require.True(catalog.ViewRegistry.Exists(db.Name(), view.Name())) |
| 85 | + |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + err := test(true) |
| 90 | + require.NoError(err) |
| 91 | + |
| 92 | + err = test(false) |
| 93 | + require.Error(err) |
| 94 | +} |
0 commit comments