Skip to content

Commit 407fab5

Browse files
authored
Add tests on primary key inference for v1.0.0 of Meilisearch (#1436)
1 parent 245eeae commit 407fab5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/documents.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,74 @@ describe('Documents tests', () => {
319319
expect(documents.results.length).toEqual(dataset.length + 1)
320320
})
321321

322+
test(`${permission} key: Add document and infer correct "_id" primary key`, async () => {
323+
const client = await getClient(permission)
324+
325+
const doc = {
326+
title: 'hello',
327+
_id: 1,
328+
}
329+
const { taskUid: addDocTask } = await client
330+
.index(indexNoPk.uid)
331+
.addDocuments([doc])
332+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
333+
334+
const index = await client.index(indexNoPk.uid).fetchInfo()
335+
336+
expect(index).toHaveProperty('primaryKey', '_id')
337+
})
338+
339+
test(`${permission} key: Add document and infer correct "findmeid" primary key`, async () => {
340+
const client = await getClient(permission)
341+
const doc = {
342+
title: 'hello',
343+
findmeid: 1,
344+
}
345+
const { taskUid: addDocTask } = await client
346+
.index(indexNoPk.uid)
347+
.addDocuments([doc])
348+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
349+
350+
const index = await client.index(indexNoPk.uid).fetchInfo()
351+
352+
expect(index).toHaveProperty('primaryKey', 'findmeid')
353+
})
354+
355+
test(`${permission} key: Add document with two inferable primary key`, async () => {
356+
const client = await getClient(permission)
357+
const doc = {
358+
title: 'hello',
359+
id: 1,
360+
_id: 1,
361+
}
362+
const { taskUid: addDocTask } = await client
363+
.index(indexNoPk.uid)
364+
.addDocuments([doc])
365+
const task = await client.index(indexNoPk.uid).waitForTask(addDocTask)
366+
const index = await client.index(indexNoPk.uid).fetchInfo()
367+
368+
expect(task.error?.code).toEqual(
369+
'index_primary_key_multiple_candidates_found'
370+
)
371+
expect(index).toHaveProperty('primaryKey', null)
372+
})
373+
374+
test(`${permission} key: Add document with none inferable primary key`, async () => {
375+
const client = await getClient(permission)
376+
const doc = {
377+
title: 'hello',
378+
idfindme: 1,
379+
}
380+
const { taskUid: addDocTask } = await client
381+
.index(indexNoPk.uid)
382+
.addDocuments([doc])
383+
const task = await client.index(indexNoPk.uid).waitForTask(addDocTask)
384+
const index = await client.index(indexNoPk.uid).fetchInfo()
385+
386+
expect(task.error?.code).toEqual('index_primary_key_no_candidate_found')
387+
expect(index).toHaveProperty('primaryKey', null)
388+
})
389+
322390
test(`${permission} key: Delete a document from index that has NO primary key`, async () => {
323391
const client = await getClient(permission)
324392
const { taskUid: addDocTask } = await client

0 commit comments

Comments
 (0)