collection_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. package client
  17. import (
  18. "context"
  19. "fmt"
  20. "testing"
  21. "github.com/samber/lo"
  22. "github.com/stretchr/testify/mock"
  23. "github.com/stretchr/testify/suite"
  24. "google.golang.org/protobuf/proto"
  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/milvus-io/milvus-proto/go-api/v2/schemapb"
  28. "github.com/milvus-io/milvus/client/v2/entity"
  29. "github.com/milvus-io/milvus/pkg/util/merr"
  30. )
  31. type CollectionSuite struct {
  32. MockSuiteBase
  33. }
  34. func (s *CollectionSuite) TestListCollection() {
  35. ctx, cancel := context.WithCancel(context.Background())
  36. defer cancel()
  37. s.Run("success", func() {
  38. s.mock.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&milvuspb.ShowCollectionsResponse{
  39. CollectionNames: []string{"test1", "test2", "test3"},
  40. }, nil).Once()
  41. names, err := s.client.ListCollections(ctx, NewListCollectionOption())
  42. s.NoError(err)
  43. s.ElementsMatch([]string{"test1", "test2", "test3"}, names)
  44. })
  45. s.Run("failure", func() {
  46. s.mock.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  47. _, err := s.client.ListCollections(ctx, NewListCollectionOption())
  48. s.Error(err)
  49. })
  50. }
  51. func (s *CollectionSuite) TestCreateCollection() {
  52. ctx, cancel := context.WithCancel(context.Background())
  53. defer cancel()
  54. s.Run("success", func() {
  55. s.mock.EXPECT().CreateCollection(mock.Anything, mock.Anything).Return(merr.Success(), nil).Once()
  56. s.mock.EXPECT().CreateIndex(mock.Anything, mock.Anything).Return(merr.Success(), nil).Once()
  57. s.mock.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(merr.Success(), nil).Once()
  58. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).Return(&milvuspb.DescribeIndexResponse{
  59. Status: merr.Success(),
  60. IndexDescriptions: []*milvuspb.IndexDescription{
  61. {FieldName: "vector", State: commonpb.IndexState_Finished},
  62. },
  63. }, nil).Once()
  64. s.mock.EXPECT().GetLoadingProgress(mock.Anything, mock.Anything).Return(&milvuspb.GetLoadingProgressResponse{
  65. Status: merr.Success(),
  66. Progress: 100,
  67. }, nil).Once()
  68. err := s.client.CreateCollection(ctx, SimpleCreateCollectionOptions("test_collection", 128))
  69. s.NoError(err)
  70. })
  71. s.Run("failure", func() {
  72. s.mock.EXPECT().CreateCollection(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  73. err := s.client.CreateCollection(ctx, SimpleCreateCollectionOptions("test_collection", 128))
  74. s.Error(err)
  75. })
  76. }
  77. func (s *CollectionSuite) TestCreateCollectionOptions() {
  78. collectionName := fmt.Sprintf("test_collection_%s", s.randString(6))
  79. opt := SimpleCreateCollectionOptions(collectionName, 128)
  80. req := opt.Request()
  81. s.Equal(collectionName, req.GetCollectionName())
  82. s.EqualValues(1, req.GetShardsNum())
  83. collSchema := &schemapb.CollectionSchema{}
  84. err := proto.Unmarshal(req.GetSchema(), collSchema)
  85. s.Require().NoError(err)
  86. s.True(collSchema.GetEnableDynamicField())
  87. collectionName = fmt.Sprintf("test_collection_%s", s.randString(6))
  88. opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).WithDynamicSchema(false)
  89. req = opt.Request()
  90. s.Equal(collectionName, req.GetCollectionName())
  91. s.EqualValues(1, req.GetShardsNum())
  92. collSchema = &schemapb.CollectionSchema{}
  93. err = proto.Unmarshal(req.GetSchema(), collSchema)
  94. s.Require().NoError(err)
  95. s.False(collSchema.GetEnableDynamicField())
  96. collectionName = fmt.Sprintf("test_collection_%s", s.randString(6))
  97. schema := entity.NewSchema().
  98. WithField(entity.NewField().WithName("int64").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
  99. WithField(entity.NewField().WithName("vector").WithDim(128).WithDataType(entity.FieldTypeFloatVector))
  100. opt = NewCreateCollectionOption(collectionName, schema).WithShardNum(2)
  101. req = opt.Request()
  102. s.Equal(collectionName, req.GetCollectionName())
  103. s.EqualValues(2, req.GetShardsNum())
  104. collSchema = &schemapb.CollectionSchema{}
  105. err = proto.Unmarshal(req.GetSchema(), collSchema)
  106. s.Require().NoError(err)
  107. }
  108. func (s *CollectionSuite) TestDescribeCollection() {
  109. ctx, cancel := context.WithCancel(context.Background())
  110. defer cancel()
  111. s.Run("success", func() {
  112. s.mock.EXPECT().DescribeCollection(mock.Anything, mock.Anything).Return(&milvuspb.DescribeCollectionResponse{
  113. Status: merr.Success(),
  114. Schema: &schemapb.CollectionSchema{
  115. Name: "test_collection",
  116. Fields: []*schemapb.FieldSchema{
  117. {FieldID: 100, DataType: schemapb.DataType_Int64, AutoID: true, Name: "ID"},
  118. {
  119. FieldID: 101, DataType: schemapb.DataType_FloatVector, Name: "vector",
  120. TypeParams: []*commonpb.KeyValuePair{
  121. {Key: "dim", Value: "128"},
  122. },
  123. },
  124. },
  125. },
  126. CollectionID: 1000,
  127. CollectionName: "test_collection",
  128. }, nil).Once()
  129. coll, err := s.client.DescribeCollection(ctx, NewDescribeCollectionOption("test_collection"))
  130. s.NoError(err)
  131. s.EqualValues(1000, coll.ID)
  132. s.Equal("test_collection", coll.Name)
  133. s.Len(coll.Schema.Fields, 2)
  134. idField, ok := lo.Find(coll.Schema.Fields, func(field *entity.Field) bool {
  135. return field.ID == 100
  136. })
  137. s.Require().True(ok)
  138. s.Equal("ID", idField.Name)
  139. s.Equal(entity.FieldTypeInt64, idField.DataType)
  140. s.True(idField.AutoID)
  141. vectorField, ok := lo.Find(coll.Schema.Fields, func(field *entity.Field) bool {
  142. return field.ID == 101
  143. })
  144. s.Require().True(ok)
  145. s.Equal("vector", vectorField.Name)
  146. s.Equal(entity.FieldTypeFloatVector, vectorField.DataType)
  147. })
  148. s.Run("failure", func() {
  149. s.mock.EXPECT().DescribeCollection(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  150. _, err := s.client.DescribeCollection(ctx, NewDescribeCollectionOption("test_collection"))
  151. s.Error(err)
  152. })
  153. }
  154. func (s *CollectionSuite) TestHasCollection() {
  155. ctx, cancel := context.WithCancel(context.Background())
  156. defer cancel()
  157. s.Run("success", func() {
  158. s.mock.EXPECT().DescribeCollection(mock.Anything, mock.Anything).Return(&milvuspb.DescribeCollectionResponse{
  159. Status: merr.Success(),
  160. Schema: &schemapb.CollectionSchema{
  161. Name: "test_collection",
  162. Fields: []*schemapb.FieldSchema{
  163. {FieldID: 100, DataType: schemapb.DataType_Int64, AutoID: true, Name: "ID"},
  164. {
  165. FieldID: 101, DataType: schemapb.DataType_FloatVector, Name: "vector",
  166. TypeParams: []*commonpb.KeyValuePair{
  167. {Key: "dim", Value: "128"},
  168. },
  169. },
  170. },
  171. },
  172. CollectionID: 1000,
  173. CollectionName: "test_collection",
  174. }, nil).Once()
  175. has, err := s.client.HasCollection(ctx, NewHasCollectionOption("test_collection"))
  176. s.NoError(err)
  177. s.True(has)
  178. })
  179. s.Run("collection_not_exist", func() {
  180. s.mock.EXPECT().DescribeCollection(mock.Anything, mock.Anything).Return(&milvuspb.DescribeCollectionResponse{
  181. Status: merr.Status(merr.WrapErrCollectionNotFound("test_collection")),
  182. }, nil).Once()
  183. has, err := s.client.HasCollection(ctx, NewHasCollectionOption("test_collection"))
  184. s.NoError(err)
  185. s.False(has)
  186. })
  187. s.Run("failure", func() {
  188. s.mock.EXPECT().DescribeCollection(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  189. _, err := s.client.HasCollection(ctx, NewHasCollectionOption("test_collection"))
  190. s.Error(err)
  191. })
  192. }
  193. func (s *CollectionSuite) TestDropCollection() {
  194. ctx, cancel := context.WithCancel(context.Background())
  195. defer cancel()
  196. s.Run("success", func() {
  197. s.mock.EXPECT().DropCollection(mock.Anything, mock.Anything).Return(merr.Success(), nil).Once()
  198. err := s.client.DropCollection(ctx, NewDropCollectionOption("test_collection"))
  199. s.NoError(err)
  200. })
  201. s.Run("failure", func() {
  202. s.mock.EXPECT().DropCollection(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  203. err := s.client.DropCollection(ctx, NewDropCollectionOption("test_collection"))
  204. s.Error(err)
  205. })
  206. }
  207. func (s *CollectionSuite) TestRenameCollection() {
  208. ctx, cancel := context.WithCancel(context.Background())
  209. defer cancel()
  210. oldName := fmt.Sprintf("test_collection_%s", s.randString(6))
  211. newName := fmt.Sprintf("%s_new", oldName)
  212. s.Run("success", func() {
  213. s.mock.EXPECT().RenameCollection(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, rcr *milvuspb.RenameCollectionRequest) (*commonpb.Status, error) {
  214. s.Equal(oldName, rcr.GetOldName())
  215. s.Equal(newName, rcr.GetNewName())
  216. return merr.Success(), nil
  217. }).Once()
  218. err := s.client.RenameCollection(ctx, NewRenameCollectionOption(oldName, newName))
  219. s.NoError(err)
  220. })
  221. s.Run("failure", func() {
  222. s.mock.EXPECT().RenameCollection(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  223. err := s.client.RenameCollection(ctx, NewRenameCollectionOption(oldName, newName))
  224. s.Error(err)
  225. })
  226. }
  227. func TestCollection(t *testing.T) {
  228. suite.Run(t, new(CollectionSuite))
  229. }