github.com/XiaoMi/Gaea@v1.2.5/mysql/resultset_sort_test.go (about)

     1  // Copyright 2016 The kingshard Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // 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, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package mysql
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"sort"
    21  	"testing"
    22  )
    23  
    24  func TestResultsetSort(t *testing.T) {
    25  	r1 := new(Resultset)
    26  	r2 := new(Resultset)
    27  
    28  	r1.Values = [][]interface{}{
    29  		{int64(1), "a", []byte("aa")},
    30  		{int64(2), "a", []byte("bb")},
    31  		{int64(3), "c", []byte("bb")},
    32  	}
    33  
    34  	r1.RowDatas = []RowData{
    35  		RowData([]byte("1")),
    36  		RowData([]byte("2")),
    37  		RowData([]byte("3")),
    38  	}
    39  
    40  	s := new(ResultsetSorter)
    41  
    42  	s.Resultset = r1
    43  
    44  	s.sk = []SortKey{
    45  		{Column: 0, Direction: SortDesc},
    46  	}
    47  
    48  	sort.Sort(s)
    49  
    50  	r2.Values = [][]interface{}{
    51  		{int64(3), "c", []byte("bb")},
    52  		{int64(2), "a", []byte("bb")},
    53  		{int64(1), "a", []byte("aa")},
    54  	}
    55  
    56  	r2.RowDatas = []RowData{
    57  		RowData([]byte("3")),
    58  		RowData([]byte("2")),
    59  		RowData([]byte("1")),
    60  	}
    61  
    62  	if !reflect.DeepEqual(r1, r2) {
    63  		t.Fatal(fmt.Sprintf("%v %v", r1, r2))
    64  	}
    65  
    66  	s.sk = []SortKey{
    67  		{Column: 1, Direction: SortAsc},
    68  		{Column: 2, Direction: SortDesc},
    69  	}
    70  
    71  	sort.Sort(s)
    72  
    73  	r2.Values = [][]interface{}{
    74  		{int64(2), "a", []byte("bb")},
    75  		{int64(1), "a", []byte("aa")},
    76  		{int64(3), "c", []byte("bb")},
    77  	}
    78  
    79  	r2.RowDatas = []RowData{
    80  		RowData([]byte("2")),
    81  		RowData([]byte("1")),
    82  		RowData([]byte("3")),
    83  	}
    84  
    85  	if !reflect.DeepEqual(r1, r2) {
    86  		t.Fatal(fmt.Sprintf("%v %v", r1, r2))
    87  	}
    88  
    89  	s.sk = []SortKey{
    90  		{Column: 1, Direction: SortAsc},
    91  		{Column: 2, Direction: SortAsc},
    92  	}
    93  
    94  	sort.Sort(s)
    95  
    96  	r2.Values = [][]interface{}{
    97  		{int64(1), "a", []byte("aa")},
    98  		{int64(2), "a", []byte("bb")},
    99  		{int64(3), "c", []byte("bb")},
   100  	}
   101  
   102  	r2.RowDatas = []RowData{
   103  		RowData([]byte("1")),
   104  		RowData([]byte("2")),
   105  		RowData([]byte("3")),
   106  	}
   107  
   108  	if !reflect.DeepEqual(r1, r2) {
   109  		t.Fatal(fmt.Sprintf("%v %v", r1, r2))
   110  	}
   111  
   112  }