Skip to content

Commit 8a51cc3

Browse files
committed
feat(cts): generate tests for helpers
1 parent 608fd29 commit 8a51cc3

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

generators/src/main/java/com/algolia/codegen/cts/tests/TestsClient.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,18 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
9393
}
9494
stepOut.put("stepTemplate", "tests/client/method.mustache");
9595
stepOut.put("isMethod", true); // TODO: remove once dart and kotlin are converted
96+
stepOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
97+
stepOut.put("hasOperationParams", ope.hasParams);
9698
}
9799

98100
stepOut.put("object", step.object);
99101
stepOut.put("path", step.path);
100102

101-
Map<String, Object> requestOptions = new HashMap<>();
102-
paramsType.enhanceParameters(step.requestOptions, requestOptions);
103-
stepOut.put("requestOptions", requestOptions);
103+
if (step.requestOptions != null) {
104+
Map<String, Object> requestOptions = new HashMap<>();
105+
paramsType.enhanceParameters(step.requestOptions, requestOptions);
106+
stepOut.put("requestOptions", requestOptions);
107+
}
104108

105109
if (step.path != null && CUSTOM_METHODS.contains(step.path)) {
106110
stepOut.put("isCustom", true);
@@ -148,10 +152,11 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
148152
stepOut.put("expectedError", step.expected.error.replace(step.path, Helpers.toPascalCase(step.path)));
149153
}
150154
} else if (step.expected.match != null) {
151-
if (step.expected.match instanceof Map) {
152-
Map<String, Object> match = new HashMap<>();
153-
paramsType.enhanceParameters((Map<String, Object>) step.expected.match, match);
154-
stepOut.put("match", match);
155+
Map<String, Object> matchMap = new HashMap<>();
156+
if (step.expected.match instanceof Map match) {
157+
paramsType.enhanceParameters(match, matchMap);
158+
stepOut.put("match", matchMap);
159+
stepOut.put("matchIsObject", true);
155160
} else {
156161
stepOut.put("match", step.expected.match);
157162
}

specs/search/helpers/generateSecuredApiKey.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ method:
1414
If you want to limit the number of requests that can be made with a secured API key, you must also rate-limit the key that you use to generate it. You can create a rate-limited key in the Algolia dashboard or use the Add API key or Update API key methods of an API client.
1515
parameters:
1616
- in: query
17-
name: apiKey
17+
name: parentAPIKey
1818
description: The search-only API key that the secured API key will inherit its restrictions from.
1919
required: true
2020
schema:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{{^useEchoRequester}}res, err := {{/useEchoRequester}}{{#useEchoRequester}}_, err = {{/useEchoRequester}}client.{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}(client.NewApi{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}Request(
1+
{{^useEchoRequester}}res, err := {{/useEchoRequester}}{{#useEchoRequester}}_, err = {{/useEchoRequester}}client.{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}({{^isHelper}}client.NewApi{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}Request({{/isHelper}}
22
{{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}
3-
){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{#requestOptions}},
3+
{{^isHelper}}){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{/isHelper}}{{#isHelper}}{{#parametersWithDataType}}{{^required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}{{/isHelper}}{{#requestOptions}}{{#hasOperationParams}},{{/hasOperationParams}}
44
{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.QueryParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.HeaderParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}}
55
{{/requestOptions}})

templates/go/tests/client/suite.mustache

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,24 @@ func Test{{#lambda.titlecase}}{{clientPrefix}}{{testType}}{{/lambda.titlecase}}{
5757
require.NoError(t, err)
5858
{{#match}}
5959
{{#testUserAgent}}
60-
require.Regexp(t, regexp.MustCompile(`{{{match}}}`), echo.Header.Get("User-Agent"))
60+
require.Regexp(t, regexp.MustCompile(`{{{match}}}`), echo.Header.Get("User-Agent"))
6161
{{/testUserAgent}}
6262
{{#testTimeouts}}
63-
require.Equal(t, int64({{{match.parametersWithDataTypeMap.connectTimeout.value}}}), echo.ConnectTimeout.Milliseconds())
64-
require.Equal(t, int64({{{match.parametersWithDataTypeMap.responseTimeout.value}}}), echo.Timeout.Milliseconds())
63+
require.Equal(t, int64({{{match.parametersWithDataTypeMap.connectTimeout.value}}}), echo.ConnectTimeout.Milliseconds())
64+
require.Equal(t, int64({{{match.parametersWithDataTypeMap.responseTimeout.value}}}), echo.Timeout.Milliseconds())
6565
{{/testTimeouts}}
6666
{{#testHost}}
67-
require.Equal(t, "{{{match}}}", echo.Host)
67+
require.Equal(t, "{{{match}}}", echo.Host)
6868
{{/testHost}}
6969
{{#testResponse}}
70-
rawBody, err := json.Marshal(res)
71-
require.NoError(t, err)
72-
require.JSONEq(t, `{{{match.parameters}}}`, string(rawBody))
70+
{{#matchIsObject}}
71+
rawBody, err := json.Marshal(res)
72+
require.NoError(t, err)
73+
require.JSONEq(t, `{{{match.parameters}}}`, string(rawBody))
74+
{{/matchIsObject}}
75+
{{^matchIsObject}}
76+
require.Equal(t, `{{{match}}}`, res)
77+
{{/matchIsObject}}
7378
{{/testResponse}}
7479
{{/match}}
7580
{{/isError}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{^isError}}_req = {{/isError}}await self._client.{{#lambda.snakecase}}{{{path}}}{{/lambda.snakecase}}{{#useEchoRequester}}_with_http_info{{/useEchoRequester}}({{#parametersWithDataType}}{{> tests/requests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}} request_options={ {{#requestOptions.headers.parameters}}"headers":loads("""{{{.}}}"""),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}"query_parameters":loads("""{{{.}}}"""),{{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})
1+
{{^isError}}_req = {{/isError}}{{^isHelper}}await {{/isHelper}}self._client.{{#lambda.snakecase}}{{{path}}}{{/lambda.snakecase}}{{#useEchoRequester}}_with_http_info{{/useEchoRequester}}({{#parametersWithDataType}}{{> tests/requests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}} request_options={ {{#requestOptions.headers.parameters}}"headers":loads("""{{{.}}}"""),{{/requestOptions.headers.parameters}}{{#requestOptions.queryParameters.parameters}}"query_parameters":loads("""{{{.}}}"""),{{/requestOptions.queryParameters.parameters}} }{{/hasRequestOptions}})

tests/CTS/client/search/helpers.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"testName": "generate secured api key basic",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "generateSecuredApiKey",
9+
"parameters": {
10+
"parentAPIKey": "seed",
11+
"restrictions": {
12+
"validUntil": 0,
13+
"restrictIndices": [
14+
"index1"
15+
]
16+
}
17+
},
18+
"expected": {
19+
"type": "response",
20+
"match": "N2U0NWZjODgzODU1ZjFkODE1ZjZiZGNiZDE4MzczOGVlMDI0NTcxMTkyOGE5ZDliOWRmNTg1MzQ0YWNlYmZiOHZhbGlkVW50aWw9MHhjMDAwNWE0YWRjJnJlc3RyaWN0SW5kaWNlcz1pbmRleDE="
21+
}
22+
}
23+
]
24+
}
25+
]

0 commit comments

Comments
 (0)