Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Add support for DROP VIEW #860

Open
wants to merge 7 commits into
base: feature/views
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test DropView node
Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
  • Loading branch information
agarciamontoro committed Oct 29, 2019
commit 38bf07f1466939c8a50309ba2abcc409ec907a0b
94 changes: 94 additions & 0 deletions sql/plan/drop_view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package plan

import (
"testing"

"github.com/src-d/go-mysql-server/memory"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"

"github.com/stretchr/testify/require"
)

// Generates a database with a single table called mytable and a catalog with
// the view that is also returned. The context returned is the one used to
// create the view.
func mockData(require *require.Assertions) (sql.Database, *sql.Catalog, *sql.Context, sql.View) {
table := memory.NewTable("mytable", sql.Schema{
{Name: "i", Source: "mytable", Type: sql.Int32},
{Name: "s", Source: "mytable", Type: sql.Text},
})

db := memory.NewDatabase("db")
db.AddTable("db", table)

catalog := sql.NewCatalog()
catalog.AddDatabase(db)

subqueryAlias := NewSubqueryAlias("myview",
NewProject(
[]sql.Expression{
expression.NewGetFieldWithTable(1, sql.Int32, table.Name(), "i", true),
},
NewUnresolvedTable("dual", ""),
),
)

createView := NewCreateView(db, subqueryAlias.Name(), nil, subqueryAlias, false)
createView.Catalog = catalog

ctx := sql.NewEmptyContext()

_, err := createView.RowIter(ctx)
require.NoError(err)

return db, catalog, ctx, createView.View()
}

// Tests that DropView works as expected and that the view is dropped in
// the catalog when RowIter is called, regardless of the value of ifExists
func TestDropExistingView(t *testing.T) {
require := require.New(t)

test := func(ifExists bool) {
db, catalog, ctx, view := mockData(require)

singleDropView := NewSingleDropView(db, view.Name())
dropView := NewDropView([]sql.Node{singleDropView}, ifExists)
dropView.Catalog = catalog

_, err := dropView.RowIter(ctx)
require.NoError(err)

require.False(catalog.ViewRegistry.Exists(db.Name(), view.Name()))
}

test(false)
test(true)
}

// Tests that DropView errors when trying to delete a non-existing view if and
// only if the flag ifExists is set to false
func TestDropNonExistingView(t *testing.T) {
require := require.New(t)

test := func(ifExists bool) error {
db, catalog, ctx, view := mockData(require)

singleDropView := NewSingleDropView(db, "non-existing-view")
dropView := NewDropView([]sql.Node{singleDropView}, ifExists)
dropView.Catalog = catalog

_, err := dropView.RowIter(ctx)

require.True(catalog.ViewRegistry.Exists(db.Name(), view.Name()))

return err
}

err := test(true)
require.NoError(err)

err = test(false)
require.Error(err)
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy