vitess.io/vitess@v0.16.2/go/vt/vtgate/endtoend/row_count_test.go (about)

     1  /*
     2  Copyright 2019 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 endtoend
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/mysql"
    27  )
    28  
    29  func TestRowCount(t *testing.T) {
    30  	ctx := context.Background()
    31  	conn, err := mysql.Connect(ctx, &vtParams)
    32  	require.NoError(t, err)
    33  	defer conn.Close()
    34  	type tc struct {
    35  		query    string
    36  		expected int
    37  	}
    38  	tests := []tc{{
    39  		query:    "insert into t1_row_count(id, id1) values(1, 1), (2, 1), (3, 3), (4, 3)",
    40  		expected: 4,
    41  	}, {
    42  		query:    "select * from t1_row_count",
    43  		expected: -1,
    44  	}, {
    45  		query:    "update t1_row_count set id1 = 500 where id in (1,3)",
    46  		expected: 2,
    47  	}, {
    48  		query:    "show tables",
    49  		expected: -1,
    50  	}, {
    51  		query:    "set @x = 24",
    52  		expected: 0,
    53  	}, {
    54  		query:    "delete from t1_row_count",
    55  		expected: 4,
    56  	}}
    57  
    58  	for _, test := range tests {
    59  		t.Run(test.query, func(t *testing.T) {
    60  			exec(t, conn, test.query)
    61  			qr := exec(t, conn, "select row_count()")
    62  			require.Equal(t, fmt.Sprintf("INT64(%d)", test.expected), qr.Rows[0][0].String())
    63  		})
    64  	}
    65  }