|
7 | 7 | getClient, |
8 | 8 | dataset, |
9 | 9 | Book, |
| 10 | + getKey, |
| 11 | + HOST, |
10 | 12 | } from './utils/meilisearch-test-utils' |
11 | 13 |
|
12 | 14 | const indexNoPk = { |
@@ -140,6 +142,57 @@ describe('Documents tests', () => { |
140 | 142 | expect(documents.offset).toEqual(2) |
141 | 143 | }) |
142 | 144 |
|
| 145 | + test(`${permission} key: Get documents with filters`, async () => { |
| 146 | + const client = await getClient(permission) |
| 147 | + await client.index(indexPk.uid).updateFilterableAttributes(['id']) |
| 148 | + const { taskUid } = await client |
| 149 | + .index(indexPk.uid) |
| 150 | + .addDocuments(dataset) |
| 151 | + await client.waitForTask(taskUid) |
| 152 | + |
| 153 | + const documents = await client.index(indexPk.uid).getDocuments<Book>({ |
| 154 | + filter: [['id = 1', 'id = 2']], |
| 155 | + }) |
| 156 | + |
| 157 | + expect(documents.results.length).toEqual(2) |
| 158 | + }) |
| 159 | + |
| 160 | + test(`${permission} key: Get documents should trigger error with a MeilisearchCommunicationError`, async () => { |
| 161 | + const apiKey = await getKey(permission) |
| 162 | + const client = new MeiliSearch({ host: `${HOST}/indexes`, apiKey }) |
| 163 | + |
| 164 | + try { |
| 165 | + await client.index(indexPk.uid).getDocuments({ filter: '' }) |
| 166 | + |
| 167 | + fail( |
| 168 | + 'getDocuments should have raised an error when the route does not exist' |
| 169 | + ) |
| 170 | + } catch (e: any) { |
| 171 | + expect(e.message).toEqual( |
| 172 | + "Not Found\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires." |
| 173 | + ) |
| 174 | + } |
| 175 | + }) |
| 176 | + |
| 177 | + test(`${permission} key: Get documents should trigger error with a hint on a MeilisearchApiError`, async () => { |
| 178 | + const apiKey = await getKey(permission) |
| 179 | + const client = new MeiliSearch({ host: `${HOST}`, apiKey }) |
| 180 | + |
| 181 | + try { |
| 182 | + await client.index(indexPk.uid).getDocuments({ filter: 'id = 1' }) |
| 183 | + |
| 184 | + fail( |
| 185 | + 'getDocuments should have raised an error when the filter is badly formatted' |
| 186 | + ) |
| 187 | + } catch (e: any) { |
| 188 | + expect(e.message).toEqual( |
| 189 | + `Attribute \`id\` is not filterable. This index does not have configured filterable attributes. |
| 190 | +1:3 id = 1 |
| 191 | +Hint: It might not be working because maybe you're not up to date with the Meilisearch version that getDocuments call requires.` |
| 192 | + ) |
| 193 | + } |
| 194 | + }) |
| 195 | + |
143 | 196 | test(`${permission} key: Get documents from index that has NO primary key`, async () => { |
144 | 197 | const client = await getClient(permission) |
145 | 198 | const { taskUid } = await client |
@@ -417,6 +470,48 @@ describe('Documents tests', () => { |
417 | 470 | expect(response.results.length).toEqual(dataset.length) |
418 | 471 | }) |
419 | 472 |
|
| 473 | + test(`${permission} key: Delete some documents with string filters`, async () => { |
| 474 | + const client = await getClient(permission) |
| 475 | + await client.index(indexPk.uid).updateFilterableAttributes(['id']) |
| 476 | + const { taskUid: addDocTask } = await client |
| 477 | + .index(indexPk.uid) |
| 478 | + .addDocuments(dataset) |
| 479 | + await client.index(indexPk.uid).waitForTask(addDocTask) |
| 480 | + |
| 481 | + const task = await client |
| 482 | + .index(indexPk.uid) |
| 483 | + .deleteDocuments({ filter: 'id IN [1, 2]' }) |
| 484 | + |
| 485 | + const resolvedTask = await client |
| 486 | + .index(indexPk.uid) |
| 487 | + .waitForTask(task.taskUid) |
| 488 | + const documents = await client.index(indexPk.uid).getDocuments<Book>() |
| 489 | + |
| 490 | + expect(resolvedTask.details.deletedDocuments).toEqual(2) |
| 491 | + expect(documents.results.length).toEqual(dataset.length - 2) |
| 492 | + }) |
| 493 | + |
| 494 | + test(`${permission} key: Delete some documents with array filters`, async () => { |
| 495 | + const client = await getClient(permission) |
| 496 | + await client.index(indexPk.uid).updateFilterableAttributes(['id']) |
| 497 | + const { taskUid: addDocTask } = await client |
| 498 | + .index(indexPk.uid) |
| 499 | + .addDocuments(dataset) |
| 500 | + await client.index(indexPk.uid).waitForTask(addDocTask) |
| 501 | + |
| 502 | + const task = await client |
| 503 | + .index(indexPk.uid) |
| 504 | + .deleteDocuments({ filter: [['id = 1', 'id = 2']] }) |
| 505 | + |
| 506 | + const resolvedTask = await client |
| 507 | + .index(indexPk.uid) |
| 508 | + .waitForTask(task.taskUid) |
| 509 | + const documents = await client.index(indexPk.uid).getDocuments<Book>() |
| 510 | + |
| 511 | + expect(resolvedTask.details.deletedDocuments).toEqual(2) |
| 512 | + expect(documents.results.length).toEqual(dataset.length - 2) |
| 513 | + }) |
| 514 | + |
420 | 515 | test(`${permission} key: Delete some documents from index that has NO primary key`, async () => { |
421 | 516 | const client = await getClient(permission) |
422 | 517 | const { taskUid: addDocTask } = await client |
@@ -458,6 +553,41 @@ describe('Documents tests', () => { |
458 | 553 | expect(returnedIds).not.toContain(ids[1]) |
459 | 554 | }) |
460 | 555 |
|
| 556 | + test(`${permission} key: Delete some documents should trigger error with a hint on a MeilisearchApiError`, async () => { |
| 557 | + const client = await getClient(permission) |
| 558 | + const task = await client.createIndex(indexPk.uid) |
| 559 | + await client.waitForTask(task.taskUid) |
| 560 | + |
| 561 | + try { |
| 562 | + await client.index(indexPk.uid).deleteDocuments({ filter: '' }) |
| 563 | + |
| 564 | + fail( |
| 565 | + 'deleteDocuments should have raised an error when the parameters are wrong' |
| 566 | + ) |
| 567 | + } catch (e: any) { |
| 568 | + expect(e.message).toEqual( |
| 569 | + "Sending an empty filter is forbidden.\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that deleteDocuments call requires." |
| 570 | + ) |
| 571 | + } |
| 572 | + }) |
| 573 | + |
| 574 | + test(`${permission} key: Delete some documents should trigger error with a hint on a MeilisearchCommunicationError`, async () => { |
| 575 | + const apiKey = await getKey(permission) |
| 576 | + const client = new MeiliSearch({ host: `${HOST}/indexes`, apiKey }) |
| 577 | + |
| 578 | + try { |
| 579 | + await client.index(indexPk.uid).deleteDocuments({ filter: 'id = 1' }) |
| 580 | + |
| 581 | + fail( |
| 582 | + 'deleteDocuments should have raised an error when the route does not exist' |
| 583 | + ) |
| 584 | + } catch (e: any) { |
| 585 | + expect(e.message).toEqual( |
| 586 | + "Not Found\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that deleteDocuments call requires." |
| 587 | + ) |
| 588 | + } |
| 589 | + }) |
| 590 | + |
461 | 591 | test(`${permission} key: Delete all document from index that has NO primary key`, async () => { |
462 | 592 | const client = await getClient(permission) |
463 | 593 | const task = await client.index(indexNoPk.uid).deleteAllDocuments() |
|
0 commit comments