Skip to content

Commit

Permalink
Add tests on pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Jun 23, 2022
1 parent 4feb2f3 commit 02e1f24
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TaskClient {
const url = `tasks/${uid}`
return await this.httpRequest.get<Task>(url)
}

/**
* Get tasks
*
Expand All @@ -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<Promise<TasksResults>>(
url,
removeUndefinedFromObject(queryParams)
Expand Down
17 changes: 5 additions & 12 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export type Config = {
/// Resources
///

export type ResourceQuery = Pagination & {}

export type Result<T> = Pagination & {
results: T
total: number
export type Pagination = {
offset?: number
limit?: number
}

export type ResourceQuery = Pagination & {}

export type ResourceResults<T> = Pagination & {
results: T
total: number
Expand Down Expand Up @@ -51,11 +51,6 @@ export type IndexesResults<T> = ResourceResults<T> & {}

export type Filter = string | Array<string | string[]>

export type Pagination = {
offset?: number
limit?: number
}

export type Query = {
q?: string | null
}
Expand Down Expand Up @@ -264,8 +259,6 @@ export type Task = Omit<EnqueuedTask, 'taskUid'> & {
finishedAt: string
}

export type TasksParams = {}

export type TasksResults = {
results: Task[]
limit: number
Expand Down
36 changes: 32 additions & 4 deletions tests/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book>({
const documents = await client.index(indexPk.uid).getDocuments<Book>({
fields: ['id'],
})
const onlyIdFields = Array.from(
new Set(
documents.results.reduce<string[]>(
(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<Book>({
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 () => {
Expand Down
25 changes: 25 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
15 changes: 15 additions & 0 deletions tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }])
Expand Down

0 comments on commit 02e1f24

Please sign in to comment.