-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update openapi core, implement new list feature for listing records b…
…y id and prefix, add integration and unit tests
- Loading branch information
1 parent
6461573
commit 5a7c90a
Showing
64 changed files
with
864 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { list } from '../list'; | ||
import type { | ||
ListRequest, | ||
ListResponse, | ||
DataPlaneApi, | ||
} from '../../pinecone-generated-ts-fetch'; | ||
import { VectorOperationsProvider } from '../vectorOperationsProvider'; | ||
|
||
const setupListResponse = (response, isSuccess = true) => { | ||
const fakeList: (req: ListRequest) => Promise<ListResponse> = jest | ||
.fn() | ||
.mockImplementation(() => | ||
isSuccess ? Promise.resolve(response) : Promise.reject(response) | ||
); | ||
const DPA = { list: fakeList } as DataPlaneApi; | ||
const VoaProvider = { provide: async () => DPA } as VectorOperationsProvider; | ||
return { DPA, VoaProvider }; | ||
}; | ||
|
||
describe('list', () => { | ||
test('calls the openapi list endpoint, passing target namespace with ListOptions', async () => { | ||
const listResponse = { | ||
vectors: [ | ||
{ id: 'prefix-1', values: [0.2, 0.4] }, | ||
{ id: 'prefix-2', values: [0.3, 0.5] }, | ||
{ id: 'prefix-3', values: [0.4, 0.6] }, | ||
], | ||
pagination: { next: 'fake-pagination-token-123123123' }, | ||
namespace: 'list-namespace', | ||
usage: { readUnits: 1 }, | ||
}; | ||
const { VoaProvider, DPA } = setupListResponse(listResponse); | ||
|
||
const listFn = list(VoaProvider, 'list-namespace'); | ||
const returned = await listFn({ prefix: 'prefix-' }); | ||
|
||
expect(returned).toBe(listResponse); | ||
expect(DPA.list).toHaveBeenCalledWith({ | ||
prefix: 'prefix-', | ||
namespace: 'list-namespace', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { buildConfigValidator } from '../validator'; | ||
import { Type } from '@sinclair/typebox'; | ||
import { VectorOperationsProvider } from './vectorOperationsProvider'; | ||
import type { ListRequest, ListResponse } from '../pinecone-generated-ts-fetch'; | ||
|
||
export type ListOptions = { | ||
prefix?: string; | ||
limit?: number; | ||
paginationToken?: string; | ||
}; | ||
|
||
const ListOptionsSchema = Type.Object( | ||
{ | ||
prefix: Type.Optional(Type.String({ minLength: 1 })), | ||
limit: Type.Optional(Type.Number()), | ||
paginationToken: Type.Optional(Type.String({ minLength: 1 })), | ||
}, | ||
{ additionalProperties: false } | ||
); | ||
|
||
export const list = ( | ||
apiProvider: VectorOperationsProvider, | ||
namespace: string | ||
) => { | ||
const validator = buildConfigValidator(ListOptionsSchema, 'list'); | ||
|
||
return async (options?: ListOptions): Promise<ListResponse> => { | ||
validator(options); | ||
|
||
const listRequest: ListRequest = { | ||
...options, | ||
namespace, | ||
}; | ||
|
||
const api = await apiProvider.provide(); | ||
return await api.list(listRequest); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Pinecone, Index } from '../../index'; | ||
import { | ||
generateRecords, | ||
randomString, | ||
INDEX_NAME, | ||
waitUntilRecordsReady, | ||
} from '../test-helpers'; | ||
|
||
describe('list', () => { | ||
let pinecone: Pinecone, | ||
index: Index, | ||
ns: Index, | ||
namespace: string, | ||
prefix: string, | ||
recordIds: string[]; | ||
|
||
beforeAll(async () => { | ||
pinecone = new Pinecone(); | ||
|
||
await pinecone.createIndex({ | ||
name: INDEX_NAME, | ||
dimension: 5, | ||
metric: 'cosine', | ||
spec: { | ||
serverless: { | ||
region: 'us-west-2', | ||
cloud: 'aws', | ||
}, | ||
}, | ||
waitUntilReady: true, | ||
suppressConflicts: true, | ||
}); | ||
|
||
namespace = randomString(16); | ||
index = pinecone.index(INDEX_NAME); | ||
ns = index.namespace(namespace); | ||
prefix = 'preTest-'; | ||
|
||
// Seed the namespace with records for testing | ||
const recordsToUpsert = generateRecords({ | ||
dimension: 5, | ||
quantity: 120, | ||
prefix, | ||
}); | ||
const upsertedIds = recordsToUpsert.map((r) => r.id); | ||
|
||
await ns.upsert(recordsToUpsert); | ||
await waitUntilRecordsReady(ns, namespace, upsertedIds); | ||
recordIds.concat(upsertedIds); | ||
}); | ||
|
||
afterAll(async () => { | ||
await ns.deleteMany(recordIds); | ||
}); | ||
|
||
test('test list with no arguments', async () => { | ||
const listResults = await index.list(); | ||
expect(listResults).toBeDefined(); | ||
expect(listResults.pagination).not.toBeDefined(); | ||
expect(listResults.vectors?.length).toBe(0); | ||
expect(listResults.namespace).toBe(namespace); | ||
}); | ||
|
||
test('test list with prefix', async () => { | ||
const listResults = await ns.list({ prefix }); | ||
expect(listResults.namespace).toBe(namespace); | ||
expect(listResults.vectors?.length).toBe(recordIds.length); | ||
expect(listResults.pagination?.next).toBeDefined(); | ||
}); | ||
|
||
test('test list with limit and pagination', async () => { | ||
const listResults = await ns.list({ prefix, limit: 60 }); | ||
expect(listResults.namespace).toBe(namespace); | ||
expect(listResults.vectors?.length).toBe(60); | ||
expect(listResults.pagination?.next).toBeDefined(); | ||
|
||
const listResultsPg2 = await ns.list({ | ||
prefix, | ||
limit: 60, | ||
paginationToken: listResults.pagination?.next, | ||
}); | ||
|
||
expect(listResultsPg2.namespace).toBe(namespace); | ||
expect(listResultsPg2.vectors?.length).toBe(60); | ||
expect(listResultsPg2.pagination?.next).not.toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.