github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/columns_varchar_test.go (about) 1 package entity 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestColumnVarChar(t *testing.T) { 14 rand.Seed(time.Now().UnixNano()) 15 columnName := fmt.Sprintf("column_VarChar_%d", rand.Int()) 16 columnLen := 8 + rand.Intn(10) 17 18 v := make([]string, columnLen) 19 column := NewColumnVarChar(columnName, v) 20 21 t.Run("test meta", func(t *testing.T) { 22 ft := FieldTypeVarChar 23 assert.Equal(t, "VarChar", ft.Name()) 24 assert.Equal(t, "string", ft.String()) 25 pbName, pbType := ft.PbFieldType() 26 assert.Equal(t, "VarChar", pbName) 27 assert.Equal(t, "string", pbType) 28 }) 29 30 t.Run("test column attribute", func(t *testing.T) { 31 assert.Equal(t, columnName, column.Name()) 32 assert.Equal(t, FieldTypeVarChar, column.Type()) 33 assert.Equal(t, columnLen, column.Len()) 34 assert.EqualValues(t, v, column.Data()) 35 }) 36 37 t.Run("test column field data", func(t *testing.T) { 38 fd := column.FieldData() 39 assert.NotNil(t, fd) 40 assert.Equal(t, fd.GetFieldName(), columnName) 41 }) 42 43 t.Run("test column value by idx", func(t *testing.T) { 44 _, err := column.ValueByIdx(-1) 45 assert.NotNil(t, err) 46 _, err = column.ValueByIdx(columnLen) 47 assert.NotNil(t, err) 48 for i := 0; i < columnLen; i++ { 49 v, err := column.ValueByIdx(i) 50 assert.Nil(t, err) 51 assert.Equal(t, column.values[i], v) 52 } 53 }) 54 } 55 56 func TestFieldDataVarCharColumn(t *testing.T) { 57 colLen := rand.Intn(10) + 8 58 name := fmt.Sprintf("fd_VarChar_%d", rand.Int()) 59 fd := &schema.FieldData{ 60 Type: schema.DataType_VarChar, 61 FieldName: name, 62 } 63 64 t.Run("normal usage", func(t *testing.T) { 65 fd.Field = &schema.FieldData_Scalars{ 66 Scalars: &schema.ScalarField{ 67 Data: &schema.ScalarField_StringData{ 68 StringData: &schema.StringArray{ 69 Data: make([]string, colLen), 70 }, 71 }, 72 }, 73 } 74 column, err := FieldDataColumn(fd, 0, colLen) 75 assert.Nil(t, err) 76 assert.NotNil(t, column) 77 78 assert.Equal(t, name, column.Name()) 79 assert.Equal(t, colLen, column.Len()) 80 assert.Equal(t, FieldTypeVarChar, column.Type()) 81 82 var ev string 83 err = column.AppendValue(ev) 84 assert.Equal(t, colLen+1, column.Len()) 85 assert.Nil(t, err) 86 87 err = column.AppendValue(struct{}{}) 88 assert.Equal(t, colLen+1, column.Len()) 89 assert.NotNil(t, err) 90 }) 91 92 t.Run("nil data", func(t *testing.T) { 93 fd.Field = nil 94 _, err := FieldDataColumn(fd, 0, colLen) 95 assert.NotNil(t, err) 96 }) 97 98 t.Run("get all data", func(t *testing.T) { 99 fd.Field = &schema.FieldData_Scalars{ 100 Scalars: &schema.ScalarField{ 101 Data: &schema.ScalarField_StringData{ 102 StringData: &schema.StringArray{ 103 Data: make([]string, colLen), 104 }, 105 }, 106 }, 107 } 108 column, err := FieldDataColumn(fd, 0, -1) 109 assert.Nil(t, err) 110 assert.NotNil(t, column) 111 112 assert.Equal(t, name, column.Name()) 113 assert.Equal(t, colLen, column.Len()) 114 assert.Equal(t, FieldTypeVarChar, column.Type()) 115 }) 116 }