Skip to content

Commit

Permalink
Try #1339:
Browse files Browse the repository at this point in the history
  • Loading branch information
meili-bors[bot] authored Oct 5, 2022
2 parents 3586193 + 61490c7 commit 77a858b
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,11 @@ update_sortable_attributes_1: |-
reset_sortable_attributes_1: |-
client.index('books').resetSortableAttributes()
get_pagination_settings_1: |-
client.index('books').getPagination()
update_pagination_settings_1: |-
client.index('books').updateSettings({ pagination: { maxTotalHits: 100 }})
reset_pagination_settings_1: |-
client.index('books').resetPagination()
get_faceting_settings_1: |-
client.index('books').getFaceting()
update_faceting_settings_1: |-
Expand All @@ -562,7 +564,7 @@ reset_faceting_settings_1: |-
settings_guide_faceting_1: |-
client.index('movies').updateSettings({ faceting: { maxValuesPerFacet: 5 }})
settings_guide_pagination_1: |-
client.index('books').updateSettings({ pagination: { maxTotalHits: 50 }})
client.index('movies').updateSettings({ pagination: { maxTotalHits: 50 }})
search_parameter_guide_sort_1: |-
client.index('books').search('science fiction', {
sort: ['price:asc'],
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,26 @@ client.index('myIndex').updateSettings(settings: Settings): Promise<EnqueuedTask
client.index('myIndex').resetSettings(): Promise<EnqueuedTask>
```

### Pagination Settings

#### [Get pagination](https://docs.meilisearch.com/reference/api/settings.html#get-pagination-settings)

```ts
client.index('myIndex').getPagination(): Promise<PaginationSettings>
```

#### [Update pagination](https://docs.meilisearch.com/reference/api/settings.html#update-pagination-settings)

```ts
client.index('myIndex').updatePagination(pagination: PaginationSettings): Promise<EnqueuedTask>
```

#### [Reset pagination](https://docs.meilisearch.com/reference/api/settings.html#reset-pagination-settings)

```ts
client.index('myIndex').resetPagination(): Promise<EnqueuedTask>
```

### Synonyms <!-- omit in toc -->

#### [Get synonyms](https://docs.meilisearch.com/reference/api/synonyms.html#get-synonyms)
Expand Down
44 changes: 44 additions & 0 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
DocumentsResults,
TasksQuery,
TasksResults,
PaginationSettings,
Faceting,
} from './types'
import { removeUndefinedFromObject } from './utils'
Expand Down Expand Up @@ -558,6 +559,49 @@ class Index<T = Record<string, any>> {
return task
}

///
/// PAGINATION SETTINGS
///

/**
* Get the pagination settings.
* @memberof Index
* @method getPagination
* @returns {Promise<PaginationSetting>} Promise containing object of pagination settings
*/
async getPagination(): Promise<PaginationSettings> {
const url = `indexes/${this.uid}/settings/pagination`
return await this.httpRequest.get<object>(url)
}

/**
* Update the pagination settings.
* @memberof Index
* @method updatePagination
* @param {PaginationSettings} pagination Pagination object
* @returns {Promise<EnqueuedTask>} Promise containing object of the enqueued task
*/
async updatePagination(
pagination: PaginationSettings
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/pagination`
const task = await this.httpRequest.patch(url, pagination)

return new EnqueuedTask(task)
}

/**
* Reset the pagination settings.
* @memberof Index
* @method resetPagination
* @returns {Promise<EnqueuedTask>} Promise containing object of the enqueued task
*/
async resetPagination(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/pagination`
const task = await this.httpRequest.delete(url)

return new EnqueuedTask(task)
}
///
/// SYNONYMS
///
Expand Down
205 changes: 205 additions & 0 deletions tests/pagination.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { ErrorStatusCode } from '../src/types'
import {
clearAllIndexes,
config,
BAD_HOST,
MeiliSearch,
getClient,
dataset,
} from './utils/meilisearch-test-utils'

const index = {
uid: 'movies_test',
}

jest.setTimeout(100 * 1000)

afterAll(() => {
return clearAllIndexes(config)
})

describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
'Test on pagination',
({ permission }) => {
beforeEach(async () => {
await clearAllIndexes(config)
const client = await getClient('Master')
const { taskUid } = await client.index(index.uid).addDocuments(dataset)
await client.waitForTask(taskUid)
})

test(`${permission} key: Get default pagination settings`, async () => {
const client = await getClient(permission)
const response = await client.index(index.uid).getPagination()

expect(response).toEqual({ maxTotalHits: 1000 })
})

test(`${permission} key: Update pagination`, async () => {
const client = await getClient(permission)
const newPagination = {
maxTotalHits: 100,
}
const task = await client.index(index.uid).updatePagination(newPagination)
await client.waitForTask(task.taskUid)

const response = await client.index(index.uid).getPagination()

expect(response).toEqual(newPagination)
})

test(`${permission} key: Update pagination at null`, async () => {
const client = await getClient(permission)
const newPagination = {
maxTotalHits: null,
}
const task = await client.index(index.uid).updatePagination(newPagination)
await client.index(index.uid).waitForTask(task.taskUid)

const response = await client.index(index.uid).getPagination()

expect(response).toEqual({ maxTotalHits: 1000 })
})

test(`${permission} key: Reset pagination`, async () => {
const client = await getClient(permission)
const newPagination = {
maxTotalHits: 100,
}
const updateTask = await client
.index(index.uid)
.updatePagination(newPagination)
await client.waitForTask(updateTask.taskUid)
const task = await client.index(index.uid).resetPagination()
await client.waitForTask(task.taskUid)

const response = await client.index(index.uid).getPagination()

expect(response).toEqual({ maxTotalHits: 1000 })
})
}
)

describe.each([{ permission: 'Public' }])(
'Test on pagination',
({ permission }) => {
beforeEach(async () => {
const client = await getClient('Master')
const { taskUid } = await client.createIndex(index.uid)
await client.waitForTask(taskUid)
})

test(`${permission} key: try to get pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).getPagination()
).rejects.toHaveProperty('code', ErrorStatusCode.INVALID_API_KEY)
})

