github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/columns_json_test.go (about) 1 package entity 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/suite" 10 ) 11 12 type ColumnJSONBytesSuite struct { 13 suite.Suite 14 } 15 16 func (s *ColumnJSONBytesSuite) SetupSuite() { 17 rand.Seed(time.Now().UnixNano()) 18 } 19 20 func (s *ColumnJSONBytesSuite) TestAttrMethods() { 21 columnName := fmt.Sprintf("column_jsonbs_%d", rand.Int()) 22 columnLen := 8 + rand.Intn(10) 23 24 v := make([][]byte, columnLen) 25 column := NewColumnJSONBytes(columnName, v).WithIsDynamic(true) 26 27 s.Run("test_meta", func() { 28 ft := FieldTypeJSON 29 s.Equal("JSON", ft.Name()) 30 s.Equal("JSON", ft.String()) 31 pbName, pbType := ft.PbFieldType() 32 s.Equal("JSON", pbName) 33 s.Equal("JSON", pbType) 34 }) 35 36 s.Run("test_column_attribute", func() { 37 s.Equal(columnName, column.Name()) 38 s.Equal(FieldTypeJSON, column.Type()) 39 s.Equal(columnLen, column.Len()) 40 s.EqualValues(v, column.Data()) 41 }) 42 43 s.Run("test_column_field_data", func() { 44 fd := column.FieldData() 45 s.NotNil(fd) 46 s.Equal(fd.GetFieldName(), columnName) 47 }) 48 49 s.Run("test_column_valuer_by_idx", func() { 50 _, err := column.ValueByIdx(-1) 51 s.Error(err) 52 _, err = column.ValueByIdx(columnLen) 53 s.Error(err) 54 for i := 0; i < columnLen; i++ { 55 v, err := column.ValueByIdx(i) 56 s.NoError(err) 57 s.Equal(column.values[i], v) 58 } 59 }) 60 61 s.Run("test_append_value", func() { 62 item := make([]byte, 10) 63 err := column.AppendValue(item) 64 s.NoError(err) 65 s.Equal(columnLen+1, column.Len()) 66 val, err := column.ValueByIdx(columnLen) 67 s.NoError(err) 68 s.Equal(item, val) 69 70 err = column.AppendValue(&struct{ Tag string }{Tag: "abc"}) 71 s.NoError(err) 72 73 err = column.AppendValue(map[string]interface{}{"Value": 123}) 74 s.NoError(err) 75 76 err = column.AppendValue(1) 77 s.Error(err) 78 }) 79 } 80 81 func TestColumnJSONBytes(t *testing.T) { 82 suite.Run(t, new(ColumnJSONBytesSuite)) 83 }