github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/sqlpersist_test.go (about)

     1  // Copyright 2020 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 sqle
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/variables"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  // Structure for a test of a insert query
    32  type PersistTest struct {
    33  	// The name of this test. Names should be unique and descriptive.
    34  	Name string
    35  	// The insert query to run
    36  	PersistQuery string
    37  	// The insert query to run
    38  	SelectQuery string
    39  	// The schema of the result of the query, nil if an error is expected
    40  	ExpectedSchema schema.Schema
    41  	// The rows this query should return, nil if an error is expected
    42  	ExpectedRows []sql.Row
    43  	// The rows this query should return, nil if an error is expected
    44  	ExpectedConfig map[string]string
    45  	// An expected error string
    46  	ExpectedErr string
    47  	// Setup logic to run before executing this test, after initial tables have been created and populated
    48  	AdditionalSetup SetupFn
    49  }
    50  
    51  const maxConnTag = 0
    52  
    53  var MaxConnSchema = createMaxConnSchema()
    54  
    55  func createMaxConnSchema() schema.Schema {
    56  	colColl := schema.NewColCollection(
    57  		schema.NewColumn("@@GLOBAL.max_connections", maxConnTag, types.IntKind, false, schema.NotNullConstraint{}),
    58  	)
    59  	return schema.MustSchemaFromCols(colColl)
    60  }
    61  
    62  func NewMaxConnRow(value int) row.Row {
    63  	vals := row.TaggedValues{
    64  		maxConnTag: types.Int(value),
    65  	}
    66  
    67  	r, _ := row.New(types.Format_Default, MaxConnSchema, vals)
    68  	return r
    69  }
    70  
    71  func TestExecutePersist(t *testing.T) {
    72  	var persistTests = []PersistTest{
    73  		{
    74  			Name:           "SET PERSIST a system variable",
    75  			PersistQuery:   "SET PERSIST max_connections = 1000;",
    76  			ExpectedConfig: map[string]string{"max_connections": "1000"},
    77  			SelectQuery:    "SELECT @@GLOBAL.max_connections",
    78  			ExpectedRows:   ToSqlRows(MaxConnSchema, NewMaxConnRow(1000)),
    79  		},
    80  		{
    81  			Name:           "PERSIST ONLY a system variable",
    82  			PersistQuery:   "SET PERSIST_ONLY max_connections = 1000;",
    83  			ExpectedConfig: map[string]string{"max_connections": "1000"},
    84  			SelectQuery:    "SELECT @@GLOBAL.max_connections",
    85  			ExpectedRows:   ToSqlRows(MaxConnSchema, NewMaxConnRow(151)),
    86  		},
    87  	}
    88  	for _, test := range persistTests {
    89  		t.Run(test.Name, func(t *testing.T) {
    90  			testPersistQuery(t, test)
    91  		})
    92  	}
    93  }
    94  
    95  // Tests the given query on a freshly created dataset, asserting that the result has the given schema and rows. If
    96  // expectedErr is set, asserts instead that the execution returns an error that matches.
    97  func testPersistQuery(t *testing.T, test PersistTest) {
    98  	dEnv, err := CreateEmptyTestDatabase()
    99  	require.NoError(t, err)
   100  	defer dEnv.DoltDB.Close()
   101  
   102  	if test.AdditionalSetup != nil {
   103  		test.AdditionalSetup(t, dEnv)
   104  	}
   105  
   106  	variables.InitSystemVariables()
   107  
   108  	root, _ := dEnv.WorkingRoot(context.Background())
   109  	root, err = executeModify(t, context.Background(), dEnv, root, test.PersistQuery)
   110  	if len(test.ExpectedErr) > 0 {
   111  		require.Error(t, err)
   112  		return
   113  	} else {
   114  		require.NoError(t, err)
   115  	}
   116  
   117  	actualRows, _, err := executeSelect(t, context.Background(), dEnv, root, test.SelectQuery)
   118  	require.NoError(t, err)
   119  
   120  	assert.Equal(t, test.ExpectedRows, actualRows)
   121  }