Skip to content

Commit

Permalink
feat(cts): generate tests for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Feb 28, 2024
1 parent 608fd29 commit 8a51cc3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
}
stepOut.put("stepTemplate", "tests/client/method.mustache");
stepOut.put("isMethod", true); // TODO: remove once dart and kotlin are converted
stepOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
stepOut.put("hasOperationParams", ope.hasParams);
}

stepOut.put("object", step.object);
stepOut.put("path", step.path);

Map<String, Object> requestOptions = new HashMap<>();
paramsType.enhanceParameters(step.requestOptions, requestOptions);
stepOut.put("requestOptions", requestOptions);
if (step.requestOptions != null) {
Map<String, Object> requestOptions = new HashMap<>();
paramsType.enhanceParameters(step.requestOptions, requestOptions);
stepOut.put("requestOptions", requestOptions);
}

if (step.path != null && CUSTOM_METHODS.contains(step.path)) {
stepOut.put("isCustom", true);
Expand Down Expand Up @@ -148,10 +152,11 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
stepOut.put("expectedError", step.expected.error.replace(step.path, Helpers.toPascalCase(step.path)));
}
} else if (step.expected.match != null) {
if (step.expected.match instanceof Map) {
Map<String, Object> match = new HashMap<>();
paramsType.enhanceParameters((Map<String, Object>) step.expected.match, match);
stepOut.put("match", match);
Map<String, Object> matchMap = new HashMap<>();
if (step.expected.match instanceof Map match) {
paramsType.enhanceParameters(match, matchMap);
stepOut.put("match", matchMap);
stepOut.put("matchIsObject", true);
} else {
stepOut.put("match", step.expected.match);
}
Expand Down
2 changes: 1 addition & 1 deletion specs/search/helpers/generateSecuredApiKey.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ method:
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.
parameters:
- in: query
name: apiKey
name: parentAPIKey
description: The search-only API key that the secured API key will inherit its restrictions from.
required: true
schema:
Expand Down
4 changes: 2 additions & 2 deletions templates/go/tests/client/method.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{^useEchoRequester}}res, err := {{/useEchoRequester}}{{#useEchoRequester}}_, err = {{/useEchoRequester}}client.{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}(client.NewApi{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}Request(
{{^useEchoRequester}}res, err := {{/useEchoRequester}}{{#useEchoRequester}}_, err = {{/useEchoRequester}}client.{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}({{^isHelper}}client.NewApi{{#lambda.titlecase}}{{path}}{{/lambda.titlecase}}Request({{/isHelper}}
{{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}
){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{#requestOptions}},
{{^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}}
{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.QueryParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.HeaderParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}}
{{/requestOptions}})
19 changes: 12 additions & 7 deletions templates/go/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ func Test{{#lambda.titlecase}}{{clientPrefix}}{{testType}}{{/lambda.titlecase}}{
require.NoError(t, err)
{{#match}}
{{#testUserAgent}}
require.Regexp(t, regexp.MustCompile(`{{{match}}}`), echo.Header.Get("User-Agent"))
require.Regexp(t, regexp.MustCompile(`{{{match}}}`), echo.Header.Get("User-Agent"))
{{/testUserAgent}}
{{#testTimeouts}}
require.Equal(t, int64({{{match.parametersWithDataTypeMap.connectTimeout.value}}}), echo.ConnectTimeout.Milliseconds())
require.Equal(t, int64({{{match.parametersWithDataTypeMap.responseTimeout.value}}}), echo.Timeout.Milliseconds())
require.Equal(t, int64({{{match.parametersWithDataTypeMap.connectTimeout.value}}}), echo.ConnectTimeout.Milliseconds())
require.Equal(t, int64({{{match.parametersWithDataTypeMap.responseTimeout.value}}}), echo.Timeout.Milliseconds())
{{/testTimeouts}}
{{#testHost}}
require.Equal(t, "{{{match}}}", echo.Host)
require.Equal(t, "{{{match}}}", echo.Host)
{{/testHost}}
{{#testResponse}}
rawBody, err := json.Marshal(res)
require.NoError(t, err)
require.JSONEq(t, `{{{match.parameters}}}`, string(rawBody))
{{#matchIsObject}}
rawBody, err := json.Marshal(res)
require.NoError(t, err)
require.JSONEq(t, `{{{match.parameters}}}`, string(rawBody))
{{/matchIsObject}}
{{^matchIsObject}}
require.Equal(t, `{{{match}}}`, res)
{{/matchIsObject}}
{{/testResponse}}
{{/match}}
{{/isError}}
Expand Down
2 changes: 1 addition & 1 deletion templates/python/tests/client/method.mustache
Original file line number Diff line number Diff line change
@@ -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}})
{{^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}})
25 changes: 25 additions & 0 deletions tests/CTS/client/search/helpers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"testName": "generate secured api key basic",
"steps": [
{
"type": "method",
"object": "$client",
"path": "generateSecuredApiKey",
"parameters": {
"parentAPIKey": "seed",
"restrictions": {
"validUntil": 0,
"restrictIndices": [
"index1"
]
}
},
"expected": {
"type": "response",
"match": "N2U0NWZjODgzODU1ZjFkODE1ZjZiZGNiZDE4MzczOGVlMDI0NTcxMTkyOGE5ZDliOWRmNTg1MzQ0YWNlYmZiOHZhbGlkVW50aWw9MHhjMDAwNWE0YWRjJnJlc3RyaWN0SW5kaWNlcz1pbmRleDE="
}
}
]
}
]

0 comments on commit 8a51cc3

Please sign in to comment.