Skip to content

Commit bdc5448

Browse files
committed
feat(cts): add tests for Predict
1 parent d320ae7 commit bdc5448

File tree

16 files changed

+841
-26
lines changed

16 files changed

+841
-26
lines changed

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public Map<String, Object> postProcessSupportingFileData(
145145
bundle.put("clientPrefix", Utils.createClientName(client, language));
146146
bundle.put("import", createImportName());
147147
bundle.put("hasRegionalHost", hasRegionalHost);
148+
bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us");
148149
bundle.put("lambda", lambda);
149150

150151
List<Object> blocks = new ArrayList<>();

playground/javascript/node/predict.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const apiKey =
1111
const userId = 'user1';
1212

1313
// Init client with appId and apiKey
14-
const client = predictClient(appId, apiKey);
14+
const client = predictClient(appId, apiKey, 'ew');
1515

1616
async function testPredict() {
1717
try {

scripts/cts/client/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export async function generateClientTests(
101101
client: `${createClientName(client, language)}Client`,
102102
blocks: modifyForMustache(testsBlocks),
103103
hasRegionalHost: hasRegionalHost ? true : undefined,
104+
defaultRegion: client === 'predict' ? 'ew' : 'us',
104105
},
105106
partialTemplates
106107
);

specs/predict/common/enums.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
params:
2+
oneOf:
3+
- $ref: '#/modelsToRetrieve'
4+
- $ref: '#/typesToRetrieve'
5+
- $ref: '#/allParams'
6+
7+
allParams:
8+
allOf:
9+
- $ref: '#/modelsToRetrieve'
10+
- $ref: '#/typesToRetrieve'
11+
12+
modelsToRetrieve:
13+
type: object
14+
required:
15+
- modelsToRetrieve
16+
properties:
17+
modelsToRetrieve:
18+
type: array
19+
items:
20+
$ref: '#/modelsToRetrieveEnum'
21+
22+
typesToRetrieve:
23+
type: object
24+
required:
25+
- typesToRetrieve
26+
properties:
27+
typesToRetrieve:
28+
type: array
29+
items:
30+
$ref: '#/typesToRetrieveEnum'
31+
32+
modelsToRetrieveEnum:
33+
type: string
34+
enum: [funnel_stage, order_value, affinities]
35+
36+
typesToRetrieveEnum:
37+
type: string
38+
enum: [properties, segments]

specs/predict/paths/fetchUserProfile.yml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@ post:
99
content:
1010
application/json:
1111
schema:
12-
title: params
13-
type: object
14-
description: Object with models and types to retrieve.
15-
additionalProperties: false
16-
properties:
17-
modelsToRetrieve:
18-
type: array
19-
description: List with model types for which to retrieve predictions.
20-
items:
21-
$ref: '../common/enums.yml#/modelsToRetrieve'
22-
typesToRetrieve:
23-
type: array
24-
description: List with types to be retrieved.
25-
items:
26-
$ref: '../common/enums.yml#/typesToRetrieve'
27-
minItems: 1
12+
$ref: '../common/schemas/Params.yml#/params'
2813
responses:
2914
'200':
3015
description: OK

tests/CTS/client/predict/api.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "fetchUserProfile",
9+
"parameters": [
10+
{
11+
"userID": "user1",
12+
"params": {
13+
"modelsToRetrieve": [
14+
"funnel_stage"
15+
]
16+
}
17+
}
18+
],
19+
"expected": {
20+
"testSubject": "actual.userAgent",
21+
"match": {
22+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
23+
}
24+
}
25+
}
26+
]
27+
},
28+
{
29+
"testName": "calls api with correct timeouts",
30+
"steps": [
31+
{
32+
"type": "method",
33+
"object": "$client",
34+
"path": "fetchUserProfile",
35+
"parameters": [
36+
{
37+
"userID": "user1",
38+
"params": {
39+
"modelsToRetrieve": [
40+
"funnel_stage"
41+
]
42+
}
43+
}
44+
],
45+
"expected": {
46+
"match": {
47+
"objectContaining": {
48+
"connectTimeout": 2,
49+
"responseTimeout": 30
50+
}
51+
}
52+
}
53+
}
54+
]
55+
}
56+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"testName": "throws when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": "`region` is missing."
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "does not throw when region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "ew"
29+
},
30+
"expected": {
31+
"error": false
32+
}
33+
}
34+
]
35+
}
36+
]

tests/CTS/client/templates/javascript/suite.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const apiKey = 'test-api-key';
1111

1212
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1313
function createClient() {
14-
return {{client}}(appId, apiKey, {{#hasRegionalHost}}'us', {{/hasRegionalHost}}{ requester: echoRequester() });
14+
return {{client}}(appId, apiKey, {{#hasRegionalHost}}'{{{defaultRegion}}}', {{/hasRegionalHost}}{ requester: echoRequester() });
1515
}
1616

1717
{{#blocks}}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[
2+
{
3+
"testName": "fetchUserProfile with minimal parameters for modelsToRetrieve",
4+
"parameters": {
5+
"userID": "user1",
6+
"params": {
7+
"modelsToRetrieve": [
8+
"funnel_stage",
9+
"order_value",
10+
"affinities"
11+
]
12+
}
13+
},
14+
"request": {
15+
"path": "/1/users/user1/fetch",
16+
"method": "POST",
17+
"body": {
18+
"modelsToRetrieve": [
19+
"funnel_stage",
20+
"order_value",
21+
"affinities"
22+
]
23+
}
24+
}
25+
},
26+
{
27+
"testName": "fetchUserProfile with minimal parameters for typesToRetrieve",
28+
"parameters": {
29+
"userID": "user1",
30+
"params": {
31+
"typesToRetrieve": [
32+
"properties",
33+
"segments"
34+
]
35+
}
36+
},
37+
"request": {
38+
"path": "/1/users/user1/fetch",
39+
"method": "POST",
40+
"body": {
41+
"typesToRetrieve": [
42+
"properties",
43+
"segments"
44+
]
45+
}
46+
}
47+
},
48+
{
49+
"testName": "fetchUserProfile with all parameters",
50+
"parameters": {
51+
"userID": "user1",
52+
"params": {
53+
"modelsToRetrieve": [
54+
"funnel_stage",
55+
"order_value",
56+
"affinities"
57+
],
58+
"typesToRetrieve": [
59+
"properties",
60+
"segments"
61+
]
62+
}
63+
},
64+
"request": {
65+
"path": "/1/users/user1/fetch",
66+
"method": "POST",
67+
"body": {
68+
"modelsToRetrieve": [
69+
"funnel_stage",
70+
"order_value",
71+
"affinities"
72+
],
73+
"typesToRetrieve": [
74+
"properties",
75+
"segments"
76+
]
77+
}
78+
}
79+
}
80+
]

tests/CTS/methods/requests/templates/javascript/requests.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { EchoResponse } from '@experimental-api-clients-automation/client-c
55
const appId = process.env.ALGOLIA_APPLICATION_ID || 'test_app_id';
66
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'test_api_key';
77

8-
const client = {{client}}(appId, apiKey, {{#hasRegionalHost}}'us', {{/hasRegionalHost}}{ requester: echoRequester() });
8+
const client = {{client}}(appId, apiKey, {{#hasRegionalHost}}'{{{defaultRegion}}}', {{/hasRegionalHost}}{ requester: echoRequester() });
99

1010
{{#blocks}}
1111
describe('{{operationId}}', () => {

0 commit comments

Comments
 (0)