github.com/XiaoMi/Gaea@v1.2.5/proxy/plan/merge_result_test.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     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 plan
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/XiaoMi/Gaea/mysql"
    22  )
    23  
    24  func TestLimitSelectResult(t *testing.T) {
    25  	tests := []struct {
    26  		offset       int64
    27  		count        int64
    28  		retLen       int64
    29  		trimedRetLen int64
    30  	}{
    31  		{-1, -1, 5, 5},
    32  		{0, -1, 5, 5},
    33  		{0, 3, 5, 3},
    34  		{0, 10, 5, 5},
    35  		{3, 10, 5, 2},
    36  		{3, 1, 5, 1},
    37  		{4, 1, 5, 1},
    38  		{5, 1, 5, 0},
    39  		{5, 10, 5, 0},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		t.Run(fmt.Sprintf("%d:%d", test.offset, test.count), func(t *testing.T) {
    44  			info := &SelectPlan{
    45  				offset: test.offset,
    46  				count:  test.count,
    47  			}
    48  
    49  			ret := &mysql.Result{
    50  				Resultset: &mysql.Resultset{
    51  					Values:   make([][]interface{}, test.retLen),
    52  					RowDatas: make([]mysql.RowData, test.retLen),
    53  				},
    54  			}
    55  
    56  			if err := limitSelectResult(info, ret); err != nil {
    57  				t.Fatalf("limitSelectResult error: %v", err)
    58  			}
    59  
    60  			if int64(len(ret.Values)) != test.trimedRetLen {
    61  				t.Errorf("len Values not equal, expect: %d, actual: %d", test.trimedRetLen, len(ret.Values))
    62  			}
    63  		})
    64  	}
    65  }