Skip to content

Commit

Permalink
feat(cts): add tests for Predict (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 10, 2022
1 parent 3e07f88 commit 7c78b03
Show file tree
Hide file tree
Showing 16 changed files with 841 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public Map<String, Object> postProcessSupportingFileData(
bundle.put("clientPrefix", Utils.createClientName(client, language));
bundle.put("import", createImportName());
bundle.put("hasRegionalHost", hasRegionalHost);
bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us");
bundle.put("lambda", lambda);

List<Object> blocks = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion playground/javascript/node/predict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const apiKey =
const userId = 'user1';

// Init client with appId and apiKey
const client = predictClient(appId, apiKey);
const client = predictClient(appId, apiKey, 'ew');

async function testPredict() {
try {
Expand Down
1 change: 1 addition & 0 deletions scripts/cts/client/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export async function generateClientTests(
client: `${createClientName(client, language)}Client`,
blocks: modifyForMustache(testsBlocks),
hasRegionalHost: hasRegionalHost ? true : undefined,
defaultRegion: client === 'predict' ? 'ew' : 'us',
},
partialTemplates
);
Expand Down
7 changes: 0 additions & 7 deletions specs/predict/common/enums.yml

This file was deleted.

38 changes: 38 additions & 0 deletions specs/predict/common/schemas/Params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
params:
oneOf:
- $ref: '#/modelsToRetrieve'
- $ref: '#/typesToRetrieve'
- $ref: '#/allParams'

allParams:
allOf:
- $ref: '#/modelsToRetrieve'
- $ref: '#/typesToRetrieve'

modelsToRetrieve:
type: object
required:
- modelsToRetrieve
properties:
modelsToRetrieve:
type: array
items:
$ref: '#/modelsToRetrieveEnum'

typesToRetrieve:
type: object
required:
- typesToRetrieve
properties:
typesToRetrieve:
type: array
items:
$ref: '#/typesToRetrieveEnum'

modelsToRetrieveEnum:
type: string
enum: [funnel_stage, order_value, affinities]

typesToRetrieveEnum:
type: string
enum: [properties, segments]
17 changes: 1 addition & 16 deletions specs/predict/paths/fetchUserProfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,7 @@ post:
content:
application/json:
schema:
title: params
type: object
description: Object with models and types to retrieve.
additionalProperties: false
properties:
modelsToRetrieve:
type: array
description: List with model types for which to retrieve predictions.
items:
$ref: '../common/enums.yml#/modelsToRetrieve'
typesToRetrieve:
type: array
description: List with types to be retrieved.
items:
$ref: '../common/enums.yml#/typesToRetrieve'
minItems: 1
$ref: '../common/schemas/Params.yml#/params'
responses:
'200':
description: OK
Expand Down
56 changes: 56 additions & 0 deletions tests/CTS/client/predict/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "fetchUserProfile",
"parameters": [
{
"userID": "user1",
"params": {
"modelsToRetrieve": [
"funnel_stage"
]
}
}
],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "fetchUserProfile",
"parameters": [
{
"userID": "user1",
"params": {
"modelsToRetrieve": [
"funnel_stage"
]
}
}
],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 2,
"responseTimeout": 30
}
}
}
}
]
}
]
36 changes: 36 additions & 0 deletions tests/CTS/client/predict/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is missing."
}
}
]
},
{
"testName": "does not throw when region is given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": "ew"
},
"expected": {
"error": false
}
}
]
}
]
2 changes: 1 addition & 1 deletion tests/CTS/client/templates/javascript/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const apiKey = 'test-api-key';

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

{{#blocks}}
Expand Down
80 changes: 80 additions & 0 deletions tests/CTS/methods/requests/predict/fetchUserProfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[
{
"testName": "fetchUserProfile with minimal parameters for modelsToRetrieve",
"parameters": {
"userID": "user1",
"params": {
"modelsToRetrieve": [
"funnel_stage",
"order_value",
"affinities"
]
}
},
"request": {
"path": "/1/users/user1/fetch",
"method": "POST",
"body": {
"modelsToRetrieve": [
"funnel_stage",
"order_value",
"affinities"
]
}
}
},
{
"testName": "fetchUserProfile with minimal parameters for typesToRetrieve",
"parameters": {
"userID": "user1",
"params": {
"typesToRetrieve": [
"properties",
"segments"
]
}
},
"request": {
"path": "/1/users/user1/fetch",
"method": "POST",
"body": {
"typesToRetrieve": [
"properties",
"segments"
]
}
}
},
{
"testName": "fetchUserProfile with all parameters",
"parameters": {
"userID": "user1",
"params": {
"modelsToRetrieve": [
"funnel_stage",
"order_value",
"affinities"
],
"typesToRetrieve": [
"properties",
"segments"
]
}
},
"request": {
"path": "/1/users/user1/fetch",
"method": "POST",
"body": {
"modelsToRetrieve": [
"funnel_stage",
"order_value",
"affinities"
],
"typesToRetrieve": [
"properties",
"segments"
]
}
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { EchoResponse } from '@experimental-api-clients-automation/client-c
const appId = process.env.ALGOLIA_APPLICATION_ID || 'test_app_id';
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'test_api_key';

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

{{#blocks}}
describe('{{operationId}}', () => {
Expand Down
Loading

0 comments on commit 7c78b03

Please sign in to comment.