From 02e1f24cc210cf3ebb677fc760c2a883faf3b8f8 Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Thu, 23 Jun 2022 14:40:02 +0200 Subject: [PATCH] Add tests on pagination --- src/task.ts | 4 ++++ src/types/types.ts | 17 +++++------------ tests/documents.test.ts | 36 ++++++++++++++++++++++++++++++++---- tests/index.test.ts | 25 +++++++++++++++++++++++++ tests/keys.test.ts | 9 +++++++++ tests/task.test.ts | 15 +++++++++++++++ 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/task.ts b/src/task.ts index 93dd0b8b1..83878cb5e 100644 --- a/src/task.ts +++ b/src/task.ts @@ -28,6 +28,7 @@ class TaskClient { const url = `tasks/${uid}` return await this.httpRequest.get(url) } + /** * Get tasks * @@ -42,7 +43,10 @@ class TaskClient { indexUid: parameters?.indexUid?.join(','), type: parameters?.type?.join(','), status: parameters?.status?.join(','), + from: parameters.from, + limit: parameters.limit, } + return await this.httpRequest.get>( url, removeUndefinedFromObject(queryParams) diff --git a/src/types/types.ts b/src/types/types.ts index 5fe59b6eb..b60c059e4 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -14,13 +14,13 @@ export type Config = { /// Resources /// -export type ResourceQuery = Pagination & {} - -export type Result = Pagination & { - results: T - total: number +export type Pagination = { + offset?: number + limit?: number } +export type ResourceQuery = Pagination & {} + export type ResourceResults = Pagination & { results: T total: number @@ -51,11 +51,6 @@ export type IndexesResults = ResourceResults & {} export type Filter = string | Array -export type Pagination = { - offset?: number - limit?: number -} - export type Query = { q?: string | null } @@ -264,8 +259,6 @@ export type Task = Omit & { finishedAt: string } -export type TasksParams = {} - export type TasksResults = { results: Task[] limit: number diff --git a/tests/documents.test.ts b/tests/documents.test.ts index c1537aa50..4fea51995 100644 --- a/tests/documents.test.ts +++ b/tests/documents.test.ts @@ -71,14 +71,42 @@ describe('Documents tests', () => { test(`${permission} key: Get documents with array fields`, async () => { const client = await getClient(permission) + const { taskUid } = await client + .index(indexPk.uid) + .addDocuments(dataset) + await client.waitForTask(taskUid) - const documents = await client.index(indexNoPk.uid).getDocuments({ + const documents = await client.index(indexPk.uid).getDocuments({ fields: ['id'], }) + const onlyIdFields = Array.from( + new Set( + documents.results.reduce( + (acc, document) => [...acc, ...Object.keys(document)], + [] + ) + ) + ) - expect( - documents.results.find((x) => Object.keys(x).length !== 1) - ).toBeUndefined() + expect(onlyIdFields.length).toEqual(1) + expect(onlyIdFields[0]).toEqual('id') + }) + + test(`${permission} key: Get documents with pagination`, async () => { + const client = await getClient(permission) + const { taskUid } = await client + .index(indexPk.uid) + .addDocuments(dataset) + await client.waitForTask(taskUid) + + const documents = await client.index(indexPk.uid).getDocuments({ + limit: 1, + offset: 2, + }) + + expect(documents.results.length).toEqual(1) + expect(documents.limit).toEqual(1) + expect(documents.offset).toEqual(2) }) test(`${permission} key: Get documents from index that has NO primary key`, async () => { diff --git a/tests/index.test.ts b/tests/index.test.ts index 1511b351d..004abf868 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -213,6 +213,31 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])( expect(response).toHaveProperty('primaryKey', null) }) + test(`${permission} key: get all indexes`, async () => { + const client = await getClient(permission) + const task1 = await client.createIndex(indexNoPk.uid) + const task2 = await client.createIndex(indexPk.uid) + await client.waitForTask(task1.taskUid) + await client.waitForTask(task2.taskUid) + + const indexes = await client.getIndexes() + + expect(indexes.results.length).toEqual(2) + }) + + test(`${permission} key: get all indexes with filters`, async () => { + const client = await getClient(permission) + const task1 = await client.createIndex(indexNoPk.uid) + const task2 = await client.createIndex(indexPk.uid) + await client.waitForTask(task1.taskUid) + await client.waitForTask(task2.taskUid) + + const indexes = await client.getIndexes({ limit: 1, offset: 1 }) + + expect(indexes.results.length).toEqual(1) + expect(indexes.results[0].uid).toEqual(indexPk.uid) + }) + test(`${permission} key: update primary key on an index that has no primary key already`, async () => { const client = await getClient(permission) const { taskUid: createTask } = await client.createIndex(indexNoPk.uid) diff --git a/tests/keys.test.ts b/tests/keys.test.ts index 5cda3ed29..dc260f16e 100644 --- a/tests/keys.test.ts +++ b/tests/keys.test.ts @@ -66,6 +66,15 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])( expect(adminKey).toHaveProperty('updatedAt') }) + test(`${permission} key: get keys with pagination`, async () => { + const client = await getClient(permission) + const keys = await client.getKeys({ limit: 1, offset: 2 }) + + expect(keys.limit).toEqual(1) + expect(keys.offset).toEqual(2) + expect(keys.total).toEqual(2) + }) + test(`${permission} key: get on key`, async () => { const client = await getClient(permission) const apiKey = await getKey('Private') diff --git a/tests/task.test.ts b/tests/task.test.ts index 058deded8..7af51931f 100644 --- a/tests/task.test.ts +++ b/tests/task.test.ts @@ -132,6 +132,21 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])( expect(onlyDocumentAddition.size).toEqual(2) }) + test(`${permission} key: Get all tasks with pagination`, async () => { + const client = await getClient(permission) + const task1 = await client.index(index.uid).addDocuments([{ id: 1 }]) + const task2 = await client.index(index.uid).addDocuments([{ id: 1 }]) + await client.waitForTask(task1.taskUid) + await client.waitForTask(task2.taskUid) + + const tasks = await client.getTasks({ from: 1, limit: 1 }) + + expect(tasks.results.length).toEqual(1) + expect(tasks.from).toEqual(1) + expect(tasks.limit).toEqual(1) + expect(tasks.next).toEqual(0) + }) + test(`${permission} key: Get all tasks with status filter`, async () => { const client = await getClient(permission) const task1 = await client.index(index.uid).addDocuments([{ id: 1 }])