vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/normalize/normalize_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package normalize
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"net/http"
    25  	"testing"
    26  	"time"
    27  
    28  	"vitess.io/vitess/go/test/endtoend/utils"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  
    33  	"vitess.io/vitess/go/mysql"
    34  )
    35  
    36  func TestNormalizeAllFields(t *testing.T) {
    37  	conn, err := mysql.Connect(context.Background(), &vtParams)
    38  	require.NoError(t, err)
    39  	defer conn.Close()
    40  
    41  	insertQuery := `insert into t1 values (1, "chars", "variable chars", x'73757265', 0x676F, 0.33, 9.99, 1, "1976-06-08", "small", "b", "{\"key\":\"value\"}", point(1,5), b'011', 0b0101)`
    42  	normalizedInsertQuery := `insert into t1 values (:vtg1, :vtg2, :vtg3, :vtg4, :vtg5, :vtg6, :vtg7, :vtg8, :vtg9, :vtg10, :vtg11, :vtg12, point(:vtg13, :vtg14), :vtg15, :vtg16)`
    43  	selectQuery := "select * from t1"
    44  	utils.Exec(t, conn, insertQuery)
    45  	qr := utils.Exec(t, conn, selectQuery)
    46  	assert.Equal(t, 1, len(qr.Rows), "wrong number of table rows, expected 1 but had %d. Results: %v", len(qr.Rows), qr.Rows)
    47  
    48  	// Now need to figure out the best way to check the normalized query in the planner cache...
    49  	results, err := getPlanCache(fmt.Sprintf("%s:%d", vtParams.Host, clusterInstance.VtgateProcess.Port))
    50  	require.Nil(t, err)
    51  	found := false
    52  	for _, record := range results {
    53  		key := record["Key"].(string)
    54  		if key == normalizedInsertQuery {
    55  			found = true
    56  			break
    57  		}
    58  	}
    59  	assert.True(t, found, "correctly normalized record not found in planner cache")
    60  }
    61  
    62  func getPlanCache(vtgateHostPort string) ([]map[string]any, error) {
    63  	var results []map[string]any
    64  	client := http.Client{
    65  		Timeout: 10 * time.Second,
    66  	}
    67  	resp, err := client.Get(fmt.Sprintf("http://%s/debug/query_plans", vtgateHostPort))
    68  	if err != nil {
    69  		return results, err
    70  	}
    71  	defer resp.Body.Close()
    72  	body, err := io.ReadAll(resp.Body)
    73  	if err != nil {
    74  		return results, err
    75  	}
    76  
    77  	err = json.Unmarshal(body, &results)
    78  	if err != nil {
    79  		return results, err
    80  	}
    81  
    82  	return results, nil
    83  }