Skip to content

Commit

Permalink
fix(cts): add the remaining helpers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Aug 29, 2024
1 parent e1b2d50 commit 0d8ae6e
Show file tree
Hide file tree
Showing 23 changed files with 812 additions and 594 deletions.
3 changes: 3 additions & 0 deletions scripts/buildClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ async function buildLanguage(language: Language, gens: Generator[], buildType: B
case 'csharp':
await run('dotnet build --configuration Release', { cwd, language });
break;
case 'go':
await run('go build ./...', { cwd, language });
break;
case 'javascript':
await run('YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install', { cwd });
if (buildType === 'client') {
Expand Down
2 changes: 1 addition & 1 deletion scripts/cts/runCts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { printBenchmarkReport } from './testServer/benchmark.js';
import { assertChunkWrapperValid } from './testServer/chunkWrapper.js';
import { assertValidReplaceAllObjects } from './testServer/replaceAllObjects.js';
import { assertValidTimeouts } from './testServer/timeout.js';
import { assertValidWaitForApiKey } from './testServer/waitForApiKey.js';
import { assertValidWaitForApiKey } from './testServer/waitFor.js';

export type CTSType = 'benchmark' | 'client' | 'e2e' | 'requests';

Expand Down
2 changes: 1 addition & 1 deletion scripts/cts/testServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { gzipServer } from './gzip';
import { replaceAllObjectsServer } from './replaceAllObjects';
import { timeoutServer } from './timeout';
import { timeoutServerBis } from './timeoutBis';
import { waitForApiKeyServer } from './waitForApiKey';
import { waitForApiKeyServer } from './waitFor';

export async function startTestServer(suites: Record<CTSType, boolean>): Promise<() => Promise<void>> {
const toStart: Array<Promise<Server>> = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,20 @@ function addRoutes(app: Express): void {
throw new Error(`Invalid API key ${req.params.key}`);
}
});

app.get('/1/indexes/:indexName/task/:taskID', (req, res) => {
res.status(200).json({
status: 'published',
});
});

app.get('/1/task/:taskID', (req, res) => {
res.status(200).json({
status: 'published',
});
});
}

export function waitForApiKeyServer(): Promise<Server> {
return setupServer('waitForApiKey', 6681, addRoutes);
return setupServer('waitFor', 6681, addRoutes);
}
8 changes: 8 additions & 0 deletions specs/search/common/schemas/GetTaskResponse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: getTaskResponse
type: object
additionalProperties: false
properties:
status:
$ref: '../../../common/responses/common.yml#/taskStatus'
required:
- status
31 changes: 31 additions & 0 deletions specs/search/helpers/browseObjects.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
method:
get:
x-helper: true
tags:
- Records
operationId: browseObjects
summary: Get all records from an index
description: |
You can use the browse method to get records from an index—for example, to export your index as a backup. To export all records, use an empty query.
Use browse instead of search when exporting records from your index, when ranking, or analytics, isn't important. The Analytics API doesn't collect data when using browse.
Don't use this method for building a search UI. Use search instead.
parameters:
- in: query
name: indexName
description: The name of the index on which the operation was performed.
required: true
schema:
type: string
- in: query
name: browseParams
description: Browse parameters.
required: true
schema:
$ref: '../common/schemas/BrowseParams.yml#/browseParamsObject'
responses:
'204':
description: No content.
'400':
$ref: '../../common/responses/IndexNotFound.yml'
23 changes: 23 additions & 0 deletions specs/search/helpers/waitForAppTask.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
method:
get:
x-helper: true
operationId: waitForAppTask
summary: Wait for application-level operation to complete
description: Wait for a application-level task to complete.
parameters:
- in: query
name: taskID
description: The taskID returned by the operation.
required: true
schema:
type: integer
format: int64
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '../common/schemas/GetTaskResponse.yml'
'400':
$ref: '../../common/responses/IndexNotFound.yml'
34 changes: 34 additions & 0 deletions specs/search/helpers/waitForTask.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
method:
get:
x-helper: true
tags:
- Records
operationId: waitForTask
summary: Wait for operation to complete
description: |
Wait for a task to complete to ensure synchronized index updates.
All Algolia write operations are asynchronous. When you make a request for a write operation, for example, to add or update records in your index, Algolia creates a task on a queue and returns a taskID. The task itself runs separately, depending on the server load.
parameters:
- in: query
name: indexName
description: The name of the index on which the operation was performed.
required: true
schema:
type: string
- in: query
name: taskID
description: The taskID returned by the operation.
required: true
schema:
type: integer
format: int64
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '../common/schemas/GetTaskResponse.yml'
'400':
$ref: '../../common/responses/IndexNotFound.yml'
9 changes: 1 addition & 8 deletions specs/search/paths/advanced/getAppTask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ get:
content:
application/json:
schema:
title: getTaskResponse
type: object
additionalProperties: false
properties:
status:
$ref: '../../../common/responses/common.yml#/taskStatus'
required:
- status
$ref: '../../common/schemas/GetTaskResponse.yml'
'400':
$ref: '../../../common/responses/BadRequest.yml'
'402':
Expand Down
9 changes: 1 addition & 8 deletions specs/search/paths/advanced/getTask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ get:
content:
application/json:
schema:
title: getTaskResponse
type: object
additionalProperties: false
properties:
status:
$ref: '../../../common/responses/common.yml#/taskStatus'
required:
- status
$ref: '../../common/schemas/GetTaskResponse.yml'
'400':
$ref: '../../../common/responses/BadRequest.yml'
'402':
Expand Down
9 changes: 9 additions & 0 deletions specs/search/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ paths:
/waitForApiKey:
$ref: 'helpers/waitForApiKey.yml#/method'

