github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/enginetest/prepared_queries.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 enginetest
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/dolthub/go-mysql-server/enginetest"
    22  	"github.com/dolthub/go-mysql-server/enginetest/queries"
    23  	"github.com/dolthub/go-mysql-server/enginetest/scriptgen/setup"
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/vitess/go/sqltypes"
    26  	"github.com/dolthub/vitess/go/vt/proto/query"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  // DoltPreparedScripts tests dolt-specific prepared statements through
    31  // the handler interface.
    32  func DoltPreparedScripts(t *testing.T, harness enginetest.Harness) {
    33  	tests := []queries.QueryTest{
    34  		{
    35  			Query: "set @@SESSION.dolt_show_system_tables = 1",
    36  		},
    37  		{
    38  			Query: "SELECT table_name FROM information_schema.columns WHERE table_name = ? group by table_name ORDER BY ORDINAL_POSITION",
    39  			Bindings: map[string]*query.BindVariable{
    40  				"v1": sqltypes.StringBindVariable("dolt_history_mytable"),
    41  			},
    42  			Expected: []sql.Row{{"dolt_history_mytable"}},
    43  		},
    44  	}
    45  
    46  	harness.Setup(setup.MydbData, setup.MytableData)
    47  	e, err := harness.NewEngine(t)
    48  	require.NoError(t, err)
    49  	defer e.Close()
    50  
    51  	enginetest.RunQueryWithContext(t, e, harness, nil, "CREATE TABLE a (x int, y int, z int)")
    52  	enginetest.RunQueryWithContext(t, e, harness, nil, "INSERT INTO a VALUES (0,1,1), (1,1,1), (2,1,1), (3,2,2), (4,2,2)")
    53  	for _, tt := range tests {
    54  		t.Run(fmt.Sprintf("%s", tt.Query), func(t *testing.T) {
    55  			ctx := enginetest.NewContext(harness)
    56  			_, err := e.PrepareQuery(ctx, tt.Query)
    57  			require.NoError(t, err)
    58  			enginetest.TestQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, tt.Bindings)
    59  		})
    60  	}
    61  }