github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/alias_test.go (about) 1 // Licensed to the LF AI & Data foundation under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package client 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/cockroachdb/errors" 25 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 26 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 27 "github.com/stretchr/testify/mock" 28 "github.com/stretchr/testify/suite" 29 ) 30 31 type AliasSuite struct { 32 MockSuiteBase 33 } 34 35 func (s *AliasSuite) TestCreateAlias() { 36 c := s.client 37 ctx, cancel := context.WithCancel(context.Background()) 38 defer cancel() 39 40 s.Run("normal_create", func() { 41 defer s.resetMock() 42 43 collName := fmt.Sprintf("coll_%s", randStr(6)) 44 alias := fmt.Sprintf("alias_%s", randStr(6)) 45 46 s.mock.EXPECT().CreateAlias(mock.Anything, mock.AnythingOfType("*milvuspb.CreateAliasRequest")). 47 Run(func(ctx context.Context, req *milvuspb.CreateAliasRequest) { 48 s.Equal(collName, req.GetCollectionName()) 49 s.Equal(alias, req.GetAlias()) 50 }).Return(s.getSuccessStatus(), nil) 51 err := c.CreateAlias(ctx, collName, alias) 52 s.NoError(err) 53 }) 54 55 s.Run("failure_cases", func() { 56 collName := fmt.Sprintf("coll_%s", randStr(6)) 57 alias := fmt.Sprintf("alias_%s", randStr(6)) 58 59 s.Run("return_error", func() { 60 defer s.resetMock() 61 s.mock.EXPECT().CreateAlias(mock.Anything, mock.AnythingOfType("*milvuspb.CreateAliasRequest")). 62 Return(nil, errors.New("mocked")) 63 err := c.CreateAlias(ctx, collName, alias) 64 s.Error(err) 65 }) 66 67 s.Run("failure_status", func() { 68 defer s.resetMock() 69 70 s.mock.EXPECT().CreateAlias(mock.Anything, mock.AnythingOfType("*milvuspb.CreateAliasRequest")). 71 Return(s.getStatus(commonpb.ErrorCode_UnexpectedError, "mocked"), nil) 72 err := c.CreateAlias(ctx, collName, alias) 73 s.Error(err) 74 }) 75 }) 76 77 s.Run("invalid_client", func() { 78 c := &GrpcClient{} 79 collName := fmt.Sprintf("coll_%s", randStr(6)) 80 alias := fmt.Sprintf("alias_%s", randStr(6)) 81 82 err := c.CreateAlias(ctx, collName, alias) 83 s.Error(err) 84 }) 85 } 86 87 func (s *AliasSuite) TestDropAlias() { 88 c := s.client 89 ctx, cancel := context.WithCancel(context.Background()) 90 defer cancel() 91 92 s.Run("normal_create", func() { 93 defer s.resetMock() 94 95 alias := fmt.Sprintf("alias_%s", randStr(6)) 96 97 s.mock.EXPECT().DropAlias(mock.Anything, mock.AnythingOfType("*milvuspb.DropAliasRequest")). 98 Run(func(ctx context.Context, req *milvuspb.DropAliasRequest) { 99 s.Equal(alias, req.GetAlias()) 100 }).Return(s.getSuccessStatus(), nil) 101 err := c.DropAlias(ctx, alias) 102 s.NoError(err) 103 }) 104 105 s.Run("failure_cases", func() { 106 alias := fmt.Sprintf("alias_%s", randStr(6)) 107 s.Run("return_error", func() { 108 defer s.resetMock() 109 s.mock.EXPECT().DropAlias(mock.Anything, mock.AnythingOfType("*milvuspb.DropAliasRequest")). 110 Return(nil, errors.New("mocked")) 111 err := c.DropAlias(ctx, alias) 112 s.Error(err) 113 }) 114 115 s.Run("failure_status", func() { 116 defer s.resetMock() 117 118 s.mock.EXPECT().DropAlias(mock.Anything, mock.AnythingOfType("*milvuspb.DropAliasRequest")). 119 Return(s.getStatus(commonpb.ErrorCode_UnexpectedError, "mocked"), nil) 120 err := c.DropAlias(ctx, alias) 121 s.Error(err) 122 }) 123 }) 124 125 s.Run("invalid_client", func() { 126 c := &GrpcClient{} 127 alias := fmt.Sprintf("alias_%s", randStr(6)) 128 129 err := c.DropAlias(ctx, alias) 130 s.Error(err) 131 }) 132 } 133 134 func (s *AliasSuite) TestAlterAlias() { 135 c := s.client 136 ctx, cancel := context.WithCancel(context.Background()) 137 defer cancel() 138 139 s.Run("normal_create", func() { 140 defer s.resetMock() 141 142 collName := fmt.Sprintf("coll_%s", randStr(6)) 143 alias := fmt.Sprintf("alias_%s", randStr(6)) 144 145 s.mock.EXPECT().AlterAlias(mock.Anything, mock.AnythingOfType("*milvuspb.AlterAliasRequest")). 146 Run(func(ctx context.Context, req *milvuspb.AlterAliasRequest) { 147 s.Equal(collName, req.GetCollectionName()) 148 s.Equal(alias, req.GetAlias()) 149 }).Return(s.getSuccessStatus(), nil) 150 err := c.AlterAlias(ctx, collName, alias) 151 s.NoError(err) 152 }) 153 154 s.Run("failure_cases", func() { 155 collName := fmt.Sprintf("coll_%s", randStr(6)) 156 alias := fmt.Sprintf("alias_%s", randStr(6)) 157 158 s.Run("return_error", func() { 159 defer s.resetMock() 160 s.mock.EXPECT().AlterAlias(mock.Anything, mock.AnythingOfType("*milvuspb.AlterAliasRequest")). 161 Return(nil, errors.New("mocked")) 162 err := c.AlterAlias(ctx, collName, alias) 163 s.Error(err) 164 }) 165 166 s.Run("failure_status", func() { 167 defer s.resetMock() 168 169 s.mock.EXPECT().AlterAlias(mock.Anything, mock.AnythingOfType("*milvuspb.AlterAliasRequest")). 170 Return(s.getStatus(commonpb.ErrorCode_UnexpectedError, "mocked"), nil) 171 err := c.AlterAlias(ctx, collName, alias) 172 s.Error(err) 173 }) 174 }) 175 176 s.Run("invalid_client", func() { 177 c := &GrpcClient{} 178 collName := fmt.Sprintf("coll_%s", randStr(6)) 179 alias := fmt.Sprintf("alias_%s", randStr(6)) 180 181 err := c.AlterAlias(ctx, collName, alias) 182 s.Error(err) 183 }) 184 185 } 186 187 func TestAlias(t *testing.T) { 188 suite.Run(t, new(AliasSuite)) 189 }