/waitForTask:
$ref: 'helpers/waitForTask.yml#/method'

/waitForAppTask:
$ref: 'helpers/waitForAppTask.yml#/method'

/browseObjects:
$ref: 'helpers/browseObjects.yml#/method'

/generateSecuredApiKey:
$ref: 'helpers/generateSecuredApiKey.yml#/method'

Expand Down
6 changes: 3 additions & 3 deletions templates/go/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *APIClient) WaitForTask(
taskID int64,
opts ...IterableOption,
) (*GetTaskResponse, error) {
// provide a defalut timeout function
// provide a default timeout function
opts = append([]IterableOption{WithTimeout(func(count int) time.Duration {
return time.Duration(min(200*count, 5000)) * time.Millisecond
}), WithMaxRetries(50)}, opts...)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (c *APIClient) WaitForAppTask(
taskID int64,
opts ...IterableOption,
) (*GetTaskResponse, error) {
// provide a defalut timeout function
// provide a default timeout function
opts = append([]IterableOption{WithTimeout(func(count int) time.Duration {
return time.Duration(min(200*count, 5000)) * time.Millisecond
}), WithMaxRetries(50)}, opts...)
Expand Down Expand Up @@ -276,7 +276,7 @@ func (c *APIClient) WaitForApiKey(
}


// provide a defalut timeout function
// provide a default timeout function
opts = append([]WaitForApiKeyOption{WithTimeout(func(count int) time.Duration {
return time.Duration(min(200*count, 5000)) * time.Millisecond
}), WithMaxRetries(50)}, opts...)
Expand Down
32 changes: 16 additions & 16 deletions templates/java/api_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
TaskUtils.retryUntil(
public GetTaskResponse waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
return TaskUtils.retryUntil(
() -> this.getTask(indexName, taskID, requestOptions),
(GetTaskResponse task) -> task.getStatus() == TaskStatus.PUBLISHED,
maxRetries,
Expand All @@ -24,8 +24,8 @@ public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryO
* @param taskID The `taskID` returned in the method response.
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
public GetTaskResponse waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
return this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}

/**
Expand All @@ -36,8 +36,8 @@ public void waitForTask(String indexName, Long taskID, RequestOptions requestOpt
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
*/
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) {
this.waitForTask(indexName, taskID, maxRetries, timeout, null);
public GetTaskResponse waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) {
return this.waitForTask(indexName, taskID, maxRetries, timeout, null);
}

/**
Expand All @@ -46,8 +46,8 @@ public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryO
* @param indexName The `indexName` where the operation was performed.
* @param taskID The `taskID` returned in the method response.
*/
public void waitForTask(String indexName, Long taskID) {
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
public GetTaskResponse waitForTask(String indexName, Long taskID) {
return this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}

/**
Expand All @@ -60,8 +60,8 @@ public void waitForTask(String indexName, Long taskID) {
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public void waitForAppTask(Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
TaskUtils.retryUntil(
public GetTaskResponse waitForAppTask(Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
return TaskUtils.retryUntil(
() -> this.getAppTask(taskID, requestOptions),
(GetTaskResponse task) -> task.getStatus() == TaskStatus.PUBLISHED,
maxRetries,
Expand All @@ -76,8 +76,8 @@ TaskUtils.retryUntil(
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public void waitForAppTask(Long taskID, RequestOptions requestOptions) {
this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
public GetTaskResponse waitForAppTask(Long taskID, RequestOptions requestOptions) {
return this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}

/**
Expand All @@ -88,17 +88,17 @@ this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIM
* @param timeout The function to decide how long to wait between retries. min(retries * 200,
* 5000) by default. (optional)
*/
public void waitForAppTask(Long taskID, int maxRetries, IntUnaryOperator timeout) {
this.waitForAppTask(taskID, maxRetries, timeout, null);
public GetTaskResponse waitForAppTask(Long taskID, int maxRetries, IntUnaryOperator timeout) {
return this.waitForAppTask(taskID, maxRetries, timeout, null);
}

/**
* Helper: Wait for an application-level task to complete with `taskID`.
*
* @param taskID The `taskID` returned in the method response.
*/
public void waitForAppTask(Long taskID) {
this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
public GetTaskResponse waitForAppTask(Long taskID) {
return this.waitForAppTask(taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/CTS/client/search/browseObjects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "test-app-id",
"apiKey": "test-api-key",
"customHosts": [
{
"host": "${{localhost}}",
"port": 6681
}
]
}
},
{
"type": "method",
"method": "browseObjects",
"parameters": {
"indexName": "browse-objects-${{language}}",
"browseParams": {
"query": "batman",
"facets": [
"year"
]
}
}
}
]
}
]
44 changes: 44 additions & 0 deletions tests/CTS/client/search/deleteObjects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"testName": "call deleteObjects without error",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "test-app-id",
"apiKey": "test-api-key",
"customHosts": [
{
"host": "${{localhost}}",
"port": 6680
}
]
}
},
{
"type": "method",
"method": "deleteObjects",
"parameters": {
"indexName": "cts_e2e_deleteObjects_${{language}}",
"objectIDs": [
"1",
"2"
]
},
"expected": {
"type": "response",
"match": [
{
"taskID": 666,
"objectIDs": [
"1",
"2"
]
}
]
}
}
]
}
]
Loading

0 comments on commit 0d8ae6e

Please sign in to comment.