github.com/dolthub/go-mysql-server@v0.18.0/memory/persisted_session_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 memory 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 "gopkg.in/src-d/go-errors.v1" 24 25 "github.com/dolthub/go-mysql-server/sql" 26 _ "github.com/dolthub/go-mysql-server/sql/variables" 27 ) 28 29 func newPersistedSqlContext() *sql.Context { 30 ctx, _ := context.WithCancel(context.TODO()) 31 pro := NewDBProvider() 32 sess := sql.NewBaseSession() 33 34 persistedGlobals := GlobalsMap{"max_connections": 1000, "net_read_timeout": 1000, "auto_increment_increment": 123} 35 persistedSess := NewSession(sess, pro).SetGlobals(persistedGlobals) 36 sqlCtx := sql.NewContext(ctx) 37 sqlCtx.Session = persistedSess 38 return sqlCtx 39 } 40 41 func TestInitPersistedSession(t *testing.T) { 42 sqlCtx := newPersistedSqlContext() 43 pg := sqlCtx.Session.(*Session).persistedGlobals 44 res, ok := pg["max_connections"] 45 require.True(t, ok) 46 assert.Equal(t, 1000, res) 47 } 48 49 func TestPersistVariable(t *testing.T) { 50 persistTests := []struct { 51 title string 52 name string 53 value interface{} 54 err *errors.Kind 55 expectedCmp interface{} 56 }{ 57 {"set variable", "max_connections", int64(10), nil, int64(10)}, 58 {"set bad variable", "nonexistent_var", int64(10), sql.ErrUnknownSystemVariable, nil}, 59 } 60 61 for _, test := range persistTests { 62 t.Run(test.title, func(t *testing.T) { 63 sqlCtx := newPersistedSqlContext() 64 sess := sqlCtx.Session.(*Session) 65 err := sqlCtx.Session.(sql.PersistableSession).PersistGlobal(test.name, test.value) 66 if test.err != nil { 67 assert.True(t, test.err.Is(err)) 68 } else { 69 require.NoError(t, err) 70 res := sess.persistedGlobals[test.name] 71 assert.Equal(t, test.expectedCmp, res) 72 } 73 }) 74 } 75 } 76 77 func TestRemoveGlobal(t *testing.T) { 78 sqlCtx := newPersistedSqlContext() 79 sess := sqlCtx.Session.(*Session) 80 81 key := "auto_increment_increment" 82 err := sqlCtx.Session.(sql.PersistableSession).RemovePersistedGlobal(key) 83 require.NoError(t, err) 84 85 res := sess.persistedGlobals[key] 86 assert.Equal(t, nil, res) 87 assert.Equal(t, 2, len(sess.persistedGlobals)) 88 } 89 90 func TestRemoveAllGlobals(t *testing.T) { 91 sqlCtx := newPersistedSqlContext() 92 sess := sqlCtx.Session.(*Session) 93 err := sqlCtx.Session.(sql.PersistableSession).RemoveAllPersistedGlobals() 94 require.NoError(t, err) 95 assert.Equal(t, 0, len(sess.persistedGlobals)) 96 }