github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/columns_dynamic_test.go (about) 1 package entity 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/suite" 7 ) 8 9 type ColumnDynamicSuite struct { 10 suite.Suite 11 } 12 13 func (s *ColumnDynamicSuite) TestGetInt() { 14 cases := []struct { 15 input string 16 expectErr bool 17 expectValue int64 18 }{ 19 {`{"field": 1000000000000000001}`, false, 1000000000000000001}, 20 {`{"field": 4418489049307132905}`, false, 4418489049307132905}, 21 {`{"other_field": 4418489049307132905}`, true, 0}, 22 {`{"field": "string"}`, true, 0}, 23 } 24 25 for _, c := range cases { 26 s.Run(c.input, func() { 27 column := NewColumnDynamic(&ColumnJSONBytes{ 28 values: [][]byte{[]byte(c.input)}, 29 }, "field") 30 v, err := column.GetAsInt64(0) 31 if c.expectErr { 32 s.Error(err) 33 return 34 } 35 s.NoError(err) 36 s.Equal(c.expectValue, v) 37 }) 38 } 39 } 40 41 func (s *ColumnDynamicSuite) TestGetString() { 42 cases := []struct { 43 input string 44 expectErr bool 45 expectValue string 46 }{ 47 {`{"field": "abc"}`, false, "abc"}, 48 {`{"field": "test"}`, false, "test"}, 49 {`{"other_field": "string"}`, true, ""}, 50 {`{"field": 123}`, true, ""}, 51 } 52 53 for _, c := range cases { 54 s.Run(c.input, func() { 55 column := NewColumnDynamic(&ColumnJSONBytes{ 56 values: [][]byte{[]byte(c.input)}, 57 }, "field") 58 v, err := column.GetAsString(0) 59 if c.expectErr { 60 s.Error(err) 61 return 62 } 63 s.NoError(err) 64 s.Equal(c.expectValue, v) 65 }) 66 } 67 } 68 69 func (s *ColumnDynamicSuite) TestGetBool() { 70 cases := []struct { 71 input string 72 expectErr bool 73 expectValue bool 74 }{ 75 {`{"field": true}`, false, true}, 76 {`{"field": false}`, false, false}, 77 {`{"other_field": true}`, true, false}, 78 {`{"field": "test"}`, true, false}, 79 } 80 81 for _, c := range cases { 82 s.Run(c.input, func() { 83 column := NewColumnDynamic(&ColumnJSONBytes{ 84 values: [][]byte{[]byte(c.input)}, 85 }, "field") 86 v, err := column.GetAsBool(0) 87 if c.expectErr { 88 s.Error(err) 89 return 90 } 91 s.NoError(err) 92 s.Equal(c.expectValue, v) 93 }) 94 } 95 } 96 97 func (s *ColumnDynamicSuite) TestGetDouble() { 98 cases := []struct { 99 input string 100 expectErr bool 101 expectValue float64 102 }{ 103 {`{"field": 1}`, false, 1.0}, 104 {`{"field": 6231.123}`, false, 6231.123}, 105 {`{"other_field": 1.0}`, true, 0}, 106 {`{"field": "string"}`, true, 0}, 107 } 108 109 for _, c := range cases { 110 s.Run(c.input, func() { 111 column := NewColumnDynamic(&ColumnJSONBytes{ 112 values: [][]byte{[]byte(c.input)}, 113 }, "field") 114 v, err := column.GetAsDouble(0) 115 if c.expectErr { 116 s.Error(err) 117 return 118 } 119 s.NoError(err) 120 s.Less(v-c.expectValue, 1e-10) 121 }) 122 } 123 124 } 125 126 func (s *ColumnDynamicSuite) TestIndexOutOfRange() { 127 var err error 128 column := NewColumnDynamic(&ColumnJSONBytes{}, "field") 129 130 s.Equal("field", column.Name()) 131 132 _, err = column.GetAsInt64(0) 133 s.Error(err) 134 135 _, err = column.GetAsString(0) 136 s.Error(err) 137 138 _, err = column.GetAsBool(0) 139 s.Error(err) 140 141 _, err = column.GetAsDouble(0) 142 s.Error(err) 143 } 144 145 func TestColumnDynamic(t *testing.T) { 146 suite.Run(t, new(ColumnDynamicSuite)) 147 }