github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/options_test.go (about)

     1  // Copyright (C) 2019-2021 Zilliz. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
     4  // with the License. You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed under the License
     9  // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    10  // or implied. See the License for the specific language governing permissions and limitations under the License.
    11  
    12  package client
    13  
    14  import (
    15  	"math/rand"
    16  	"testing"
    17  
    18  	"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
    19  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestCreateCollectionWithConsistencyLevel(t *testing.T) {
    24  	opt := WithConsistencyLevel(entity.ConsistencyLevel(entity.ClBounded))
    25  	assert.NotNil(t, opt)
    26  	o := &createCollOpt{}
    27  
    28  	assert.NotPanics(t, func() {
    29  		opt(o)
    30  	})
    31  
    32  	assert.Equal(t, entity.ClBounded, o.ConsistencyLevel)
    33  }
    34  
    35  func TestCreateCollectionWithPartitionNum(t *testing.T) {
    36  	partitionNum := rand.Int63n(1000) + 1
    37  	opt := WithPartitionNum(partitionNum)
    38  	assert.NotNil(t, opt)
    39  	o := &createCollOpt{}
    40  
    41  	assert.NotPanics(t, func() {
    42  		opt(o)
    43  	})
    44  
    45  	assert.Equal(t, partitionNum, o.NumPartitions)
    46  }
    47  
    48  func TestLoadCollectionWithReplicaNumber(t *testing.T) {
    49  	opt := WithReplicaNumber(testMultiReplicaNumber)
    50  	assert.NotNil(t, opt)
    51  	req := &milvuspb.LoadCollectionRequest{}
    52  
    53  	assert.NotPanics(t, func() {
    54  		opt(req)
    55  	})
    56  
    57  	assert.Equal(t, testMultiReplicaNumber, req.GetReplicaNumber())
    58  }
    59  
    60  func TestMakeSearchQueryOption(t *testing.T) {
    61  	c := &entity.Collection{
    62  		Name:             "999",
    63  		ConsistencyLevel: entity.ClStrong,
    64  	}
    65  
    66  	cInfo := collInfo{
    67  		Name:             c.Name,
    68  		ConsistencyLevel: c.ConsistencyLevel,
    69  	}
    70  	MetaCache.setCollectionInfo(c.Name, &cInfo)
    71  
    72  	t.Run("strong consistency", func(t *testing.T) {
    73  		opt, err := makeSearchQueryOption(c.Name)
    74  		assert.Nil(t, err)
    75  		assert.NotNil(t, opt)
    76  		expected := &SearchQueryOption{
    77  			ConsistencyLevel:   entity.ClStrong,
    78  			GuaranteeTimestamp: StrongTimestamp,
    79  			IgnoreGrowing:      false,
    80  			ForTuning:          false,
    81  		}
    82  		assert.Equal(t, expected, opt)
    83  	})
    84  
    85  	t.Run("ignore growing", func(t *testing.T) {
    86  		opt, err := makeSearchQueryOption(c.Name, WithIgnoreGrowing())
    87  		assert.Nil(t, err)
    88  		assert.NotNil(t, opt)
    89  		expected := &SearchQueryOption{
    90  			ConsistencyLevel:   entity.ClStrong,
    91  			GuaranteeTimestamp: StrongTimestamp,
    92  			IgnoreGrowing:      true,
    93  			ForTuning:          false,
    94  		}
    95  		assert.Equal(t, expected, opt)
    96  	})
    97  
    98  	t.Run("for tuning", func(t *testing.T) {
    99  		opt, err := makeSearchQueryOption(c.Name, WithForTuning())
   100  		assert.Nil(t, err)
   101  		assert.NotNil(t, opt)
   102  		expected := &SearchQueryOption{
   103  			ConsistencyLevel:   entity.ClStrong,
   104  			GuaranteeTimestamp: StrongTimestamp,
   105  			IgnoreGrowing:      false,
   106  			ForTuning:          true,
   107  		}
   108  		assert.Equal(t, expected, opt)
   109  	})
   110  
   111  	t.Run("session consistency", func(t *testing.T) {
   112  		opt, err := makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClSession))
   113  		assert.Nil(t, err)
   114  		assert.NotNil(t, opt)
   115  		expected := &SearchQueryOption{
   116  			ConsistencyLevel:   entity.ClSession,
   117  			GuaranteeTimestamp: EventuallyTimestamp,
   118  			IgnoreGrowing:      false,
   119  			ForTuning:          false,
   120  		}
   121  		assert.Equal(t, expected, opt)
   122  
   123  		MetaCache.setSessionTs(c.Name, 99)
   124  		opt, err = makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClSession))
   125  		assert.Nil(t, err)
   126  		assert.NotNil(t, opt)
   127  		expected = &SearchQueryOption{
   128  			ConsistencyLevel:   entity.ClSession,
   129  			GuaranteeTimestamp: 99,
   130  			IgnoreGrowing:      false,
   131  			ForTuning:          false,
   132  		}
   133  		assert.Equal(t, expected, opt)
   134  	})
   135  
   136  	t.Run("bounded consistency", func(t *testing.T) {
   137  		opt, err := makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClBounded))
   138  		assert.Nil(t, err)
   139  		assert.NotNil(t, opt)
   140  		expected := &SearchQueryOption{
   141  			ConsistencyLevel:   entity.ClBounded,
   142  			GuaranteeTimestamp: BoundedTimestamp,
   143  			IgnoreGrowing:      false,
   144  			ForTuning:          false,
   145  		}
   146  		assert.Equal(t, expected, opt)
   147  	})
   148  
   149  	t.Run("eventually consistency", func(t *testing.T) {
   150  		opt, err := makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClEventually))
   151  		assert.Nil(t, err)
   152  		assert.NotNil(t, opt)
   153  		expected := &SearchQueryOption{
   154  			ConsistencyLevel:   entity.ClEventually,
   155  			GuaranteeTimestamp: EventuallyTimestamp,
   156  			IgnoreGrowing:      false,
   157  			ForTuning:          false,
   158  		}
   159  		assert.Equal(t, expected, opt)
   160  	})
   161  
   162  	t.Run("customized consistency", func(t *testing.T) {
   163  		opt, err := makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClCustomized), WithGuaranteeTimestamp(100))
   164  		assert.Nil(t, err)
   165  		assert.NotNil(t, opt)
   166  		expected := &SearchQueryOption{
   167  			ConsistencyLevel:   entity.ClCustomized,
   168  			GuaranteeTimestamp: 100,
   169  			IgnoreGrowing:      false,
   170  			ForTuning:          false,
   171  		}
   172  		assert.Equal(t, expected, opt)
   173  	})
   174  
   175  	t.Run("guarantee timestamp sanity check", func(t *testing.T) {
   176  		_, err := makeSearchQueryOption(c.Name, WithSearchQueryConsistencyLevel(entity.ClStrong), WithGuaranteeTimestamp(100))
   177  		assert.Error(t, err)
   178  	})
   179  }