diff --git a/.github/workflows/test-ci.yaml b/.github/workflows/test-ci.yaml index ac3a44c8..d84cac2d 100644 --- a/.github/workflows/test-ci.yaml +++ b/.github/workflows/test-ci.yaml @@ -73,7 +73,7 @@ jobs: gotestsum --format testname --hide-summary=output ./testcases/... --tags L0 --addr=127.0.0.1:19530 - name: Export logs - if: ${{ always() }} + if: ${{ !success() }} shell: bash working-directory: ci/scripts/ run: | diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 196c4e72..00000000 --- a/tests/README.md +++ /dev/null @@ -1 +0,0 @@ -# Scaffold diff --git a/tests/client_collection_test.go b/tests/client_collection_test.go deleted file mode 100644 index 6fadc4f5..00000000 --- a/tests/client_collection_test.go +++ /dev/null @@ -1,122 +0,0 @@ -package tests - -import ( - "context" - "fmt" - "math/rand" - "testing" - "time" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/milvus-io/milvus-sdk-go/v2/entity" - "github.com/stretchr/testify/assert" -) - -func TestConnect(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } -} - -func TestListCollections(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - // TODO create before list and assert exists - _, err = c.ListCollections(context.Background()) - assert.Nil(t, err) -} - -func TestCreateCollection(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - cname := generateCollectionName() - schema := &entity.Schema{ - CollectionName: cname, - Fields: []*entity.Field{ - { - Name: fmt.Sprintf("int64_%v", rand.Intn(10)), - DataType: entity.FieldTypeInt64, - PrimaryKey: true, - }, - { - Name: "vector", - DataType: entity.FieldTypeFloatVector, - TypeParams: map[string]string{ - entity.TypeParamDim: "128", - }, - }, - }, - } - shards := rand.Int31n(3) - err = c.CreateCollection(context.Background(), schema, shards) - assert.Nil(t, err) -} - -func TestDescribeCollection(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - cname := generateCollectionName() - schema := generateSchema() - generateCollection(t, c, cname, schema, false) - - // TODO merge describe and create - coll, err := c.DescribeCollection(context.Background(), cname) - if assert.Nil(t, err) { - t.Logf("schema -- name: %s\n", coll.Name) - for _, field := range coll.Schema.Fields { - t.Logf("schema -- field: %s data type: %v, is primary: %v\n", field.Name, field.DataType, field.PrimaryKey) - } - } -} - -func TestDropCollection(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - cname := generateCollectionName() - schema := generateSchema() - generateCollection(t, c, cname, schema, false) - - c.DropCollection(context.Background(), cname) -} - -func TestGetCollectionStatistics(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - cname := generateCollectionName() - schema := generateSchema() - generateCollection(t, c, cname, schema, false) - - m, err := c.GetCollectionStatistics(context.Background(), cname) - if assert.Nil(t, err) { - for k, v := range m { - t.Log(k, v) - } - } -} diff --git a/tests/client_dm_test.go b/tests/client_dm_test.go deleted file mode 100644 index b95c0c2f..00000000 --- a/tests/client_dm_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package tests - -import ( - "context" - "testing" - "time" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/milvus-io/milvus-sdk-go/v2/entity" - "github.com/stretchr/testify/assert" -) - -func TestSearch(t *testing.T) { - c := getDefaultClient(t) - if c != nil { - defer c.Close() - } - cname := generateCollectionName() - schema := generateSchema() - vectors := generateCollection(t, c, cname, schema, true) - - waitRowCountChanged(t, c) - c.LoadCollection(context.Background(), cname, false) - sp, _ := entity.NewIndexFlatSearchParam() - results, err := c.Search(context.Background(), cname, []string{}, "int64 > 0", []string{"int64"}, []entity.Vector{entity.FloatVector(vectors[0])}, - testVectorField, entity.L2, 10, sp) - - assert.Nil(t, err) - for _, result := range results { - t.Logf("result count: %d, ids %v fields %v", result.ResultCount, result.IDs, result.Fields) - } -} - -func waitRowCountChanged(t *testing.T, c client.Client) { - cname := generateCollectionName() - schema := generateSchema() - generateCollection(t, c, cname, schema, true) - - start := time.Now() - f := func() bool { - ctx := context.Background() - m, err := c.GetCollectionStatistics(ctx, cname) - assert.Nil(t, err) - for k, v := range m { - t.Log(k, v) - } - return m["row_count"] != "0" - } - for !f() { - time.Sleep(time.Millisecond * 100) - } - t.Log(time.Since(start)) - -} diff --git a/tests/client_index_test.go b/tests/client_index_test.go deleted file mode 100644 index fff8d27c..00000000 --- a/tests/client_index_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package tests - -import ( - "context" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/milvus-io/milvus-sdk-go/v2/entity" - "github.com/stretchr/testify/assert" -) - -func TestCreateIndex(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - cname := generateCollectionName() - schema := generateSchema() - generateCollection(t, c, cname, schema, true) - - idx := entity.NewGenericIndex("", entity.Flat, map[string]string{ - "nlist": "1024", - "metric_type": "IP", - }) - err = c.CreateIndex(context.Background(), cname, "vector", idx, false) - assert.Nil(t, err) - indexes, err := c.DescribeIndex(context.Background(), cname, "vector") - if assert.Nil(t, err) { - for _, idx := range indexes { - t.Log(idx.IndexType()) - t.Log(idx.Params()) - } - } -} diff --git a/tests/client_partition_test.go b/tests/client_partition_test.go deleted file mode 100644 index 965f56c1..00000000 --- a/tests/client_partition_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package tests - -import ( - "context" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/stretchr/testify/assert" -) - -func TestShowPartitions(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - c.ShowPartitions(context.Background(), "list_collections_ggIQr3xO") -} - -func TestCreatePartition(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - c.CreatePartition(context.Background(), "list_collections_ggIQr3xO", "_dim_part") -} - -func TestDropPartition(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } - - c.DropPartition(context.Background(), "list_collections_ggIQr3xO", "_dim_part") -} diff --git a/tests/collection_test.go b/tests/collection_test.go deleted file mode 100644 index 2e12f682..00000000 --- a/tests/collection_test.go +++ /dev/null @@ -1,207 +0,0 @@ -package tests - -import ( - "context" - "fmt" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/entity" - ut "github.com/milvus-io/milvus-sdk-go/v2/tests/testutil" - "github.com/stretchr/testify/assert" -) - -var nameTests = []struct { - name string -}{ - {"123"}, - {"中文"}, - {"(mn)"}, - {"%$#"}, -} - -var dimTests = []struct { - dim int64 -}{ - {-1}, - {0}, - {32769}, -} - -// Test create collection with default schema -func TestClientCreateCollection(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - cname := generateCollectionName() - fields := ut.GenDefaultFields(ut.DefaultDim) - schema := ut.GenSchema(cname, false, fields) - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - assert.NoError(t, err) - isExist, err1 := c.HasCollection(ctx, cname) - assert.NoError(t, err1) - assert.True(t, isExist) -} - -// Test create collection without name, expected an error -func TestCollectionWithoutName(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - fields := ut.GenDefaultFields(ut.DefaultDim) - schema := &entity.Schema{ - AutoID: false, - Fields: fields, - } - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - errorStr := "collection name cannot be empty" - assert.EqualError(t, err, errorStr) -} - -// Test create collection without fields, expected an error -func TestCollectionWithoutFields(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - // fields := ut.GenDefaultFields() - name := ut.GenRandomString(8) - schema := &entity.Schema{ - CollectionName: name, - } - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - errorStr := "vector field not set" - assert.EqualError(t, err, errorStr) -} - -// Test create collection with invalid name, expected an error -func TestCollectionInvalidName(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - fields := ut.GenDefaultFields(ut.DefaultDim) - schema := ut.GenSchema("name", false, fields) - for _, test := range nameTests { - schema.CollectionName = test.name - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - - // TODO Inaccurate error msg - expError := "Invalid collection name" - assert.Contains(t, err.Error(), expError) - } -} - -// TODO issue: #217 -// Test create collection with invalid field name, expected an error -func TestCollectionInvalidFieldName(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - fields := ut.GenDefaultFields(ut.DefaultDim) - collectionName := ut.GenRandomString(8) - for _, test := range nameTests { - fields[0].Name = test.name - t.Logf("field -- name: %s\n", fields[0].Name) - schema := ut.GenSchema(collectionName, false, fields) - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - t.Log(err) - } -} - -// TODO issue: #218 -// Test create autoId collection -func TestCollectionAutoId(t *testing.T) { - t.Skip("issue #218") - - ctx := context.Background() - c := GenClient(t) - name := ut.GenRandomString(8) - t.Logf("exp name: %s", name) - fields := ut.GenDefaultFields(ut.DefaultDim) - schema := ut.GenSchema(name, true, fields) - t.Logf("exp autoId: %t", schema.AutoID) - c.CreateCollection(ctx, schema, ut.DefaultShards) - collection, _ := c.DescribeCollection(ctx, name) - t.Logf("actual name: %s", collection.Name) - t.Logf("actual autoId: %t", collection.Schema.AutoID) - assert.Equal(t, schema.AutoID, collection.Schema.AutoID) -} - -// TODO issue: #219 -// Test create collection only with vector field, expected an error -func TestCollectionOnlyVector(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - name := ut.GenRandomString(8) - var fields = []*entity.Field{ - { - Name: name, - DataType: entity.FieldTypeFloatVector, - TypeParams: map[string]string{ - "dim": fmt.Sprintf("%d", ut.DefaultDim), - }, - }, - } - schema := &entity.Schema{ - CollectionName: name, - Fields: fields, - } - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - t.Log(err) - isExist, _ := c.HasCollection(ctx, name) - t.Log(isExist) -} - -// Test create collection without vector field, expected an error -func TestCollectionWithoutVector(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - fields := []*entity.Field{ - { - Name: ut.DefaultIntFieldName, - DataType: entity.FieldTypeInt64, - PrimaryKey: true, - }, - } - name := ut.GenRandomString(8) - schema := &entity.Schema{ - CollectionName: name, - Fields: fields, - } - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - expError := "vector field not set" - assert.Contains(t, err.Error(), expError) -} - -// Test create collection with invalid dim -// TODO issue: #220 -func TestCollectionInvalidDim(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - for _, test := range dimTests { - name := ut.GenRandomString(8) - fields := ut.GenDefaultFields(test.dim) - t.Log(fields[2].TypeParams["dim"]) - schema := ut.GenSchema(name, false, fields) - err := c.CreateCollection(ctx, schema, ut.DefaultDim) - t.Log(err) - } -} - -// Test create collection with wrong keyword "dim", like "dims", expected an error -// TODO issue: #221 -func TestCollectionWrongDimKey(t *testing.T) { - ctx := context.Background() - c := GenClient(t) - name := ut.GenRandomString(8) - fields := []*entity.Field{ - { - Name: ut.DefaultIntFieldName, - DataType: entity.FieldTypeInt64, - PrimaryKey: true, - }, - { - Name: ut.DefaultFloatVecFieldName, - DataType: entity.FieldTypeFloatVector, - TypeParams: map[string]string{ - "dims": fmt.Sprintf("%d", ut.DefaultDim), - }, - }, - } - schema := ut.GenSchema(name, false, fields) - err := c.CreateCollection(ctx, schema, ut.DefaultShards) - t.Log(err) -} diff --git a/tests/connect_test.go b/tests/connect_test.go deleted file mode 100644 index e21149d3..00000000 --- a/tests/connect_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package tests - -import ( - "context" - "fmt" - "reflect" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/stretchr/testify/assert" -) - -func TestClientConnect(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), GenMilvusAddr()) - assert.NoError(t, err) - tp := reflect.TypeOf(c) - t.Logf("Client type is: %s\n", tp) - //assert.IsType(t, *client.grpcClient, c) - defer c.Close() -} - -func TestConnectRepeatedly(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), GenMilvusAddr()) - assert.NoError(t, err) - c1, err1 := client.NewGrpcClient(context.Background(), GenMilvusAddr()) - assert.NoError(t, err1) - assert.ObjectsAreEqualValues(c, c1) - defer c.Close() -} - -func TestConnectInvalid(t *testing.T) { - addrTests := []struct { - addr string - }{ - {fmt.Sprintf("%s%d", host, port)}, - {host}, - {fmt.Sprintf("%s:-1", host)}, - {fmt.Sprintf("%s:%d_", host, port)}, - {fmt.Sprintf(" :%d_", port)}, - {fmt.Sprintf("中文:%d_", port)}, - } - errString := "context deadline exceeded" - for _, test := range addrTests { - t.Log(test.addr) - _, err := client.NewGrpcClient(context.Background(), test.addr) - assert.EqualError(t, err, errString) - } -} - -func TestClose(t *testing.T) { - c := GenClient(t) - res := c.Close() - assert.NoError(t, res) - _, err1 := c.ListCollections(context.Background()) - errString := "rpc error: code = Canceled desc = grpc: the client connection is closing" - assert.EqualError(t, err1, errString) -} - -func TestCloseRepeatedly(t *testing.T) { - c := GenClient(t) - res := c.Close() - assert.NoError(t, res) - res1 := c.Close() - assert.NoError(t, res1) -} diff --git a/tests/e2e/connect_test.go b/tests/e2e/connect_test.go deleted file mode 100644 index 039afde5..00000000 --- a/tests/e2e/connect_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package e2e - -import ( - "context" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/stretchr/testify/assert" -) - -func TestConnect(t *testing.T) { - c, err := client.NewGrpcClient(context.Background(), "localhost:19530") - assert.Nil(t, err) - assert.NotNil(t, c) - if c != nil { - defer c.Close() - } -} diff --git a/tests/main_test.go b/tests/main_test.go deleted file mode 100644 index 4bb1655c..00000000 --- a/tests/main_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package tests - -import ( - "context" - "flag" - "fmt" - "os" - "testing" - - "github.com/milvus-io/milvus-sdk-go/v2/client" -) - -var host string -var port int64 - -func init() { - flag.StringVar(&host, "host", "localhost", "server host") - flag.Int64Var(&port, "port", 19530, "server port") -} - -// Generate addr to connect Milvus -func GenMilvusAddr() string { - addr := fmt.Sprintf("%s:%d", host, port) - return addr -} - -// Generate an connected Milvus client -func GenClient(t *testing.T) client.Client { - t.Helper() - addr := GenMilvusAddr() - c, err := client.NewGrpcClient(context.Background(), addr) - if err != nil { - t.Errorf("Failed to connect %s\n", addr) - } - return c -} - -func teardown() { - fmt.Println("Start to tear down") - ctx := context.Background() - c, _ := client.NewGrpcClient(context.Background(), GenMilvusAddr()) - defer c.Close() - for _, cname := range testCollections { - c.DropCollection(ctx, cname) - fmt.Printf("Drop collection %s\n", cname) - } -} - -func TestMain(m *testing.M) { - flag.Parse() - code := m.Run() - teardown() - os.Exit(code) -} diff --git a/tests/testutil/common.go b/tests/testutil/common.go deleted file mode 100644 index c23d0a22..00000000 --- a/tests/testutil/common.go +++ /dev/null @@ -1,15 +0,0 @@ -package testutil - -const ( - DefaultIntFieldName = "int64" - - DefaultFloatFieldName = "float" - - DefaultFloatVecFieldName = "float_vec" - - //DefaultBinaryVecFieldName = "binary_vec" - - DefaultDim = 128 - - DefaultShards = 4 -) diff --git a/tests/testutil/utils.go b/tests/testutil/utils.go deleted file mode 100644 index 9c757025..00000000 --- a/tests/testutil/utils.go +++ /dev/null @@ -1,57 +0,0 @@ -package testutil - -import ( - "fmt" - "math/rand" - "time" - - "github.com/milvus-io/milvus-sdk-go/v2/entity" -) - -var r *rand.Rand - -func init() { - r = rand.New(rand.NewSource(time.Now().UnixNano())) -} - -var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - -// GenRandomString returns a batch of random string -func GenRandomString(n int) string { - b := make([]rune, n) - for i := range b { - b[i] = letterRunes[r.Intn(len(letterRunes))] - } - return string(b) -} - -func GenDefaultFields(dim int64) []*entity.Field { - var fields = []*entity.Field{ - { - Name: DefaultIntFieldName, - DataType: entity.FieldTypeInt64, - PrimaryKey: true, - }, - { - Name: DefaultFloatFieldName, - DataType: entity.FieldTypeFloat, - }, - { - Name: DefaultFloatVecFieldName, - DataType: entity.FieldTypeFloatVector, - TypeParams: map[string]string{ - entity.TypeParamDim: fmt.Sprintf("%d", dim), - }, - }, - } - return fields -} - -func GenSchema(name string, autoID bool, fields []*entity.Field) *entity.Schema { - schema := &entity.Schema{ - CollectionName: name, - AutoID: autoID, - Fields: fields, - } - return schema -} diff --git a/tests/util.go b/tests/util.go deleted file mode 100644 index 55a90fee..00000000 --- a/tests/util.go +++ /dev/null @@ -1,107 +0,0 @@ -package tests - -import ( - "context" - "fmt" - "math/rand" - "testing" - "time" - - "github.com/milvus-io/milvus-sdk-go/v2/client" - "github.com/milvus-io/milvus-sdk-go/v2/entity" - ut "github.com/milvus-io/milvus-sdk-go/v2/tests/testutil" - "github.com/stretchr/testify/assert" -) - -var ( - defaultMilvusAddr = `localhost:19530` - - testCollectionPrefix = `test_sdk_go` // collection name used for testing - testPrimaryField = `int64` // default primary key name - testVectorField = `vector` // default vector field name - testVectorDim = 128 - testShardsNum = 1 -) - -var testCollections []string // generated collection names during testing, used to in tear down - -func getDefaultClient(t *testing.T) client.Client { - c, err := client.NewGrpcClient(context.Background(), defaultMilvusAddr) - assert.Nil(t, err) - assert.NotNil(t, c) - if c == nil { - t.FailNow() - } - return c -} - -func generateCollectionName() string { - cname := testCollectionPrefix + ut.GenRandomString(8) - testCollections = append(testCollections, cname) - return cname -} - -func generateSchema() *entity.Schema { - return &entity.Schema{ - Fields: []*entity.Field{ - { - Name: testPrimaryField, - DataType: entity.FieldTypeInt64, - PrimaryKey: true, - AutoID: true, - }, - { - Name: testVectorField, - DataType: entity.FieldTypeFloatVector, - TypeParams: map[string]string{ - entity.TypeParamDim: fmt.Sprintf("%d", testVectorDim), - }, - IndexParams: map[string]string{ - "metric_type": "L2", - }, - }, - }, - } -} - -func generateCollection(t *testing.T, c client.Client, cname string, schema *entity.Schema, data bool) [][]float32 { - if c == nil { - t.FailNow() - return nil - } - - ctx := context.Background() - has, err := c.HasCollection(ctx, cname) - assert.Nil(t, err) - if has { // maybe last test crashed, do clean up - assert.Nil(t, c.DropCollection(ctx, cname)) - } - schema.CollectionName = cname - assert.Nil(t, c.CreateCollection(ctx, schema, int32(testShardsNum))) - - if !data { - return nil - } - - vector := generateFloatVector(4096, testVectorDim) - _, err = c.Insert(ctx, cname, "", // use default partition - entity.NewColumnFloatVector(testVectorField, testVectorDim, vector)) - if err != nil { - t.Log(err.Error()) - } - c.Flush(ctx, cname, false) - return vector -} - -func generateFloatVector(num, dim int) [][]float32 { - rand.Seed(time.Now().Unix()) - r := make([][]float32, 0, num) - for i := 0; i < num; i++ { - v := make([]float32, 0, dim) - for j := 0; j < dim; j++ { - v = append(v, rand.Float32()) - } - r = append(r, v) - } - return r -}