github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/ddl_test.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rowexec 16 17 import ( 18 "io" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/go-mysql-server/memory" 24 "github.com/dolthub/go-mysql-server/sql" 25 "github.com/dolthub/go-mysql-server/sql/plan" 26 "github.com/dolthub/go-mysql-server/sql/types" 27 ) 28 29 func TestCreateTable(t *testing.T) { 30 require := require.New(t) 31 32 db := memory.NewDatabase("test") 33 pro := memory.NewDBProvider(db) 34 35 tables := db.Tables() 36 _, ok := tables["testTable"] 37 require.False(ok) 38 39 s := sql.NewPrimaryKeySchema(sql.Schema{ 40 {Name: "c1", Type: types.Text}, 41 {Name: "c2", Type: types.Int32}, 42 }) 43 44 ctx := newContext(pro) 45 require.NoError(createTable(t, ctx, db, "testTable", s, plan.IfNotExistsAbsent, plan.IsTempTableAbsent)) 46 47 tables = db.Tables() 48 49 newTable, ok := tables["testTable"] 50 require.True(ok) 51 52 require.Equal(newTable.Schema(), s.Schema) 53 54 for _, s := range newTable.Schema() { 55 require.Equal("testTable", s.Source) 56 } 57 58 require.Error(createTable(t, ctx, db, "testTable", s, plan.IfNotExistsAbsent, plan.IsTempTableAbsent)) 59 require.NoError(createTable(t, ctx, db, "testTable", s, plan.IfNotExists, plan.IsTempTableAbsent)) 60 } 61 62 func TestDropTable(t *testing.T) { 63 require := require.New(t) 64 65 db := memory.NewDatabase("test") 66 pro := memory.NewDBProvider(db) 67 ctx := newContext(pro) 68 69 s := sql.NewPrimaryKeySchema(sql.Schema{ 70 {Name: "c1", Type: types.Text}, 71 {Name: "c2", Type: types.Int32}, 72 }) 73 74 require.NoError(createTable(t, ctx, db, "testTable1", s, plan.IfNotExistsAbsent, plan.IsTempTableAbsent)) 75 require.NoError(createTable(t, ctx, db, "testTable2", s, plan.IfNotExistsAbsent, plan.IsTempTableAbsent)) 76 require.NoError(createTable(t, ctx, db, "testTable3", s, plan.IfNotExistsAbsent, plan.IsTempTableAbsent)) 77 78 d := plan.NewDropTable([]sql.Node{ 79 plan.NewResolvedTable(memory.NewTable(db.BaseDatabase, "testTable1", s, db.GetForeignKeyCollection()), db, nil), 80 plan.NewResolvedTable(memory.NewTable(db.BaseDatabase, "testTable2", s, db.GetForeignKeyCollection()), db, nil), 81 }, false) 82 rows, err := DefaultBuilder.Build(ctx, d, nil) 83 require.NoError(err) 84 85 r, err := rows.Next(ctx) 86 require.Nil(err) 87 require.Equal(sql.NewRow(types.NewOkResult(0)), r) 88 89 r, err = rows.Next(ctx) 90 require.Equal(io.EOF, err) 91 92 _, ok := db.Tables()["testTable1"] 93 require.False(ok) 94 _, ok = db.Tables()["testTable2"] 95 require.False(ok) 96 _, ok = db.Tables()["testTable3"] 97 require.True(ok) 98 99 d = plan.NewDropTable([]sql.Node{plan.NewResolvedTable(memory.NewTable(db.Database(), "testTable1", s, db.GetForeignKeyCollection()), db, nil)}, false) 100 _, err = DefaultBuilder.Build(ctx, d, nil) 101 require.Error(err) 102 103 d = plan.NewDropTable([]sql.Node{plan.NewResolvedTable(memory.NewTable(db.Database(), "testTable3", s, db.GetForeignKeyCollection()), db, nil)}, false) 104 _, err = DefaultBuilder.Build(ctx, d, nil) 105 require.NoError(err) 106 107 _, ok = db.Tables()["testTable3"] 108 require.False(ok) 109 } 110 111 func createTable(t *testing.T, ctx *sql.Context, db sql.Database, name string, schema sql.PrimaryKeySchema, ifNotExists plan.IfNotExistsOption, temporary plan.TempTableOption) error { 112 c := plan.NewCreateTable(db, name, ifNotExists, temporary, &plan.TableSpec{Schema: schema}) 113 114 rows, err := DefaultBuilder.Build(ctx, c, nil) 115 if err != nil { 116 return err 117 } 118 119 r, err := rows.Next(ctx) 120 require.Nil(t, err) 121 require.Equal(t, sql.NewRow(types.NewOkResult(0)), r) 122 123 r, err = rows.Next(ctx) 124 require.Equal(t, io.EOF, err) 125 return nil 126 }