test(`${permission} key: try to update pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).updatePagination({ maxTotalHits: 10 })
).rejects.toHaveProperty('code', ErrorStatusCode.INVALID_API_KEY)
})

test(`${permission} key: try to reset pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).resetPagination()
).rejects.toHaveProperty('code', ErrorStatusCode.INVALID_API_KEY)
})
}
)

describe.each([{ permission: 'No' }])(
'Test on pagination',
({ permission }) => {
beforeAll(async () => {
const client = await getClient('Master')
const { taskUid } = await client.createIndex(index.uid)
await client.waitForTask(taskUid)
})

test(`${permission} key: try to get pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).getPagination()
).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})

test(`${permission} key: try to update pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).updatePagination({ maxTotalHits: 10 })
).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})

test(`${permission} key: try to reset pagination and be denied`, async () => {
const client = await getClient(permission)
await expect(
client.index(index.uid).resetPagination()
).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
}
)

describe.each([
{ host: BAD_HOST, trailing: false },
{ host: `${BAD_HOST}/api`, trailing: false },
{ host: `${BAD_HOST}/trailing/`, trailing: true },
])('Tests on url construction', ({ host, trailing }) => {
test(`Test getPagination route`, async () => {
const route = `indexes/${index.uid}/settings/pagination`
const client = new MeiliSearch({ host })
const strippedHost = trailing ? host.slice(0, -1) : host
await expect(
client.index(index.uid).getPagination()
).rejects.toHaveProperty(
'message',
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
'http://',
''
)}`
)
})

test(`Test updatePagination route`, async () => {
const route = `indexes/${index.uid}/settings/pagination`
const client = new MeiliSearch({ host })
const strippedHost = trailing ? host.slice(0, -1) : host
await expect(
client.index(index.uid).updatePagination({ maxTotalHits: null })
).rejects.toHaveProperty(
'message',
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
'http://',
''
)}`
)
})

test(`Test resetPagination route`, async () => {
const route = `indexes/${index.uid}/settings/pagination`
const client = new MeiliSearch({ host })
const strippedHost = trailing ? host.slice(0, -1) : host
await expect(
client.index(index.uid).resetPagination()
).rejects.toHaveProperty(
'message',
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
'http://',
''
)}`
)
})
})
10 changes: 10 additions & 0 deletions tests/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', [])
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Get default settings of empty index with primary key`, async () => {
Expand All @@ -111,6 +112,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', [])
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Update settings`, async () => {
Expand Down Expand Up @@ -174,6 +176,9 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
faceting: {
maxValuesPerFacet: null,
},
pagination: {
maxTotalHits: null,
},
}
// Add the settings
const task = await client.index(index.uid).updateSettings(newSettings)
Expand Down Expand Up @@ -210,6 +215,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', newSettings.stopWords)
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Reset settings`, async () => {
Expand All @@ -227,6 +233,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', [])
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Reset settings of empty index`, async () => {
Expand All @@ -243,6 +250,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', [])
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Update searchableAttributes settings on empty index`, async () => {
Expand All @@ -268,6 +276,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', defaultSettings.stopWords)
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})

test(`${permission} key: Update searchableAttributes settings on empty index with a primary key`, async () => {
Expand Down Expand Up @@ -303,6 +312,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
expect(response).toHaveProperty('stopWords', defaultSettings.stopWords)
expect(response).toHaveProperty('synonyms', {})
expect(response).toHaveProperty('faceting', { maxValuesPerFacet: 100 })
expect(response).toHaveProperty('pagination', { maxTotalHits: 1000 })
})
}
)
Expand Down

0 comments on commit 77a858b

Please sign in to comment.