github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/schema_test.go (about) 1 package entity 2 3 import ( 4 "testing" 5 6 common "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/suite" 9 ) 10 11 func TestCL_CommonCL(t *testing.T) { 12 cls := []ConsistencyLevel{ 13 ClStrong, 14 ClBounded, 15 ClSession, 16 ClEventually, 17 } 18 for _, cl := range cls { 19 assert.EqualValues(t, common.ConsistencyLevel(cl), cl.CommonConsistencyLevel()) 20 } 21 } 22 23 func TestFieldSchema(t *testing.T) { 24 fields := []*Field{ 25 NewField().WithName("int_field").WithDataType(FieldTypeInt64).WithIsAutoID(true).WithIsPrimaryKey(true).WithDescription("int_field desc"), 26 NewField().WithName("string_field").WithDataType(FieldTypeString).WithIsAutoID(false).WithIsPrimaryKey(true).WithIsDynamic(false).WithTypeParams("max_len", "32").WithDescription("string_field desc"), 27 NewField().WithName("partition_key").WithDataType(FieldTypeInt32).WithIsPartitionKey(true), 28 NewField().WithName("array_field").WithDataType(FieldTypeArray).WithElementType(FieldTypeBool).WithMaxCapacity(128), 29 NewField().WithName("fp16_field").WithDataType(FieldTypeFloat16Vector).WithDim(128), 30 NewField().WithName("bf16_field").WithDataType(FieldTypeBFloat16Vector).WithDim(128), 31 /* 32 NewField().WithName("default_value_bool").WithDataType(FieldTypeBool).WithDefaultValueBool(true), 33 NewField().WithName("default_value_int").WithDataType(FieldTypeInt32).WithDefaultValueInt(1), 34 NewField().WithName("default_value_long").WithDataType(FieldTypeInt64).WithDefaultValueLong(1), 35 NewField().WithName("default_value_float").WithDataType(FieldTypeFloat).WithDefaultValueFloat(1), 36 NewField().WithName("default_value_double").WithDataType(FieldTypeDouble).WithDefaultValueDouble(1), 37 NewField().WithName("default_value_string").WithDataType(FieldTypeString).WithDefaultValueString("a"),*/ 38 } 39 40 for _, field := range fields { 41 fieldSchema := field.ProtoMessage() 42 assert.Equal(t, field.ID, fieldSchema.GetFieldID()) 43 assert.Equal(t, field.Name, fieldSchema.GetName()) 44 assert.EqualValues(t, field.DataType, fieldSchema.GetDataType()) 45 assert.Equal(t, field.AutoID, fieldSchema.GetAutoID()) 46 assert.Equal(t, field.PrimaryKey, fieldSchema.GetIsPrimaryKey()) 47 assert.Equal(t, field.IsPartitionKey, fieldSchema.GetIsPartitionKey()) 48 assert.Equal(t, field.IsDynamic, fieldSchema.GetIsDynamic()) 49 assert.Equal(t, field.Description, fieldSchema.GetDescription()) 50 assert.Equal(t, field.TypeParams, KvPairsMap(fieldSchema.GetTypeParams())) 51 assert.EqualValues(t, field.ElementType, fieldSchema.GetElementType()) 52 // marshal & unmarshal, still equals 53 nf := &Field{} 54 nf = nf.ReadProto(fieldSchema) 55 assert.Equal(t, field.ID, nf.ID) 56 assert.Equal(t, field.Name, nf.Name) 57 assert.EqualValues(t, field.DataType, nf.DataType) 58 assert.Equal(t, field.AutoID, nf.AutoID) 59 assert.Equal(t, field.PrimaryKey, nf.PrimaryKey) 60 assert.Equal(t, field.Description, nf.Description) 61 assert.Equal(t, field.IsDynamic, nf.IsDynamic) 62 assert.Equal(t, field.IsPartitionKey, nf.IsPartitionKey) 63 assert.EqualValues(t, field.TypeParams, nf.TypeParams) 64 assert.EqualValues(t, field.ElementType, nf.ElementType) 65 } 66 67 assert.NotPanics(t, func() { 68 (&Field{}).WithTypeParams("a", "b") 69 }) 70 } 71 72 type SchemaSuite struct { 73 suite.Suite 74 } 75 76 func (s *SchemaSuite) TestBasic() { 77 cases := []struct { 78 tag string 79 input *Schema 80 pkName string 81 }{ 82 { 83 "test_collection", 84 NewSchema().WithName("test_collection_1").WithDescription("test_collection_1 desc").WithAutoID(false). 85 WithField(NewField().WithName("ID").WithDataType(FieldTypeInt64).WithIsPrimaryKey(true)). 86 WithField(NewField().WithName("vector").WithDataType(FieldTypeFloatVector).WithDim(128)), 87 "ID", 88 }, 89 { 90 "dynamic_schema", 91 NewSchema().WithName("dynamic_schema").WithDescription("dynamic_schema desc").WithAutoID(true).WithDynamicFieldEnabled(true). 92 WithField(NewField().WithName("ID").WithDataType(FieldTypeVarChar).WithMaxLength(256)). 93 WithField(NewField().WithName("$meta").WithIsDynamic(true)), 94 "", 95 }, 96 } 97 98 for _, c := range cases { 99 s.Run(c.tag, func() { 100 sch := c.input 101 p := sch.ProtoMessage() 102 s.Equal(sch.CollectionName, p.GetName()) 103 s.Equal(sch.AutoID, p.GetAutoID()) 104 s.Equal(sch.Description, p.GetDescription()) 105 s.Equal(sch.EnableDynamicField, p.GetEnableDynamicField()) 106 s.Equal(len(sch.Fields), len(p.GetFields())) 107 108 nsch := &Schema{} 109 nsch = nsch.ReadProto(p) 110 111 s.Equal(sch.CollectionName, nsch.CollectionName) 112 s.Equal(sch.AutoID, nsch.AutoID) 113 s.Equal(sch.Description, nsch.Description) 114 s.Equal(sch.EnableDynamicField, nsch.EnableDynamicField) 115 s.Equal(len(sch.Fields), len(nsch.Fields)) 116 s.Equal(c.pkName, sch.PKFieldName()) 117 s.Equal(c.pkName, nsch.PKFieldName()) 118 }) 119 } 120 } 121 122 func (s *SchemaSuite) TestFp16Vector() { 123 cases := []struct { 124 tag string 125 input *Schema 126 pkName string 127 }{ 128 { 129 "test_collection", 130 NewSchema().WithName("test_collection_1").WithDescription("test_collection_1 desc").WithAutoID(true). 131 WithField(NewField().WithName("fp16_field").WithDataType(FieldTypeFloat16Vector).WithDim(128)). 132 WithField(NewField().WithName("bf16_field").WithDataType(FieldTypeBFloat16Vector).WithDim(128)), 133 "", 134 }, 135 } 136 137 for _, c := range cases { 138 s.Run(c.tag, func() { 139 sch := c.input 140 p := sch.ProtoMessage() 141 s.Equal(sch.CollectionName, p.GetName()) 142 s.Equal(sch.AutoID, p.GetAutoID()) 143 s.Equal(sch.Description, p.GetDescription()) 144 s.Equal(sch.EnableDynamicField, p.GetEnableDynamicField()) 145 s.Equal(len(sch.Fields), len(p.GetFields())) 146 147 nsch := &Schema{} 148 nsch = nsch.ReadProto(p) 149 150 s.Equal(sch.CollectionName, nsch.CollectionName) 151 s.Equal(sch.AutoID, nsch.AutoID) 152 s.Equal(sch.Description, nsch.Description) 153 s.Equal(sch.EnableDynamicField, nsch.EnableDynamicField) 154 s.Equal(len(sch.Fields), len(nsch.Fields)) 155 s.Equal(c.pkName, sch.PKFieldName()) 156 s.Equal(c.pkName, nsch.PKFieldName()) 157 }) 158 } 159 } 160 161 func TestSchema(t *testing.T) { 162 suite.Run(t, new(SchemaSuite)) 163 }