github.com/dolthub/go-mysql-server@v0.18.0/sql/analyzer/catalog_test.go (about) 1 // Copyright 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 analyzer 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 22 "github.com/dolthub/go-mysql-server/memory" 23 "github.com/dolthub/go-mysql-server/sql" 24 ) 25 26 func TestAllDatabases(t *testing.T) { 27 require := require.New(t) 28 29 var dbs = []sql.Database{ 30 memory.NewDatabase("a"), 31 memory.NewDatabase("b"), 32 memory.NewDatabase("c"), 33 } 34 35 c := NewCatalog(sql.NewDatabaseProvider(dbs...)) 36 37 databases := c.AllDatabases(sql.NewEmptyContext()) 38 require.Equal(4, len(databases)) 39 require.Equal("information_schema", databases[0].Name()) 40 require.Equal(dbs, databases[1:]) 41 } 42 43 func TestCatalogDatabase(t *testing.T) { 44 require := require.New(t) 45 46 mydb := memory.NewDatabase("foo") 47 c := NewCatalog(sql.NewDatabaseProvider(mydb)) 48 49 db, err := c.Database(sql.NewEmptyContext(), "flo") 50 require.EqualError(err, "database not found: flo, maybe you mean foo?") 51 require.Nil(db) 52 53 db, err = c.Database(sql.NewEmptyContext(), "foo") 54 require.NoError(err) 55 require.Equal(mydb, db) 56 } 57 58 func TestCatalogTable(t *testing.T) { 59 require := require.New(t) 60 61 db := memory.NewDatabase("foo") 62 pro := memory.NewDBProvider(db) 63 ctx := newContext(pro) 64 c := NewCatalog(pro) 65 66 table, _, err := c.Table(ctx, "foo", "bar") 67 require.EqualError(err, "table not found: bar") 68 require.Nil(table) 69 70 mytable := memory.NewTable(db, "bar", sql.PrimaryKeySchema{PkOrdinals: []int{}}, db.GetForeignKeyCollection()) 71 db.AddTable("bar", mytable) 72 73 table, _, err = c.Table(ctx, "foo", "baz") 74 require.EqualError(err, "table not found: baz, maybe you mean bar?") 75 require.Nil(table) 76 77 table, _, err = c.Table(ctx, "foo", "bar") 78 require.NoError(err) 79 require.Equal(mytable, table) 80 81 table, _, err = c.Table(ctx, "foo", "BAR") 82 require.NoError(err) 83 require.Equal(mytable, table) 84 } 85 86 func TestCatalogUnlockTables(t *testing.T) { 87 require := require.New(t) 88 db := memory.NewDatabase("db") 89 pro := memory.NewDBProvider(db) 90 c := NewCatalog(pro) 91 ctx := newContext(pro) 92 93 t1 := newLockableTable(memory.NewTable(db, "t1", sql.PrimaryKeySchema{}, db.GetForeignKeyCollection())) 94 t2 := newLockableTable(memory.NewTable(db, "t2", sql.PrimaryKeySchema{}, db.GetForeignKeyCollection())) 95 db.AddTable("t1", t1) 96 db.AddTable("t2", t2) 97 98 ctx.SetCurrentDatabase(db.Name()) 99 c.LockTable(ctx, "t1") 100 c.LockTable(ctx, "t2") 101 102 require.NoError(c.UnlockTables(ctx, ctx.ID())) 103 104 require.Equal(1, t1.unlocks) 105 require.Equal(1, t2.unlocks) 106 } 107 108 type lockableTable struct { 109 sql.Table 110 unlocks int 111 } 112 113 func (l *lockableTable) IgnoreSessionData() bool { 114 return true 115 } 116 117 func (l *lockableTable) UnderlyingTable() *memory.Table { 118 return nil 119 } 120 121 func newLockableTable(t sql.Table) *lockableTable { 122 return &lockableTable{Table: t} 123 } 124 125 var _ sql.Lockable = (*lockableTable)(nil) 126 127 func (l *lockableTable) Lock(ctx *sql.Context, write bool) error { 128 return nil 129 } 130 131 func (l *lockableTable) Unlock(ctx *sql.Context, id uint32) error { 132 l.unlocks++ 133 return nil 134 }