Skip to content

Commit

Permalink
Adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Sep 13, 2023
1 parent 0f0c6bf commit 85809c9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 37 deletions.
11 changes: 10 additions & 1 deletion src/__tests__/pinecone.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Pinecone } from '../pinecone';
import type { PineconeConfiguration } from '../data';
import crossFetch from 'cross-fetch';

jest.mock('cross-fetch', () => {
//Mock the default export
return {
__esModule: true,
default: jest.fn(),
};
});

jest.mock('../data/fetch');
jest.mock('../data/upsert');
Expand Down Expand Up @@ -96,7 +105,7 @@ describe('Pinecone', () => {
const client = new Pinecone();

expect(client.getConfig().projectId).toEqual('test-project');
expect(global.fetch).not.toHaveBeenCalled();
expect(crossFetch).not.toHaveBeenCalled();
});

test('config object should take precedence when both config object and environment variables are provided', () => {
Expand Down
37 changes: 21 additions & 16 deletions src/data/__tests__/projectIdSingleton.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { ProjectIdSingleton } from '../projectIdSingleton';
import crossFetch from 'cross-fetch';

jest.mock('cross-fetch', () => {
//Mock the default export
return {
__esModule: true,
default: jest.fn(),
};
});

describe('ProjectIdSingleton', () => {
afterEach(() => {
Expand All @@ -7,12 +16,10 @@ describe('ProjectIdSingleton', () => {

test('issues whoami requests for unknown projectId', async () => {
// @ts-ignore
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({ project_name: 'abcdef' }),
})
);
crossFetch.mockResolvedValue({
status: 200,
json: () => Promise.resolve({ project_name: 'abcdef' }),
});

const testApiKey = 'api-key-1';
const testConfig = {
Expand All @@ -23,7 +30,7 @@ describe('ProjectIdSingleton', () => {
const id = await ProjectIdSingleton.getProjectId(testConfig);

expect(id).toEqual('abcdef');
expect(global.fetch).toHaveBeenCalledWith(
expect(crossFetch).toHaveBeenCalledWith(
'https://controller.gcp-free.pinecone.io/actions/whoami',
{
headers: {
Expand All @@ -38,12 +45,10 @@ describe('ProjectIdSingleton', () => {

test('only makes API call once per api key', async () => {
// @ts-ignore
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({ project_name: 'xyzxyz' }),
})
);
crossFetch.mockResolvedValue({
status: 200,
json: () => Promise.resolve({ project_name: 'xyzxyz' }),
});

const testApiKey = 'api-key-2';
const testConfig2 = {
Expand All @@ -53,15 +58,15 @@ describe('ProjectIdSingleton', () => {

const id = await ProjectIdSingleton.getProjectId(testConfig2);
expect(id).toEqual('xyzxyz');
expect(global.fetch).toHaveBeenCalledTimes(1);
expect(crossFetch).toHaveBeenCalledTimes(1);

const id2 = await ProjectIdSingleton.getProjectId(testConfig2);
expect(id2).toEqual('xyzxyz');
expect(global.fetch).toHaveBeenCalledTimes(1);
expect(crossFetch).toHaveBeenCalledTimes(1);

await ProjectIdSingleton.getProjectId(testConfig2);
await ProjectIdSingleton.getProjectId(testConfig2);
await ProjectIdSingleton.getProjectId(testConfig2);
expect(global.fetch).toHaveBeenCalledTimes(1);
expect(crossFetch).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion src/data/__tests__/upsert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
VectorOperationsApi,
type UpsertOperationRequest,
} from '../../pinecone-generated-ts-fetch';
import { VectorOperationsProvider } from '../vectorOperationsProvider';
import type { VectorOperationsProvider } from '../vectorOperationsProvider';

const setupResponse = (response, isSuccess) => {
const fakeUpsert: (req: UpsertOperationRequest) => Promise<object> = jest
Expand Down
4 changes: 2 additions & 2 deletions src/data/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class UpsertCommand<T extends RecordMetadata = RecordMetadata> {
});
return;
} catch (e) {
// const err = await handleApiError(e);
throw e;
const err = await handleApiError(e);
throw err;
}
}
}
5 changes: 1 addition & 4 deletions src/data/vectorOperationsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '../pinecone-generated-ts-fetch';
import { queryParamsStringify, buildUserAgent } from '../utils';
import { ProjectIdSingleton } from './projectIdSingleton';
import { debugLog } from '../utils';
import fetch from 'cross-fetch';

const basePath = (config: PineconeConfiguration, indexName: string) =>
Expand Down Expand Up @@ -46,16 +45,14 @@ export class VectorOperationsProvider {
}

buildVectorOperationsConfig(config, indexName) {
debugLog('Building config that targets ' + basePath(config, indexName));

const indexConfigurationParameters: ConfigurationParameters = {
basePath: basePath(config, indexName),
apiKey: config.apiKey,
queryParamsStringify,
headers: {
'User-Agent': buildUserAgent(false),
},
fetchApi: fetch
fetchApi: fetch,
};

const indexConfiguration = new Configuration(indexConfigurationParameters);
Expand Down
2 changes: 1 addition & 1 deletion src/integration/data/deleteMany.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('deleteMany', () => {
beforeAll(async () => {
pinecone = new Pinecone();

// await createIndexIfDoesNotExist(pinecone, INDEX_NAME);
await createIndexIfDoesNotExist(pinecone, INDEX_NAME);

namespace = randomString(16);
index = pinecone.index(INDEX_NAME);
Expand Down
2 changes: 1 addition & 1 deletion src/pinecone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Pinecone {
headers: {
'User-Agent': buildUserAgent(false),
},
fetchApi: fetch
fetchApi: fetch,
};
const api = new IndexOperationsApi(new ApiConfiguration(apiConfig));

Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"types": ["node", "jest"],
"noImplicitAny": false
"noImplicitAny": false,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["dist", "src/v0/pinecone-generated-ts-fetch"]
"exclude": ["node_modules"]
}
11 changes: 2 additions & 9 deletions utils/globalUnitTestSetup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import 'cross-fetch/polyfill';

// We want unit tests to fail if they attempt API calls without specifying mock responses
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();

// @ts-ignore
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => {
throw new Error('Attempted to fetch but no fetch mock was set.');
},
})
);
});

0 comments on commit 85809c9

Please sign in to comment.