|
1 | 1 | package io.pinecone.integration.controlPlane.pod;
|
2 | 2 |
|
3 | 3 | import io.pinecone.clients.Pinecone;
|
| 4 | +import io.pinecone.exceptions.PineconeBadRequestException; |
| 5 | +import io.pinecone.exceptions.PineconeUnmappedHttpException; |
4 | 6 | import io.pinecone.helpers.RandomStringBuilder;
|
5 |
| -import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
6 | 9 | import org.junit.jupiter.api.Test;
|
7 | 10 | import org.openapitools.client.model.*;
|
8 | 11 |
|
9 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
10 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 12 | +import static io.pinecone.helpers.IndexManager.waitUntilIndexIsReady; |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
11 | 14 |
|
12 | 15 | public class CreateDescribeListAndDeleteIndexTest {
|
13 |
| - private Pinecone controlPlaneClient; |
14 |
| - private final String apiKey = System.getenv("PINECONE_API_KEY"); |
15 |
| - private final String environment = System.getenv("PINECONE_ENVIRONMENT"); |
16 |
| - |
17 |
| - @BeforeEach |
18 |
| - public void setUp() { |
19 |
| - controlPlaneClient = new Pinecone(apiKey); |
20 |
| - } |
21 |
| - |
22 |
| - @Test |
23 |
| - public void createAndDelete() throws InterruptedException { |
24 |
| - String indexName = RandomStringBuilder.build("index-name", 8); |
25 |
| - CreateIndexRequestSpecPod podSpec = new CreateIndexRequestSpecPod().environment(environment).podType("p1.x1"); |
26 |
| - CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 16 | + private static final String indexName = RandomStringBuilder.build("create-index", 8); |
| 17 | + private static final String indexPodType = "p1.x1"; |
| 18 | + private static Pinecone controlPlaneClient = new Pinecone(System.getenv("PINECONE_API_KEY")); |
| 19 | + private static final String environment = System.getenv("PINECONE_ENVIRONMENT"); |
27 | 20 |
|
| 21 | + @BeforeAll |
| 22 | + public static void setUp() throws InterruptedException { |
28 | 23 | // Create the index
|
| 24 | + CreateIndexRequestSpecPod podSpec = new CreateIndexRequestSpecPod().environment(environment).podType(indexPodType); |
| 25 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
29 | 26 | CreateIndexRequest createIndexRequest = new CreateIndexRequest()
|
30 | 27 | .name(indexName)
|
31 | 28 | .metric(IndexMetric.COSINE)
|
32 | 29 | .dimension(10)
|
33 | 30 | .spec(createIndexRequestSpec);
|
34 | 31 | controlPlaneClient.createIndex(createIndexRequest);
|
| 32 | + waitUntilIndexIsReady(controlPlaneClient, indexName); |
| 33 | + } |
| 34 | + |
| 35 | + @AfterAll |
| 36 | + public static void cleanUp() { |
| 37 | + // Delete the index |
| 38 | + controlPlaneClient.deleteIndex(indexName); |
| 39 | + } |
35 | 40 |
|
| 41 | + @Test |
| 42 | + public void describeAndListIndex() { |
36 | 43 | // Describe the index
|
37 | 44 | IndexModel indexModel = controlPlaneClient.describeIndex(indexName);
|
38 | 45 | assertNotNull(indexModel);
|
39 | 46 | assertEquals(10, indexModel.getDimension());
|
40 | 47 | assertEquals(indexName, indexModel.getName());
|
41 | 48 | assertEquals(IndexMetric.COSINE, indexModel.getMetric());
|
| 49 | + assertNotNull(indexModel.getSpec().getPod()); |
| 50 | + assertEquals(indexPodType, indexModel.getSpec().getPod().getPodType()); |
42 | 51 |
|
43 | 52 | // List the index
|
44 | 53 | IndexList indexList = controlPlaneClient.listIndexes();
|
45 | 54 | assertNotNull(indexList.getIndexes());
|
| 55 | + assertTrue(indexList.getIndexes().stream().anyMatch(index -> indexName.equals(index.getName()))); |
| 56 | + } |
46 | 57 |
|
47 |
| - // Delete the index |
48 |
| - controlPlaneClient.deleteIndex(indexName); |
49 |
| - Thread.sleep(3500); |
| 58 | + @Test |
| 59 | + public void createIndexWithPodsAndPodType() { |
| 60 | + String podIndexName = RandomStringBuilder.build("create-pod", 8); |
| 61 | + String podType = "p1.x2"; |
| 62 | + CreateIndexRequestSpecPod podSpec = new CreateIndexRequestSpecPod().environment(environment).pods(2).podType(podType); |
| 63 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 64 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 65 | + .name(podIndexName) |
| 66 | + .dimension(10) |
| 67 | + .metric(IndexMetric.EUCLIDEAN) |
| 68 | + .spec(createIndexRequestSpec); |
| 69 | + |
| 70 | + IndexModel createdIndex = controlPlaneClient.createIndex(createIndexRequest); |
| 71 | + assertEquals(createdIndex.getName(), podIndexName); |
| 72 | + assertEquals(createdIndex.getMetric(), IndexMetric.EUCLIDEAN); |
| 73 | + assertEquals(createdIndex.getSpec().getPod().getPods(), 2); |
| 74 | + assertEquals(createdIndex.getSpec().getPod().getPodType(), podType); |
| 75 | + assertEquals(createdIndex.getStatus().getReady(), false); |
| 76 | + assertEquals(createdIndex.getStatus().getState(), IndexModelStatus.StateEnum.INITIALIZING); |
| 77 | + |
| 78 | + controlPlaneClient.deleteIndex(podIndexName); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void createIndexWithInvalidName() { |
| 83 | + CreateIndexRequestSpecPod podSpec = new CreateIndexRequestSpecPod().environment(environment).podType(indexPodType); |
| 84 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 85 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 86 | + .name("Invalid-name") |
| 87 | + .metric(IndexMetric.COSINE) |
| 88 | + .dimension(10) |
| 89 | + .spec(createIndexRequestSpec); |
| 90 | + |
| 91 | + try { |
| 92 | + controlPlaneClient.createIndex(createIndexRequest); |
| 93 | + |
| 94 | + fail("Expected to throw PineconeBadRequestException"); |
| 95 | + } catch (PineconeBadRequestException expected) { |
| 96 | + assertTrue(expected.getLocalizedMessage().contains("Name must consist of lower case alphanumeric characters or '-'")); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void createIndexWithInvalidDimension() { |
| 102 | + CreateIndexRequestSpecPod podSpec = new CreateIndexRequestSpecPod().environment(environment).podType(indexPodType); |
| 103 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 104 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 105 | + .name("invalid-dimension") |
| 106 | + .metric(IndexMetric.COSINE) |
| 107 | + .dimension(-1) |
| 108 | + .spec(createIndexRequestSpec); |
| 109 | + |
| 110 | + try { |
| 111 | + controlPlaneClient.createIndex(createIndexRequest); |
| 112 | + |
| 113 | + fail("Expected to throw PineconeUnmappedHttpException"); |
| 114 | + } catch (PineconeUnmappedHttpException expected) { |
| 115 | + assertTrue(expected.getLocalizedMessage().contains("dimension: invalid value: integer `-1`, expected u32")); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void createIndexWithInvalidPods() { |
| 121 | + CreateIndexRequestSpecPod podSpec = |
| 122 | + new CreateIndexRequestSpecPod().environment(environment).pods(-1).podType(indexPodType); |
| 123 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 124 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 125 | + .name("invalid-pods") |
| 126 | + .metric(IndexMetric.COSINE) |
| 127 | + .dimension(10) |
| 128 | + .spec(createIndexRequestSpec); |
| 129 | + |
| 130 | + try { |
| 131 | + controlPlaneClient.createIndex(createIndexRequest); |
| 132 | + |
| 133 | + fail("Expected to throw PineconeBadRequestException"); |
| 134 | + } catch (PineconeBadRequestException expected) { |
| 135 | + assertTrue(expected.getLocalizedMessage().contains("Invalid value for pods: must be greater than 0")); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void createIndexWithInvalidReplicas() { |
| 141 | + CreateIndexRequestSpecPod podSpec = |
| 142 | + new CreateIndexRequestSpecPod().environment(environment).pods(1).replicas(-1).podType(indexPodType); |
| 143 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 144 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 145 | + .name("invalid-replicas") |
| 146 | + .metric(IndexMetric.COSINE) |
| 147 | + .dimension(10) |
| 148 | + .spec(createIndexRequestSpec); |
| 149 | + |
| 150 | + try { |
| 151 | + controlPlaneClient.createIndex(createIndexRequest); |
| 152 | + |
| 153 | + fail("Expected to throw PineconeBadRequestException"); |
| 154 | + } catch (PineconeBadRequestException expected) { |
| 155 | + assertTrue(expected.getLocalizedMessage().contains("Invalid value for replicas: must be greater than 0")); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void createIndexWithInvalidPodsToShards() { |
| 161 | + CreateIndexRequestSpecPod podSpec = |
| 162 | + new CreateIndexRequestSpecPod().environment(environment).pods(5).replicas(2).shards(2).podType(indexPodType); |
| 163 | + CreateIndexRequestSpec createIndexRequestSpec = new CreateIndexRequestSpec().pod(podSpec); |
| 164 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest() |
| 165 | + .name("invalid-shards") |
| 166 | + .metric(IndexMetric.COSINE) |
| 167 | + .dimension(10) |
| 168 | + .spec(createIndexRequestSpec); |
| 169 | + |
| 170 | + try { |
| 171 | + controlPlaneClient.createIndex(createIndexRequest); |
| 172 | + |
| 173 | + fail("Expected to throw PineconeBadRequestException"); |
| 174 | + } catch (PineconeBadRequestException expected) { |
| 175 | + assertTrue(expected.getLocalizedMessage().contains("Invalid value for pods: total pods must be divisible by number of shards")); |
| 176 | + } |
50 | 177 | }
|
51 | 178 | }
|
0 commit comments