...

Source file src/go.etcd.io/etcd/client/v3/op_test.go

Documentation: go.etcd.io/etcd/client/v3

     1  // Copyright 2016 The etcd Authors
     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 clientv3
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	pb "go.etcd.io/etcd/api/v3/etcdserverpb"
    22  )
    23  
    24  // TestOpWithSort tests if WithSort(ASCEND, KEY) and WithLimit are specified,
    25  // RangeRequest ignores the SortOption to avoid unnecessarily fetching
    26  // the entire key-space.
    27  func TestOpWithSort(t *testing.T) {
    28  	opReq := OpGet("foo", WithSort(SortByKey, SortAscend), WithLimit(10)).toRequestOp().Request
    29  	q, ok := opReq.(*pb.RequestOp_RequestRange)
    30  	if !ok {
    31  		t.Fatalf("expected range request, got %v", reflect.TypeOf(opReq))
    32  	}
    33  	req := q.RequestRange
    34  	wreq := &pb.RangeRequest{Key: []byte("foo"), SortOrder: pb.RangeRequest_NONE, Limit: 10}
    35  	if !reflect.DeepEqual(req, wreq) {
    36  		t.Fatalf("expected %+v, got %+v", wreq, req)
    37  	}
    38  }
    39  
    40  func TestIsOptsWithPrefix(t *testing.T) {
    41  	optswithprefix := []OpOption{WithPrefix()}
    42  	if !IsOptsWithPrefix(optswithprefix) {
    43  		t.Errorf("IsOptsWithPrefix = false, expected true")
    44  	}
    45  
    46  	optswithfromkey := []OpOption{WithFromKey()}
    47  	if IsOptsWithPrefix(optswithfromkey) {
    48  		t.Errorf("IsOptsWithPrefix = true, expected false")
    49  	}
    50  }
    51  
    52  func TestIsOptsWithFromKey(t *testing.T) {
    53  	optswithfromkey := []OpOption{WithFromKey()}
    54  	if !IsOptsWithFromKey(optswithfromkey) {
    55  		t.Errorf("IsOptsWithFromKey = false, expected true")
    56  	}
    57  
    58  	optswithprefix := []OpOption{WithPrefix()}
    59  	if IsOptsWithFromKey(optswithprefix) {
    60  		t.Errorf("IsOptsWithFromKey = true, expected false")
    61  	}
    62  }
    63  

View as plain text