-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dictionaries): adds methods and tests
- Loading branch information
Showing
21 changed files
with
585 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/client-search/src/__tests__/integration/dictionary.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { TestSuite } from '../../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const testSuite = new TestSuite('dictionary'); | ||
|
||
test(testSuite.testName, async () => { | ||
const client = testSuite.makeSearchClient('ALGOLIA_APPLICATION_ID_2', 'ALGOLIA_ADMIN_KEY_2') | ||
|
||
// Stopwords | ||
const stopwordEntryId = Math.floor(Math.random() * 10000).toString(); | ||
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0); | ||
|
||
const stopwordEntry = { | ||
objectID: stopwordEntryId, | ||
language: 'en', | ||
word: 'down', | ||
}; | ||
|
||
await client.saveDictionaryEntries('stopwords', [stopwordEntry]); | ||
|
||
const stopwords = await client.searchDictionaryEntries('stopwords', stopwordEntryId); | ||
expect(stopwords.nbHits).toEqual(1); | ||
expect(stopwords.hits[0].objectID).toEqual(stopwordEntry.objectID); | ||
expect(stopwords.hits[0].word).toEqual(stopwordEntry.word); | ||
|
||
await client.deleteDictionaryEntries('stopwords', [stopwordEntryId]); | ||
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0); | ||
|
||
const oldDictionaryState = await client.searchDictionaryEntries('stopwords', ''); | ||
const oldDictionaryEntries = oldDictionaryState.hits.map(hit => { | ||
delete hit.type; | ||
|
||
return hit; | ||
}); | ||
|
||
await client.saveDictionaryEntries('stopwords', [stopwordEntry]); | ||
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(1); | ||
|
||
await client.replaceDictionaryEntries('stopwords', oldDictionaryEntries); | ||
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0); | ||
|
||
const stopwordsSettings = { | ||
disableStandardEntries: { | ||
stopwords: { | ||
en: true, | ||
}, | ||
}, | ||
}; | ||
|
||
await client.setDictionarySettings(stopwordsSettings); | ||
expect(await client.getDictionarySettings()).toEqual(stopwordsSettings); | ||
|
||
// Plurals | ||
const pluralEntryId = Math.floor(Math.random() * 10000).toString(); | ||
expect(await client.searchDictionaryEntries('plurals', pluralEntryId).nbHits).toEqual(0); | ||
|
||
const pluralEntry = { | ||
objectID: pluralEntryId, | ||
language: 'fr', | ||
words: ['cheval', 'chevaux'], | ||
}; | ||
|
||
await client.saveDictionaryEntries('plurals', [pluralEntry]); | ||
|
||
const plurals = await client.searchDictionaryEntries('plurals', pluralEntryId); | ||
expect(plurals.nbHits).toEqual(1); | ||
expect(plurals.hits[0].objectID).toEqual(pluralEntry.objectID); | ||
expect(plurals.hits[0].words).toEqual(pluralEntry.words); | ||
|
||
await client.deleteDictionaryEntries('plurals', [pluralEntryId]); | ||
expect(await client.searchDictionaryEntries('plurals', pluralEntryId).nbHits).toEqual(0); | ||
|
||
// Compounds | ||
const compoundEntryId = Math.floor(Math.random() * 10000).toString(); | ||
expect(await client.searchDictionaryEntries('plurals', compoundEntryId).nbHits).toEqual(0); | ||
|
||
const compoundEntry = { | ||
objectID: compoundEntryId, | ||
language: 'fr', | ||
word: 'kopfschmerztablette', | ||
decomposition: ['kopf', 'schmerz', 'tablette'], | ||
}; | ||
|
||
await client.saveDictionaryEntries('plurals', [compoundEntry]); | ||
|
||
const compounds = await client.searchDictionaryEntries('plurals', compoundEntryId); | ||
expect(compounds.nbHits).toEqual(1); | ||
expect(compounds.hits[0].objectID).toEqual(compoundEntry.objectID); | ||
expect(compounds.hits[0].word).toEqual(compoundEntry.word); | ||
expect(compounds.hits[0].decomposition).toEqual(compoundEntry.decomposition); | ||
|
||
await client.deleteDictionaryEntries('plurals', [compoundEntryId]); | ||
expect(await client.searchDictionaryEntries('plurals', compoundEntryId).nbHits).toEqual(0); | ||
}); |
30 changes: 30 additions & 0 deletions
30
packages/client-search/src/methods/client/clearDictionaryEntries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createWaitablePromise, encode, WaitablePromise } from '@algolia/client-common'; | ||
import { MethodEnum } from '@algolia/requester-common'; | ||
import { RequestOptions } from '@algolia/transporter'; | ||
|
||
import { SaveDictionaryEntriesOptions, SaveDictionaryEntriesResponse, SearchClient } from '../..'; | ||
import { waitDictionaryTask } from '.'; | ||
|
||
// TODO: fill in SaveDictionaryEntriesOptions type | ||
export const clearDictionaryEntries = (base: SearchClient) => { | ||
return ( | ||
dictionary: string, | ||
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions | ||
): Readonly<WaitablePromise<SaveDictionaryEntriesResponse>> => { | ||
return createWaitablePromise<SaveDictionaryEntriesResponse>( | ||
base.transporter.write( | ||
{ | ||
method: MethodEnum.Post, | ||
path: encode('/1/dictionaries/%s/batch', dictionary), | ||
data: { | ||
clearExistingDictionaryEntries: true, | ||
requests: { action: 'addEntry', body: [] }, | ||
}, | ||
}, | ||
requestOptions | ||
), | ||
(response, waitRequestOptions) => | ||
waitDictionaryTask(base)(response.taskID, waitRequestOptions) | ||
); | ||
}; | ||
}; |
Oops, something went wrong.