github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/test/testcases/collection_test.go (about) 1 //go:build L0 2 3 package testcases 4 5 import ( 6 "fmt" 7 "log" 8 "testing" 9 "time" 10 11 "github.com/milvus-io/milvus-sdk-go/v2/client" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/milvus-io/milvus-sdk-go/v2/entity" 16 17 "github.com/milvus-io/milvus-sdk-go/v2/test/common" 18 ) 19 20 // test create default floatVec and binaryVec collection 21 func TestCreateCollection(t *testing.T) { 22 t.Parallel() 23 ctx := createContext(t, time.Second*common.DefaultTimeout) 24 mc := createMilvusClient(ctx, t) 25 26 // prepare 27 defaultFields := [][]*entity.Field{ 28 common.GenDefaultFields(false), 29 common.GenDefaultBinaryFields(false, common.DefaultDim), 30 common.GenDefaultVarcharFields(false), 31 } 32 for _, fields := range defaultFields { 33 collName := common.GenRandomString(6) 34 schema := common.GenSchema(collName, false, fields) 35 36 // create collection 37 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 38 common.CheckErr(t, errCreateCollection, true) 39 40 // check describe collection 41 collection, _ := mc.DescribeCollection(ctx, collName) 42 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 43 44 // check collName in ListCollections 45 collections, errListCollection := mc.ListCollections(ctx) 46 common.CheckErr(t, errListCollection, true) 47 common.CheckContainsCollection(t, collections, collName) 48 } 49 } 50 51 func TestCreateAutoIdCollection(t *testing.T) { 52 t.Skipf("issue: %v", "https://github.com/milvus-io/milvus-sdk-go/issues/343") 53 t.Parallel() 54 ctx := createContext(t, time.Second*common.DefaultTimeout) 55 mc := createMilvusClient(ctx, t) 56 57 // prepare 58 defaultFields := [][]*entity.Field{ 59 common.GenDefaultFields(true), 60 common.GenDefaultBinaryFields(true, common.DefaultDim), 61 common.GenDefaultVarcharFields(true), 62 } 63 for _, fields := range defaultFields { 64 collName := common.GenRandomString(6) 65 schema := common.GenSchema(collName, true, fields) 66 67 // create collection 68 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 69 common.CheckErr(t, errCreateCollection, true) 70 71 // check describe collection 72 collection, _ := mc.DescribeCollection(ctx, collName) 73 log.Printf("collection schema autoid: %v", collection.Schema.AutoID) 74 log.Printf("collection pk field autoid: %v", collection.Schema.Fields[0].AutoID) 75 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 76 77 // check collName in ListCollections 78 collections, errListCollection := mc.ListCollections(ctx) 79 common.CheckErr(t, errListCollection, true) 80 common.CheckContainsCollection(t, collections, collName) 81 } 82 } 83 84 // test create collection with invalid collection and field name 85 func TestCreateCollectionInvalidName(t *testing.T) { 86 t.Parallel() 87 ctx := createContext(t, time.Second*common.DefaultTimeout) 88 mc := createMilvusClient(ctx, t) 89 90 fields := common.GenDefaultFields(false) 91 type invalidNameStruct struct { 92 name string 93 errMsg string 94 } 95 96 invalidNames := []invalidNameStruct{ 97 {name: "", errMsg: "not be empty"}, 98 {name: "12-s", errMsg: "name must be an underscore or letter"}, 99 {name: "(mn)", errMsg: "name must be an underscore or letter"}, 100 {name: "中文", errMsg: "name must be an underscore or letter"}, 101 {name: "%$#", errMsg: "name must be an underscore or letter"}, 102 {name: common.GenLongString(common.MaxCollectionNameLen + 1), errMsg: "name must be less than 255 characters"}, 103 } 104 105 for _, invalidName := range invalidNames { 106 schema := &entity.Schema{ 107 CollectionName: invalidName.name, 108 AutoID: false, 109 Fields: fields, 110 } 111 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 112 common.CheckErr(t, errCreateCollection, false, invalidName.errMsg) 113 } 114 115 for _, invalidName := range invalidNames { 116 field := common.GenField(invalidName.name, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)) 117 if invalidName.name == "" { 118 field.WithName("") 119 } 120 invalidField := []*entity.Field{ 121 field, 122 common.GenField(common.DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, common.WithDim(common.DefaultDim)), 123 } 124 schema := &entity.Schema{ 125 CollectionName: common.GenRandomString(6), 126 AutoID: false, 127 Fields: invalidField, 128 } 129 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 130 common.CheckErr(t, errCreateCollection, false, invalidName.errMsg) 131 } 132 } 133 134 // test create collection with nil fields and nil schema 135 func TestCreateCollectionWithNil(t *testing.T) { 136 ctx := createContext(t, time.Second*common.DefaultTimeout) 137 mc := createMilvusClient(ctx, t) 138 139 // create collection with nil schema 140 errCreateCollection := mc.CreateCollection(ctx, nil, common.DefaultShards) 141 common.CheckErr(t, errCreateCollection, false, "nil schema") 142 143 // create collection with nil fields 144 schema := common.GenSchema(common.GenRandomString(6), true, nil) 145 errCreateCollection2 := mc.CreateCollection(ctx, schema, common.DefaultShards) 146 common.CheckErr(t, errCreateCollection2, false, "vector field not set") 147 } 148 149 // test create collection with invalid fields: without pk, without vec field, multi pk field 150 // TODO multi vector field 151 func TestCreateCollectionInvalidFields(t *testing.T) { 152 t.Parallel() 153 ctx := createContext(t, time.Second*common.DefaultTimeout) 154 mc := createMilvusClient(ctx, t) 155 156 type invalidFieldsStruct struct { 157 fields []*entity.Field 158 errMsg string 159 } 160 invalidFields := []invalidFieldsStruct{ 161 // create collection without pk field 162 {fields: []*entity.Field{common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim))}, 163 errMsg: "primary key is not specified"}, 164 165 // create collection without vector field 166 {fields: []*entity.Field{common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true))}, 167 errMsg: "vector field not set"}, 168 169 // create collection with multi pk fields 170 {fields: []*entity.Field{ 171 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 172 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(true)), 173 common.GenField(common.DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, common.WithDim(common.DefaultDim)), 174 }, errMsg: "only one primary key only"}, 175 176 // create collection with multi vector fields 177 //{fields: []*entity.Field{ 178 // common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 179 // common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 180 // common.GenField(common.DefaultBinaryVecFieldName, entity.FieldTypeBinaryVector, common.WithDim(common.DefaultDim)), 181 //}, errMsg: "multiple vector fields is not supported"}, 182 183 // create collection with None field type 184 {fields: []*entity.Field{ 185 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 186 common.GenField("", entity.FieldTypeNone), 187 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 188 }, errMsg: "data type None is not valid"}, 189 190 // create collection with String field type 191 {fields: []*entity.Field{ 192 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 193 common.GenField("", entity.FieldTypeString), 194 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 195 }, errMsg: "string data type not supported yet, please use VarChar type instead"}, 196 197 // varchar field not specify max_length 198 {fields: []*entity.Field{ 199 common.GenField(common.DefaultVarcharFieldName, entity.FieldTypeVarChar, common.WithIsPrimaryKey(true)), 200 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 201 }, errMsg: "type param(max_length) should be specified for varChar field"}, 202 } 203 204 for _, invalidField := range invalidFields { 205 schema := common.GenSchema(common.GenRandomString(6), false, invalidField.fields) 206 errWithoutPk := mc.CreateCollection(ctx, schema, common.DefaultShards) 207 common.CheckErr(t, errWithoutPk, false, invalidField.errMsg) 208 } 209 } 210 211 func TestCreateCollectionNonInt64AutoField(t *testing.T) { 212 t.Parallel() 213 ctx := createContext(t, time.Second*common.DefaultTimeout) 214 mc := createMilvusClient(ctx, t) 215 216 invalidPkFields := []entity.FieldType{ 217 entity.FieldTypeBool, 218 entity.FieldTypeInt8, 219 entity.FieldTypeInt16, 220 entity.FieldTypeInt32, 221 entity.FieldTypeFloat, 222 entity.FieldTypeDouble, 223 // entity.FieldTypeVarChar, 224 entity.FieldTypeString, 225 entity.FieldTypeNone, 226 entity.FieldTypeJSON, 227 } 228 for _, fieldType := range invalidPkFields { 229 fields := []*entity.Field{ 230 common.GenField(common.DefaultIntFieldName, fieldType, common.WithIsPrimaryKey(true), common.WithAutoID(true)), 231 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 232 } 233 schema := common.GenSchema(common.GenRandomString(6), true, fields) 234 errNonInt64Field := mc.CreateCollection(ctx, schema, common.DefaultShards) 235 common.CheckErr(t, errNonInt64Field, false, "only int64 and varchar column can be primary key for now") 236 } 237 } 238 239 // test create collection with duplicate field name 240 func TestCreateCollectionDuplicateField(t *testing.T) { 241 ctx := createContext(t, time.Second*common.DefaultTimeout) 242 mc := createMilvusClient(ctx, t) 243 244 // duplicate field 245 fields := []*entity.Field{ 246 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 247 common.GenField(common.DefaultFloatFieldName, entity.FieldTypeFloat), 248 common.GenField(common.DefaultFloatFieldName, entity.FieldTypeFloat), 249 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 250 } 251 252 collName := common.GenRandomString(6) 253 schema := common.GenSchema(collName, false, fields) 254 errDupField := mc.CreateCollection(ctx, schema, common.DefaultShards) 255 common.CheckErr(t, errDupField, false, "duplicated field name") 256 } 257 258 // test create collection with invalid pk field type 259 func TestCreateCollectionInvalidPkType(t *testing.T) { 260 ctx := createContext(t, time.Second*common.DefaultTimeout) 261 mc := createMilvusClient(ctx, t) 262 263 invalidPkFields := []entity.FieldType{ 264 entity.FieldTypeBool, 265 entity.FieldTypeInt8, 266 entity.FieldTypeInt16, 267 entity.FieldTypeInt32, 268 entity.FieldTypeFloat, 269 entity.FieldTypeDouble, 270 entity.FieldTypeString, 271 entity.FieldTypeNone, 272 entity.FieldTypeJSON, 273 } 274 for _, fieldType := range invalidPkFields { 275 fields := []*entity.Field{ 276 common.GenField("invalid", fieldType, common.WithIsPrimaryKey(true)), 277 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 278 } 279 schema := common.GenSchema(common.GenRandomString(6), false, fields) 280 errWithoutPk := mc.CreateCollection(ctx, schema, common.DefaultShards) 281 common.CheckErr(t, errWithoutPk, false, "only int64 and varchar column can be primary key for now") 282 } 283 } 284 285 // test create collection with partition key not supported field type 286 func TestCreateCollectionInvalidPartitionKeyType(t *testing.T) { 287 ctx := createContext(t, time.Second*common.DefaultTimeout) 288 mc := createMilvusClient(ctx, t) 289 290 invalidPkFields := []entity.FieldType{ 291 entity.FieldTypeBool, 292 entity.FieldTypeInt8, 293 entity.FieldTypeInt16, 294 entity.FieldTypeInt32, 295 entity.FieldTypeFloat, 296 entity.FieldTypeDouble, 297 entity.FieldTypeJSON, 298 } 299 pkField := common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)) 300 for _, fieldType := range invalidPkFields { 301 fields := []*entity.Field{ 302 pkField, 303 common.GenField("invalid", fieldType, common.WithIsPartitionKey(true)), 304 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 305 } 306 schema := common.GenSchema(common.GenRandomString(6), false, fields) 307 errWithoutPk := mc.CreateCollection(ctx, schema, common.DefaultShards) 308 common.CheckErr(t, errWithoutPk, false, "the data type of partition key should be Int64 or VarChar") 309 } 310 } 311 312 // test create collection with multi auto id 313 func TestCreateCollectionMultiAutoId(t *testing.T) { 314 ctx := createContext(t, time.Second*common.DefaultTimeout) 315 mc := createMilvusClient(ctx, t) 316 317 fields := []*entity.Field{ 318 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(true)), 319 common.GenField("dupInt", entity.FieldTypeInt64, common.WithAutoID(true)), 320 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 321 } 322 schema := common.GenSchema(common.GenRandomString(6), false, fields) 323 errWithoutPk := mc.CreateCollection(ctx, schema, common.DefaultShards) 324 common.CheckErr(t, errWithoutPk, false, "only one auto id is available") 325 } 326 327 // test create collection with different autoId between pk field and schema 328 func TestCreateCollectionInconsistentAutoId(t *testing.T) { 329 t.Skipf("Issue: %s", "https://github.com/milvus-io/milvus-sdk-go/issues/342") 330 ctx := createContext(t, time.Second*common.DefaultTimeout) 331 mc := createMilvusClient(ctx, t) 332 333 fields := []*entity.Field{ 334 // autoId true 335 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(true)), 336 common.GenField(common.DefaultFloatVecFieldName, entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)), 337 } 338 // autoId false 339 collName := common.GenRandomString(6) 340 schema := common.GenSchema(collName, false, fields) 341 errWithoutPk := mc.CreateCollection(ctx, schema, common.DefaultShards) 342 common.CheckErr(t, errWithoutPk, true, "only one auto id is available") 343 collection, _ := mc.DescribeCollection(ctx, collName) 344 log.Printf("collection schema AutoID is %v)", collection.Schema.AutoID) 345 log.Printf("collection pk field AutoID is %v)", collection.Schema.Fields[0].AutoID) 346 } 347 348 // test create collection with field description and schema description 349 func TestCreateCollectionDescription(t *testing.T) { 350 ctx := createContext(t, time.Second*common.DefaultTimeout) 351 mc := createMilvusClient(ctx, t) 352 353 // gen field with description 354 pkField := common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), 355 common.WithFieldDescription("pk field")) 356 vecField := common.GenField("", entity.FieldTypeFloatVector, common.WithDim(common.DefaultDim)) 357 var fields = []*entity.Field{ 358 pkField, vecField, 359 } 360 schema := &entity.Schema{ 361 CollectionName: common.GenRandomString(6), 362 AutoID: false, 363 Fields: fields, 364 Description: "schema", 365 } 366 errCreate := mc.CreateCollection(ctx, schema, common.DefaultShards) 367 common.CheckErr(t, errCreate, true) 368 369 collection, _ := mc.DescribeCollection(ctx, schema.CollectionName) 370 require.Equal(t, collection.Schema.Description, "schema") 371 require.Equal(t, collection.Schema.Fields[0].Description, "pk field") 372 } 373 374 // test create collection with invalid dim 375 func TestCreateBinaryCollectionInvalidDim(t *testing.T) { 376 t.Parallel() 377 type invalidDimStruct struct { 378 dim string 379 errMsg string 380 } 381 invalidDims := []invalidDimStruct{ 382 {dim: "10", errMsg: "should be multiple of 8"}, 383 {dim: "0", errMsg: "should be in range 2 ~ 32768"}, 384 {dim: "1", errMsg: "should be in range 2 ~ 32768"}, 385 {dim: "", errMsg: "invalid syntax"}, 386 {dim: "中文", errMsg: "invalid syntax"}, 387 {dim: "%$#", errMsg: "invalid syntax"}, 388 {dim: fmt.Sprintf("%d", common.MaxDim*9), errMsg: "binary vector dimension should be in range 2 ~ 262144"}, 389 {dim: fmt.Sprintf("%d", common.MaxDim*8+1), errMsg: "binary vector dimension should be multiple of 8"}, 390 } 391 392 // connect 393 ctx := createContext(t, time.Second*common.DefaultTimeout) 394 mc := createMilvusClient(ctx, t) 395 396 // create binary collection with autoID true 397 pkField := common.GenField("", entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(true)) 398 for _, invalidDim := range invalidDims { 399 collName := common.GenRandomString(6) 400 binaryFields := entity.NewField(). 401 WithName(common.DefaultFloatVecFieldName). 402 WithDataType(entity.FieldTypeBinaryVector). 403 WithTypeParams(entity.TypeParamDim, invalidDim.dim) 404 schema := common.GenSchema(collName, true, []*entity.Field{pkField, binaryFields}) 405 errCreate := mc.CreateCollection(ctx, schema, common.DefaultShards) 406 common.CheckErr(t, errCreate, false, invalidDim.errMsg) 407 } 408 } 409 410 func TestCreateFloatCollectionInvalidDim(t *testing.T) { 411 t.Parallel() 412 type invalidDimStruct struct { 413 dim string 414 errMsg string 415 } 416 invalidDims := []invalidDimStruct{ 417 {dim: "0", errMsg: "should be in range 2 ~ 32768"}, 418 {dim: "1", errMsg: "should be in range 2 ~ 32768"}, 419 {dim: "", errMsg: "invalid syntax"}, 420 {dim: "中文", errMsg: "invalid syntax"}, 421 {dim: "%$#", errMsg: "invalid syntax"}, 422 {dim: fmt.Sprintf("%d", common.MaxDim+1), errMsg: "float vector dimension should be in range 2 ~ 32768"}, 423 } 424 425 // connect 426 ctx := createContext(t, time.Second*common.DefaultTimeout) 427 mc := createMilvusClient(ctx, t) 428 429 // create binary collection with autoID true 430 pkField := common.GenField("", entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(true)) 431 for _, invalidDim := range invalidDims { 432 collName := common.GenRandomString(6) 433 binaryFields := entity.NewField(). 434 WithName(common.DefaultFloatVecFieldName). 435 WithDataType(entity.FieldTypeFloatVector). 436 WithTypeParams(entity.TypeParamDim, invalidDim.dim) 437 schema := common.GenSchema(collName, true, []*entity.Field{pkField, binaryFields}) 438 errCreate := mc.CreateCollection(ctx, schema, common.DefaultShards) 439 common.CheckErr(t, errCreate, false, invalidDim.errMsg) 440 } 441 } 442 443 func TestCreateJsonCollection(t *testing.T) { 444 ctx := createContext(t, time.Second*common.DefaultTimeout) 445 mc := createMilvusClient(ctx, t) 446 447 // fields 448 fields := common.GenDefaultFields(true) 449 for i := 0; i < 2; i++ { 450 jsonField1 := common.GenField("", entity.FieldTypeJSON) 451 fields = append(fields, jsonField1) 452 } 453 454 // schema 455 collName := common.GenRandomString(6) 456 schema := common.GenSchema(collName, false, fields) 457 458 // create collection 459 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 460 common.CheckErr(t, err, true) 461 462 // check describe collection 463 collection, _ := mc.DescribeCollection(ctx, collName) 464 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 465 466 // check collName in ListCollections 467 collections, errListCollection := mc.ListCollections(ctx) 468 common.CheckErr(t, errListCollection, true) 469 common.CheckContainsCollection(t, collections, collName) 470 } 471 472 // test create collection enable dynamic field 473 func TestCreateCollectionDynamicSchema(t *testing.T) { 474 ctx := createContext(t, time.Second*common.DefaultTimeout) 475 mc := createMilvusClient(ctx, t) 476 477 collName := common.GenRandomString(6) 478 schema := common.GenSchema(collName, false, common.GenDefaultFields(false), common.WithEnableDynamicField(true)) 479 480 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 481 common.CheckErr(t, err, true) 482 483 // check describe collection 484 collection, _ := mc.DescribeCollection(ctx, collName) 485 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 486 require.Truef(t, collection.Schema.EnableDynamicField, "Expected collection.Schema.EnableDynamicField is True") 487 488 // check collName in ListCollections 489 collections, errListCollection := mc.ListCollections(ctx) 490 common.CheckErr(t, errListCollection, true) 491 common.CheckContainsCollection(t, collections, collName) 492 493 // insert data 494 dp := DataParams{CollectionName: collName, PartitionName: "", CollectionFieldsType: Int64FloatVec, 495 start: 0, nb: common.DefaultNb, dim: common.DefaultDim, EnableDynamicField: true, WithRows: false} 496 _, err = insertData(ctx, t, mc, dp) 497 common.CheckErr(t, err, true) 498 } 499 500 // test create collection enable dynamic field by collection opt 501 func TestCreateCollectionDynamic(t *testing.T) { 502 ctx := createContext(t, time.Second*common.DefaultTimeout) 503 mc := createMilvusClient(ctx, t) 504 505 collName := common.GenRandomString(6) 506 schema := common.GenSchema(collName, false, common.GenDefaultFields(false)) 507 508 err := mc.CreateCollection(ctx, schema, common.DefaultShards, client.WithEnableDynamicSchema(true)) 509 common.CheckErr(t, err, true) 510 511 // check describe collection 512 collection, _ := mc.DescribeCollection(ctx, collName) 513 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 514 require.Truef(t, collection.Schema.EnableDynamicField, "Expected collection.Schema.EnableDynamicField is True") 515 516 // check collName in ListCollections 517 collections, errListCollection := mc.ListCollections(ctx) 518 common.CheckErr(t, errListCollection, true) 519 common.CheckContainsCollection(t, collections, collName) 520 521 // insert data 522 dp := DataParams{CollectionName: collName, PartitionName: "", CollectionFieldsType: Int64FloatVec, 523 start: 0, nb: common.DefaultNb, dim: common.DefaultDim, EnableDynamicField: true, WithRows: false} 524 _, err = insertData(ctx, t, mc, dp) 525 common.CheckErr(t, err, true) 526 } 527 528 // test create collection contains field name: $meta -> error 529 func TestCreateCollectionFieldMeta(t *testing.T) { 530 ctx := createContext(t, time.Second*common.DefaultTimeout) 531 mc := createMilvusClient(ctx, t) 532 533 collName := common.GenRandomString(6) 534 fields := common.GenDefaultFields(true) 535 fields = append(fields, common.GenField(common.DefaultDynamicFieldName, entity.FieldTypeJSON)) 536 schema := common.GenSchema(collName, false, fields, common.WithEnableDynamicField(true)) 537 538 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 539 common.CheckErr(t, err, false, fmt.Sprintf("Invalid field name: %s. The first character of a field name must be an underscore or letter", common.DefaultDynamicFieldName)) 540 } 541 542 func TestCreateArrayFieldInvalidCapacity(t *testing.T) { 543 ctx := createContext(t, time.Second*common.DefaultTimeout) 544 mc := createMilvusClient(ctx, t) 545 546 fields := common.GenDefaultFields(true) 547 // array field no Capacity 548 arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(entity.FieldTypeFloat)) 549 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, arrayField), common.WithEnableDynamicField(true)) 550 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 551 common.CheckErr(t, err, false, "type param(max_capacity) should be specified for array field") 552 553 // invalid Capacity 554 for _, invalidCapacity := range []int64{-1, 0, common.MaxCapacity + 1} { 555 arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(entity.FieldTypeFloat), common.WithMaxCapacity(invalidCapacity)) 556 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, arrayField), common.WithEnableDynamicField(true)) 557 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 558 common.CheckErr(t, err, false, "the maximum capacity specified for a Array should be in (0, 4096]") 559 } 560 } 561 562 // test create collection varchar array with invalid max length 563 func TestCreateVarcharArrayInvalidLength(t *testing.T) { 564 ctx := createContext(t, time.Second*common.DefaultTimeout) 565 mc := createMilvusClient(ctx, t) 566 567 fields := common.GenDefaultFields(true) 568 varcharArrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(entity.FieldTypeVarChar), common.WithMaxCapacity(100)) 569 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, varcharArrayField), common.WithEnableDynamicField(true)) 570 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 571 common.CheckErr(t, err, false, "type param(max_length) should be specified for varChar field") 572 573 // invalid max length 574 for _, invalidLength := range []int64{-1, 0, common.MaxLength + 1} { 575 arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(entity.FieldTypeVarChar), 576 common.WithMaxCapacity(100), common.WithMaxLength(invalidLength)) 577 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, arrayField), common.WithEnableDynamicField(true)) 578 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 579 common.CheckErr(t, err, false, "the maximum length specified for a VarChar should be in (0, 65535]: invalid parameter") 580 } 581 } 582 583 // test create collection array field not supported type 584 func TestCreateArrayNotSupportedFieldType(t *testing.T) { 585 t.Parallel() 586 ctx := createContext(t, time.Second*common.DefaultTimeout) 587 mc := createMilvusClient(ctx, t) 588 589 // not supported ElementType: Array, Json, FloatVector, BinaryVector 590 fields := common.GenDefaultFields(true) 591 for _, fieldType := range []entity.FieldType{entity.FieldTypeArray, entity.FieldTypeJSON, entity.FieldTypeBinaryVector, entity.FieldTypeFloatVector} { 592 arrayField := common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(fieldType), common.WithMaxCapacity(100)) 593 594 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, arrayField), common.WithEnableDynamicField(true)) 595 596 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 597 common.CheckErr(t, err, false, fmt.Sprintf("element type %s is not supported", fieldType.Name())) 598 } 599 600 // NoneType ElementType 601 noneArrayFields := []*entity.Field{ 602 common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithElementType(entity.FieldTypeNone), common.WithMaxCapacity(100)), 603 common.GenField(common.DefaultArrayFieldName, entity.FieldTypeArray, common.WithMaxCapacity(100)), 604 } 605 for _, noneArrayField := range noneArrayFields { 606 schema := common.GenSchema(common.GenRandomString(6), false, append(fields, noneArrayField), common.WithEnableDynamicField(true)) 607 err := mc.CreateCollection(ctx, schema, common.DefaultShards) 608 common.CheckErr(t, err, false, "element data type None is not valid") 609 } 610 } 611 612 func TestCreateCollectionAllFields(t *testing.T) { 613 ctx := createContext(t, time.Second*common.DefaultTimeout) 614 mc := createMilvusClient(ctx, t) 615 allFields := common.GenAllFields() 616 collName := common.GenRandomString(6) 617 schema := common.GenSchema(collName, false, allFields) 618 619 // create collection 620 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 621 common.CheckErr(t, errCreateCollection, true) 622 } 623 624 // the num of vector fields > default limit=4 625 func TestCreateMultiVectorExceed(t *testing.T) { 626 ctx := createContext(t, time.Second*common.DefaultTimeout) 627 mc := createMilvusClient(ctx, t) 628 fields := []*entity.Field{ 629 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true)), 630 } 631 for i := 0; i < common.MaxVectorFieldNum+1; i++ { 632 field := common.GenField(fmt.Sprintf("%s-%d", common.DefaultFloatVecFieldName, i), entity.FieldTypeFloatVector, common.WithDim(128)) 633 fields = append(fields, field) 634 } 635 collName := common.GenRandomString(6) 636 schema := common.GenSchema(collName, false, fields) 637 638 // create collection 639 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 640 common.CheckErr(t, errCreateCollection, false, "maximum vector field's number should be limited to 4") 641 } 642 643 // specify dim for sparse vector -> error 644 func TestCreateCollectionSparseVectorWithDim(t *testing.T) { 645 ctx := createContext(t, time.Second*common.DefaultTimeout) 646 mc := createMilvusClient(ctx, t) 647 allFields := []*entity.Field{ 648 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(false)), 649 common.GenField(common.DefaultSparseVecFieldName, entity.FieldTypeSparseVector, common.WithDim(common.DefaultDim)), 650 } 651 collName := common.GenRandomString(6) 652 schema := common.GenSchema(collName, false, allFields) 653 654 // create collection 655 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 656 common.CheckErr(t, errCreateCollection, false, "dim should not be specified for sparse vector field sparseVec(0)") 657 } 658 659 // create collection with sparse vector 660 func TestCreateCollectionSparseVector(t *testing.T) { 661 ctx := createContext(t, time.Second*common.DefaultTimeout) 662 mc := createMilvusClient(ctx, t) 663 allFields := []*entity.Field{ 664 common.GenField(common.DefaultIntFieldName, entity.FieldTypeInt64, common.WithIsPrimaryKey(true), common.WithAutoID(false)), 665 common.GenField(common.DefaultVarcharFieldName, entity.FieldTypeVarChar, common.WithMaxLength(common.TestMaxLen)), 666 common.GenField(common.DefaultSparseVecFieldName, entity.FieldTypeSparseVector), 667 } 668 collName := common.GenRandomString(6) 669 schema := common.GenSchema(collName, false, allFields) 670 671 // create collection 672 errCreateCollection := mc.CreateCollection(ctx, schema, common.DefaultShards) 673 common.CheckErr(t, errCreateCollection, true) 674 675 // describe collection 676 collection, err := mc.DescribeCollection(ctx, collName) 677 common.CheckErr(t, err, true) 678 common.CheckCollection(t, collection, collName, common.DefaultShards, schema, common.DefaultConsistencyLevel) 679 require.Len(t, collection.Schema.Fields, 3) 680 for _, field := range collection.Schema.Fields { 681 if field.DataType == entity.FieldTypeSparseVector { 682 require.Equal(t, common.DefaultSparseVecFieldName, field.Name) 683 } 684 } 685 } 686 687 // -- Get Collection Statistics -- 688 689 func TestGetStaticsCollectionNotExisted(t *testing.T) { 690 ctx := createContext(t, time.Second*common.DefaultTimeout) 691 // connect 692 mc := createMilvusClient(ctx, t) 693 694 // flush and check row count 695 _, errStatist := mc.GetCollectionStatistics(ctx, "collName") 696 common.CheckErr(t, errStatist, false, "collection collName does not exist") 697 }