diff --git a/tests/milvus-java-test/src/main/java/com/TestCollection.java b/tests/milvus-java-test/src/main/java/com/TestCollection.java index 94a0d1ae7b..22955ddbec 100644 --- a/tests/milvus-java-test/src/main/java/com/TestCollection.java +++ b/tests/milvus-java-test/src/main/java/com/TestCollection.java @@ -1,6 +1,5 @@ package com; - import com.alibaba.fastjson.JSONObject; import io.milvus.client.*; import org.testng.Assert; @@ -17,17 +16,18 @@ public class TestCollection { public MilvusClient setUp() throws ConnectFailedException { MilvusClient client = new MilvusGrpcClient(); ConnectParam connectParam = new ConnectParam.Builder() - .withHost("192.168.1.6") + .withHost("127.0.0.1") .withPort(19530) .build(); client.connect(connectParam); return client; } + @AfterClass public void tearDown() throws ConnectFailedException { MilvusClient client = setUp(); List collectionNames = client.listCollections().getCollectionNames(); -// collectionNames.forEach(collection -> {client.dropCollection(collection);}); + // collectionNames.forEach(collection -> {client.dropCollection(collection);}); for(String collection: collectionNames){ System.out.print(collection+" "); client.dropCollection(collection); @@ -35,8 +35,9 @@ public class TestCollection { System.out.println("After Test"); } + // case-01 @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) - public void testCreateCollection(MilvusClient client, String collectionName){ + public void testCreateCollection(MilvusClient client, String collectionName) { CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName) .withFields(Utils.genDefaultFields(dimension,false)) .withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount)) @@ -46,6 +47,7 @@ public class TestCollection { Assert.assertEquals(res.ok(), true); } + // case-02 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) public void testCreateCollectionDisconnect(MilvusClient client, String collectionName){ CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName) @@ -56,6 +58,7 @@ public class TestCollection { assert(!res.ok()); } + // case-03 @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) public void testCreateCollectionRepeatably(MilvusClient client, String collectionName){ CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName) @@ -68,6 +71,7 @@ public class TestCollection { Assert.assertEquals(resNew.ok(), false); } + // case-04 @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) public void testCreateCollectionWrongParams(MilvusClient client, String collectionName){ Integer dim = 0; @@ -80,6 +84,7 @@ public class TestCollection { Assert.assertEquals(res.ok(), false); } + // case-05 @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) public void testShowCollections(MilvusClient client, String collectionName){ Integer collectionNum = 10; @@ -96,12 +101,14 @@ public class TestCollection { } } + // case-06 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) - public void testShowCollectionsWithoutConnect(MilvusClient client, String collectionName){ + public void testShowCollectionsWithoutConnect(MilvusClient client, String collectionName) { ListCollectionsResponse res = client.listCollections(); assert(!res.getResponse().ok()); } + // case-07 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testDropCollection(MilvusClient client, String collectionName) throws InterruptedException { Response res = client.dropCollection(collectionName); @@ -111,6 +118,7 @@ public class TestCollection { Assert.assertFalse(collectionNames.contains(collectionName)); } + // case-08 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testDropCollectionNotExisted(MilvusClient client, String collectionName) { Response res = client.dropCollection(collectionName+"_"); @@ -119,12 +127,14 @@ public class TestCollection { Assert.assertTrue(collectionNames.contains(collectionName)); } + // case-09 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) public void testDropCollectionWithoutConnect(MilvusClient client, String collectionName) { Response res = client.dropCollection(collectionName); assert(!res.ok()); } + // case-10 // TODO @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testDescribeCollection(MilvusClient client, String collectionName) { @@ -147,12 +157,14 @@ public class TestCollection { Assert.assertEquals(Utils.getParam(segmentParams,"segment_row_count"), segmentRowCount); } + // case-11 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) public void testDescribeCollectionWithoutConnect(MilvusClient client, String collectionName) { GetCollectionInfoResponse res = client.getCollectionInfo(collectionName); assert(!res.getResponse().ok()); } + // case-12 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testHasCollectionNotExisted(MilvusClient client, String collectionName) { HasCollectionResponse res = client.hasCollection(collectionName+"_"); @@ -160,12 +172,14 @@ public class TestCollection { Assert.assertFalse(res.hasCollection()); } + // case-13 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) public void testHasCollectionWithoutConnect(MilvusClient client, String collectionName) { HasCollectionResponse res = client.hasCollection(collectionName); assert(!res.getResponse().ok()); } + // case-14 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testHasCollection(MilvusClient client, String collectionName) { HasCollectionResponse res = client.hasCollection(collectionName); diff --git a/tests/milvus-java-test/src/main/java/com/TestCollectionCount.java b/tests/milvus-java-test/src/main/java/com/TestCollectionCount.java index 5ce1ccbf4f..b5bc7ad4eb 100644 --- a/tests/milvus-java-test/src/main/java/com/TestCollectionCount.java +++ b/tests/milvus-java-test/src/main/java/com/TestCollectionCount.java @@ -10,23 +10,27 @@ public class TestCollectionCount { int dimension = Constants.dimension; int nb = Constants.nb; + // case-01 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testCollectionCountNoVectors(MilvusClient client, String collectionName) { Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0); } + // case-02 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testCollectionCountCollectionNotExisted(MilvusClient client, String collectionName) { CountEntitiesResponse res = client.countEntities(collectionName+"_"); assert(!res.getResponse().ok()); } + // case-03 @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) public void testCollectionCountWithoutConnect(MilvusClient client, String collectionName) { CountEntitiesResponse res = client.countEntities(collectionName+"_"); assert(!res.getResponse().ok()); } + // case-04 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testCollectionCount(MilvusClient client, String collectionName) throws InterruptedException { @@ -43,6 +47,7 @@ public class TestCollectionCount { Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb); } + // case-05 @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class) public void testCollectionCountBinary(MilvusClient client, String collectionName) throws InterruptedException { // Add vectors @@ -54,6 +59,7 @@ public class TestCollectionCount { Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb); } + // case-06 @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) public void testCollectionCountMultiCollections(MilvusClient client, String collectionName) throws InterruptedException { Integer collectionNum = 10; @@ -64,8 +70,8 @@ public class TestCollectionCount { .withFields(Utils.genDefaultFields(dimension,false)) .withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount)) .build(); - Response cteateRes = client.createCollection(collectionSchema); - Assert.assertEquals(cteateRes.ok(), true); + Response createRes = client.createCollection(collectionSchema); + Assert.assertEquals(createRes.ok(), true); // Add vectors InsertParam insertParam = new InsertParam.Builder(collectionNameNew) .withFields(Constants.defaultEntities) @@ -81,7 +87,6 @@ public class TestCollectionCount { Assert.assertEquals(res.getCollectionEntityCount(), nb); } } - } diff --git a/tests/milvus-java-test/src/main/java/com/TestCollectionCount_v2.java b/tests/milvus-java-test/src/main/java/com/TestCollectionCount_v2.java new file mode 100644 index 0000000000..4d00539525 --- /dev/null +++ b/tests/milvus-java-test/src/main/java/com/TestCollectionCount_v2.java @@ -0,0 +1,67 @@ +package com; + +import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const; +import io.milvus.client.*; +import org.testng.Assert; +import org.testng.annotations.Test; +import java.util.List; + +public class TestCollectionCount_v2 { + int segmentRowCount = 5000; + int dimension = Constants.dimension; + int nb = Constants.nb; + + // case-04 + @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) + public void testCollectionCount(MilvusClient client, String collectionName) throws InterruptedException { + + InsertParam insertParam = + Utils.genDefaultInsertParam(collectionName, Constants.dimension, Constants.nb, Constants.vectors); + InsertResponse insertResponse = client.insert(insertParam); + // Insert returns a list of entity ids that you will be using (if you did not supply the yourself) to reference the entities you just inserted + List vectorIds = insertResponse.getEntityIds(); + // Add vectors + Response flushResponse = client.flush(collectionName); + Assert.assertTrue(flushResponse.ok()); + Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb); + } + + // case-05 + @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class) + public void testCollectionCountBinary(MilvusClient client, String collectionName) throws InterruptedException { + // Add vectors + InsertParam insertParam = Utils.genDefaultBinaryInsertParam(collectionName, Constants.dimension, Constants.nb, + Constants.vectorsBinary); + client.insert(insertParam); + client.flush(collectionName); + Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb); + } + + // case-06 + @Test(dataProvider = "Collection", dataProviderClass = MainClass.class) + public void testCollectionCountMultiCollections(MilvusClient client, String collectionName) throws InterruptedException { + Integer collectionNum = 10; + CountEntitiesResponse res; + for (int i = 0; i < collectionNum; ++i) { + String collectionNameNew = collectionName + "_" + i; + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionNameNew, dimension, segmentRowCount, false); + Response createRes = client.createCollection(collectionSchema); + Assert.assertEquals(createRes.ok(), true); + // Add vectors + InsertParam insertParam = Utils.genDefaultInsertParam(collectionNameNew, Constants.dimension, Constants.nb, + Constants.vectors); + InsertResponse insertRes = client.insert(insertParam); + Assert.assertEquals(insertRes.ok(), true); + Response flushRes = client.flush(collectionNameNew); + Assert.assertEquals(flushRes.ok(), true); + } + for (int i = 0; i < collectionNum; ++i) { + String collectionNameNew = collectionName + "_" + i; + res = client.countEntities(collectionNameNew); + Assert.assertEquals(res.getCollectionEntityCount(), nb); + } + } +} + + diff --git a/tests/milvus-java-test/src/main/java/com/TestCollection_v2.java b/tests/milvus-java-test/src/main/java/com/TestCollection_v2.java new file mode 100644 index 0000000000..84f1398b4c --- /dev/null +++ b/tests/milvus-java-test/src/main/java/com/TestCollection_v2.java @@ -0,0 +1,92 @@ +package com; + +import com.alibaba.fastjson.JSONObject; +import io.milvus.client.*; +import org.testng.Assert; +import org.testng.annotations.*; + +import java.util.List; +import java.util.Map; + +public class TestCollection_v2 { + int segmentRowCount = 5000; + int dimension = 128; + + @BeforeClass + public MilvusClient setUp() throws ConnectFailedException { + MilvusClient client = new MilvusGrpcClient(); + ConnectParam connectParam = new ConnectParam.Builder() + .withHost("127.0.0.1") + .withPort(19530) + .build(); + client.connect(connectParam); + return client; + } + + @AfterClass + public void tearDown() throws ConnectFailedException { + MilvusClient client = setUp(); + List collectionNames = client.listCollections().getCollectionNames(); + // collectionNames.forEach(collection -> {client.dropCollection(collection);}); + for(String collection: collectionNames){ + System.out.print(collection+" "); + client.dropCollection(collection); + } + System.out.println("After Test"); + } + + // case-01 + @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) + public void testCreateCollection(MilvusClient client, String collectionName) { + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionName, dimension, segmentRowCount, false); + Response res = client.createCollection(collectionSchema); + assert(res.ok()); + Assert.assertEquals(res.ok(), true); + } + + // case-02 + @Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class) + public void testCreateCollectionDisconnect(MilvusClient client, String collectionName) { + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionName, dimension, segmentRowCount, false); + Response res = client.createCollection(collectionSchema); + assert(!res.ok()); + } + + // case-03 + @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) + public void testCreateCollectionRepeatably(MilvusClient client, String collectionName) { + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionName, dimension, segmentRowCount, false); + Response res = client.createCollection(collectionSchema); + Assert.assertEquals(res.ok(), true); + Response resNew = client.createCollection(collectionSchema); + Assert.assertEquals(resNew.ok(), false); + } + + // case-04 + @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) + public void testCreateCollectionWrongParams(MilvusClient client, String collectionName) { + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionName, 0, segmentRowCount, false); + Response res = client.createCollection(collectionSchema); + System.out.println(res.toString()); + Assert.assertEquals(res.ok(), false); + } + + // case-05 + @Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class) + public void testShowCollections(MilvusClient client, String collectionName) { + Integer collectionNum = 10; + ListCollectionsResponse res = null; + for (int i = 0; i < collectionNum; ++i) { + String collectionNameNew = collectionName+"_"+Integer.toString(i); + CollectionMapping collectionSchema = + Utils.genDefaultCollectionMapping(collectionNameNew, dimension, segmentRowCount, false); + client.createCollection(collectionSchema); + List collectionNames = client.listCollections().getCollectionNames(); + Assert.assertTrue(collectionNames.contains(collectionNameNew)); + } + } +} diff --git a/tests/milvus-java-test/src/main/java/com/Utils.java b/tests/milvus-java-test/src/main/java/com/Utils.java index 4dd0cab2c5..37b840011c 100644 --- a/tests/milvus-java-test/src/main/java/com/Utils.java +++ b/tests/milvus-java-test/src/main/java/com/Utils.java @@ -147,6 +147,75 @@ public class Utils { return indexParams; } + public static CollectionMapping genDefaultCollectionMapping(String collectionName, int dimension, + int segmentRowCount, boolean isBinary) { + Map vectorFieldMap; + if (isBinary) { + vectorFieldMap = new FieldBuilder("binary_vector", DataType.VECTOR_BINARY) + .param("dim", dimension) + .build(); + } else { + vectorFieldMap = new FieldBuilder("float_vector", DataType.VECTOR_FLOAT) + .param("dim", dimension) + .build(); + } + + return new CollectionMapping.Builder(collectionName) + .field(new FieldBuilder("int64", DataType.INT64).build()) + .field(new FieldBuilder("float", DataType.FLOAT).build()) + .field(vectorFieldMap) + .withParamsInJson(new JsonBuilder() + .param("segment_row_count", segmentRowCount) + .build()) + .build(); + } + + public static InsertParam genDefaultInsertParam(String collectionName, int dimension, int vectorCount, + List> vectors) { + List intValues = new ArrayList<>(vectorCount); + List floatValues = new ArrayList<>(vectorCount); + for (int i = 0; i < vectorCount; ++i) { + intValues.add((long) i); + floatValues.add((float) i); + } + + return new InsertParam.Builder(collectionName) + .field(new FieldBuilder("int64", DataType.INT64) + .values(intValues) + .build()) + .field(new FieldBuilder("float", DataType.FLOAT) + .values(floatValues) + .build()) + .field(new FieldBuilder("float_vector", DataType.VECTOR_FLOAT) + .values(vectors) + .param("dim", dimension) + .build()) + .build(); + } + + public static InsertParam genDefaultBinaryInsertParam(String collectionName, int dimension, int vectorCount, + List vectorsBinary) { + List intValues = new ArrayList<>(vectorCount); + List floatValues = new ArrayList<>(vectorCount); + for (int i = 0; i < vectorCount; ++i) { + intValues.add((long) i); + floatValues.add((float) i); + } + + return new InsertParam.Builder(collectionName) + .field(new FieldBuilder("int64", DataType.INT64) + .values(intValues) + .build()) + .field(new FieldBuilder("float", DataType.FLOAT) + .values(floatValues) + .build()) + .field(new FieldBuilder("binary_vector", DataType.VECTOR_BINARY) + .values(vectorsBinary) + .param("dim", dimension) + .build()) + .build(); + } + public static String setSearchParam(String metricType, List> queryVectors, int topk, int nprobe) { JSONObject searchParam = new JSONObject(); JSONObject fieldParam = new JSONObject();