vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/orderby/orderby_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 orderby
    18  
    19  import (
    20  	"testing"
    21  
    22  	"vitess.io/vitess/go/test/endtoend/utils"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/test/endtoend/cluster"
    27  )
    28  
    29  func start(t *testing.T) (utils.MySQLCompare, func()) {
    30  	mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams)
    31  	require.NoError(t, err)
    32  
    33  	deleteAll := func() {
    34  		_, _ = utils.ExecAllowError(t, mcmp.VtConn, "set workload = oltp")
    35  
    36  		tables := []string{"t1", "t1_id2_idx", "t2", "t2_id4_idx"}
    37  		for _, table := range tables {
    38  			_, _ = mcmp.ExecAndIgnore("delete from " + table)
    39  		}
    40  	}
    41  
    42  	deleteAll()
    43  
    44  	return mcmp, func() {
    45  		deleteAll()
    46  		mcmp.Close()
    47  		cluster.PanicHandler(t)
    48  	}
    49  }
    50  
    51  func TestSimpleOrderBy(t *testing.T) {
    52  	mcmp, closer := start(t)
    53  	defer closer()
    54  
    55  	mcmp.Exec("insert into t1(id1, id2) values (0,10),(1,9),(2,8),(3,7),(4,6),(5,5)")
    56  	mcmp.AssertMatches(`SELECT id2 FROM t1 ORDER BY id2 ASC`, `[[INT64(5)] [INT64(6)] [INT64(7)] [INT64(8)] [INT64(9)] [INT64(10)]]`)
    57  }
    58  
    59  func TestOrderBy(t *testing.T) {
    60  	mcmp, closer := start(t)
    61  	defer closer()
    62  
    63  	mcmp.Exec("insert into t4(id1, id2) values(1,'a'), (2,'Abc'), (3,'b'), (4,'c'), (5,'test')")
    64  	mcmp.Exec("insert into t4(id1, id2) values(6,'d'), (7,'e'), (8,'F')")
    65  	// test ordering of varchar column
    66  	mcmp.AssertMatches("select id1, id2 from t4 order by id2 desc", `[[INT64(5) VARCHAR("test")] [INT64(8) VARCHAR("F")] [INT64(7) VARCHAR("e")] [INT64(6) VARCHAR("d")] [INT64(4) VARCHAR("c")] [INT64(3) VARCHAR("b")] [INT64(2) VARCHAR("Abc")] [INT64(1) VARCHAR("a")]]`)
    67  	// test ordering of int column
    68  	mcmp.AssertMatches("select id1, id2 from t4 order by id1 desc", `[[INT64(8) VARCHAR("F")] [INT64(7) VARCHAR("e")] [INT64(6) VARCHAR("d")] [INT64(5) VARCHAR("test")] [INT64(4) VARCHAR("c")] [INT64(3) VARCHAR("b")] [INT64(2) VARCHAR("Abc")] [INT64(1) VARCHAR("a")]]`)
    69  
    70  	defer func() {
    71  		utils.Exec(t, mcmp.VtConn, "set workload = oltp")
    72  		_, _ = mcmp.ExecAndIgnore("delete from t4")
    73  	}()
    74  	// Test the same queries in streaming mode
    75  	utils.Exec(t, mcmp.VtConn, "set workload = olap")
    76  	mcmp.AssertMatches("select id1, id2 from t4 order by id2 desc", `[[INT64(5) VARCHAR("test")] [INT64(8) VARCHAR("F")] [INT64(7) VARCHAR("e")] [INT64(6) VARCHAR("d")] [INT64(4) VARCHAR("c")] [INT64(3) VARCHAR("b")] [INT64(2) VARCHAR("Abc")] [INT64(1) VARCHAR("a")]]`)
    77  	mcmp.AssertMatches("select id1, id2 from t4 order by id1 desc", `[[INT64(8) VARCHAR("F")] [INT64(7) VARCHAR("e")] [INT64(6) VARCHAR("d")] [INT64(5) VARCHAR("test")] [INT64(4) VARCHAR("c")] [INT64(3) VARCHAR("b")] [INT64(2) VARCHAR("Abc")] [INT64(1) VARCHAR("a")]]`)
    78  }