function_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * # Licensed to the LF AI & Data foundation under one
  3. * # or more contributor license agreements. See the NOTICE file
  4. * # distributed with this work for additional information
  5. * # regarding copyright ownership. The ASF licenses this file
  6. * # to you under the Apache License, Version 2.0 (the
  7. * # "License"); you may not use this file except in compliance
  8. * # with the License. You may obtain a copy of the License at
  9. * #
  10. * # http://www.apache.org/licenses/LICENSE-2.0
  11. * #
  12. * # Unless required by applicable law or agreed to in writing, software
  13. * # distributed under the License is distributed on an "AS IS" BASIS,
  14. * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * # See the License for the specific language governing permissions and
  16. * # limitations under the License.
  17. */
  18. package function
  19. import (
  20. "testing"
  21. "github.com/stretchr/testify/suite"
  22. "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
  23. )
  24. func TestFunctionRunnerSuite(t *testing.T) {
  25. suite.Run(t, new(FunctionRunnerSuite))
  26. }
  27. type FunctionRunnerSuite struct {
  28. suite.Suite
  29. schema *schemapb.CollectionSchema
  30. }
  31. func (s *FunctionRunnerSuite) SetupTest() {
  32. s.schema = &schemapb.CollectionSchema{
  33. Name: "test",
  34. Fields: []*schemapb.FieldSchema{
  35. {FieldID: 100, Name: "int64", DataType: schemapb.DataType_Int64},
  36. {FieldID: 101, Name: "text", DataType: schemapb.DataType_VarChar},
  37. {FieldID: 102, Name: "sparse", DataType: schemapb.DataType_SparseFloatVector},
  38. },
  39. }
  40. }
  41. func (s *FunctionRunnerSuite) TestBM25() {
  42. _, err := NewFunctionRunner(s.schema, &schemapb.FunctionSchema{
  43. Name: "test",
  44. Type: schemapb.FunctionType_BM25,
  45. InputFieldIds: []int64{101},
  46. })
  47. s.Error(err)
  48. runner, err := NewFunctionRunner(s.schema, &schemapb.FunctionSchema{
  49. Name: "test",
  50. Type: schemapb.FunctionType_BM25,
  51. InputFieldIds: []int64{101},
  52. OutputFieldIds: []int64{102},
  53. })
  54. s.NoError(err)
  55. // test batch function run
  56. output, err := runner.BatchRun([]string{"test string", "test string 2"})
  57. s.NoError(err)
  58. s.Equal(1, len(output))
  59. result, ok := output[0].(*schemapb.SparseFloatArray)
  60. s.True(ok)
  61. s.Equal(2, len(result.GetContents()))
  62. // return error because receive more than one field input
  63. _, err = runner.BatchRun([]string{}, []string{})
  64. s.Error(err)
  65. // return error because field not string
  66. _, err = runner.BatchRun([]int64{})
  67. s.Error(err)
  68. }