results_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package client
  17. import (
  18. "testing"
  19. "github.com/stretchr/testify/suite"
  20. "github.com/milvus-io/milvus/client/v2/column"
  21. "github.com/milvus-io/milvus/client/v2/entity"
  22. )
  23. type ResultSetSuite struct {
  24. suite.Suite
  25. }
  26. func (s *ResultSetSuite) TestResultsetUnmarshal() {
  27. type MyData struct {
  28. A int64 `milvus:"name:id"`
  29. V []float32 `milvus:"name:vector"`
  30. }
  31. type OtherData struct {
  32. A string `milvus:"name:id"`
  33. V []float32 `milvus:"name:vector"`
  34. }
  35. var (
  36. idData = []int64{1, 2, 3}
  37. vectorData = [][]float32{
  38. {0.1, 0.2},
  39. {0.1, 0.2},
  40. {0.1, 0.2},
  41. }
  42. )
  43. rs := DataSet([]column.Column{
  44. column.NewColumnInt64("id", idData),
  45. column.NewColumnFloatVector("vector", 2, vectorData),
  46. })
  47. err := rs.Unmarshal([]MyData{})
  48. s.Error(err)
  49. receiver := []MyData{}
  50. err = rs.Unmarshal(&receiver)
  51. s.Error(err)
  52. var ptrReceiver []*MyData
  53. err = rs.Unmarshal(&ptrReceiver)
  54. s.NoError(err)
  55. for idx, row := range ptrReceiver {
  56. s.Equal(row.A, idData[idx])
  57. s.Equal(row.V, vectorData[idx])
  58. }
  59. var otherReceiver []*OtherData
  60. err = rs.Unmarshal(&otherReceiver)
  61. s.Error(err)
  62. }
  63. func (s *ResultSetSuite) TestSearchResultUnmarshal() {
  64. type MyData struct {
  65. A int64 `milvus:"name:id"`
  66. V []float32 `milvus:"name:vector"`
  67. }
  68. type OtherData struct {
  69. A string `milvus:"name:id"`
  70. V []float32 `milvus:"name:vector"`
  71. }
  72. var (
  73. idData = []int64{1, 2, 3}
  74. vectorData = [][]float32{
  75. {0.1, 0.2},
  76. {0.1, 0.2},
  77. {0.1, 0.2},
  78. }
  79. )
  80. sr := ResultSet{
  81. sch: entity.NewSchema().
  82. WithField(entity.NewField().WithName("id").WithIsPrimaryKey(true).WithDataType(entity.FieldTypeInt64)).
  83. WithField(entity.NewField().WithName("vector").WithDim(2).WithDataType(entity.FieldTypeFloatVector)),
  84. IDs: column.NewColumnInt64("id", idData),
  85. Fields: DataSet([]column.Column{
  86. column.NewColumnFloatVector("vector", 2, vectorData),
  87. }),
  88. }
  89. err := sr.Unmarshal([]MyData{})
  90. s.Error(err)
  91. receiver := []MyData{}
  92. err = sr.Unmarshal(&receiver)
  93. s.Error(err)
  94. var ptrReceiver []*MyData
  95. err = sr.Unmarshal(&ptrReceiver)
  96. s.NoError(err)
  97. for idx, row := range ptrReceiver {
  98. s.Equal(row.A, idData[idx])
  99. s.Equal(row.V, vectorData[idx])
  100. }
  101. var otherReceiver []*OtherData
  102. err = sr.Unmarshal(&otherReceiver)
  103. s.Error(err)
  104. }
  105. func TestResults(t *testing.T) {
  106. suite.Run(t, new(ResultSetSuite))
  107. }