Skip to content

Commit

Permalink
Create index integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Oct 6, 2023
1 parent fd5bebe commit 204670e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
124 changes: 124 additions & 0 deletions src/integration/control/createIndex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { PineconeNotFoundError } from '../../errors';
import { Pinecone } from '../../index';
import { randomIndexName } from '../test-helpers';

describe('create index', () => {
let indexName;
let pinecone: Pinecone;

beforeEach(async () => {
indexName = randomIndexName('createIndex');
pinecone = new Pinecone();
});

describe('happy path', () => {
afterEach(async () => {
await pinecone.deleteIndex(indexName);
});

test('simple create', async () => {
await pinecone.createIndex({
name: indexName,
dimension: 5,
});
const description = await pinecone.describeIndex(indexName);
expect(description.database?.name).toEqual(indexName);
expect(description.database?.dimension).toEqual(5);
expect(description.database?.metric).toEqual('cosine');
expect(description.database?.pods).toEqual(1);
expect(description.database?.replicas).toEqual(1);
expect(description.database?.shards).toEqual(1);
expect(description.status?.host).toBeDefined();
});

test('create with optional properties', async () => {
await pinecone.createIndex({
name: indexName,
dimension: 5,
metric: 'euclidean',
replicas: 2,
podType: 'p1.x2',
});

const description = await pinecone.describeIndex(indexName);
expect(description.database?.name).toEqual(indexName);
expect(description.database?.dimension).toEqual(5);
expect(description.database?.metric).toEqual('euclidean');
expect(description.database?.pods).toEqual(2);
expect(description.database?.replicas).toEqual(2);
expect(description.database?.shards).toEqual(1);
expect(description.status?.host).toBeDefined();
expect(description.status?.state).toEqual('Initializing');
});

test('create with utility prop: waitUntilReady', async () => {
await pinecone.createIndex({
name: indexName,
dimension: 5,
waitUntilReady: true,
});

const description = await pinecone.describeIndex(indexName);
expect(description.database?.name).toEqual(indexName);
expect(description.status?.state).toEqual('Ready');
});

test('create with utility prop: suppressConflicts', async () => {
await pinecone.createIndex({
name: indexName,
dimension: 5,
});
await pinecone.createIndex({
name: indexName,
dimension: 5,
suppressConflicts: true,
});

const description = await pinecone.describeIndex(indexName);
expect(description.database?.name).toEqual(indexName);
});
});

describe('error cases', () => {
test('create index with invalid index name', async () => {
try {
await pinecone.createIndex({
name: indexName + '-',
dimension: 5,
});
} catch (e) {
const err = e as PineconeNotFoundError;
expect(err.name).toEqual('PineconeBadRequestError');
expect(err.message).toContain('alphanumeric characters');
}
});

test('insufficient quota', async () => {
try {
await pinecone.createIndex({
name: indexName,
dimension: 5,
replicas: 20,
});
} catch (e) {
const err = e as PineconeNotFoundError;
expect(err.name).toEqual('PineconeBadRequestError');
expect(err.message).toContain('exceeds the project quota');
}
});

test('create from non-existent collection', async () => {
try {
await pinecone.createIndex({
name: indexName,
dimension: 5,
sourceCollection: 'non-existent-collection',
});
} catch (e) {
const err = e as PineconeNotFoundError;
expect(err.name).toEqual('PineconeBadRequestError');
expect(err.message).toContain('failed to fetch source collection');
}
});
});
});
6 changes: 3 additions & 3 deletions src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ if (process && process.env && process.env.PINECONE_DEBUG) {
if (context.init.body) {
console.debug(chalk(`>>> Body: ${context.init.body}`, 'blue'));
}
console.debug();
console.debug('');
},

post: async (context) => {
console.debug(chalk(`<<< Status: ${context.response.status}`, 'green'));
console.debug(
chalk(`<<< Body: ${await context.response.text()}`, 'green')
);
console.debug();
console.debug('');
},
};

Expand All @@ -65,7 +65,7 @@ if (process && process.env && process.env.PINECONE_DEBUG_CURL) {
context.init.body ? `-d '${context.init.body}'` : ''
}`;
console.debug(chalk(cmd, 'red'));
console.debug();
console.debug('');
},
};
debugMiddleware.push(debugCurlMiddleware);
Expand Down

0 comments on commit 204670e

Please sign in to comment.