Skip to content

Commit 58d4e81

Browse files
authored
docs(cts): update doc and client test doc (#766)
1 parent ef2275e commit 58d4e81

File tree

6 files changed

+83
-49
lines changed

6 files changed

+83
-49
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
106106
bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us");
107107
bundle.put("lambda", lambda);
108108

109-
if (language.equals("javascript")) {
110-
bundle.put("npmNamespace", Utils.getClientConfigField(language, "npmNamespace"));
111-
}
112-
113109
ctsManager.addDataToBundle(bundle);
114110

115111
for (TestsGenerator testGen : testsGenerators) {

generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void addSupportingFiles(List<SupportingFile> supportingFiles) {}
2020
@Override
2121
public void addDataToBundle(Map<String, Object> bundle) throws GeneratorException {
2222
bundle.put("utilsPackageVersion", Utils.getClientConfigField("javascript", "utilsPackageVersion"));
23+
bundle.put("npmNamespace", Utils.getClientConfigField("javascript", "npmNamespace"));
2324

2425
JsonNode openApiToolsConfig = Utils.readJsonFile("config/openapitools.json").get("generator-cli").get("generators");
2526

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,11 @@ private Map<String, Object> traverseParams(
123123
if (language.equals("java") && paramName.startsWith("_")) {
124124
finalParamName = paramName.substring(1);
125125
}
126-
Boolean isFirstLevel = suffix == 0;
127126

128127
Map<String, Object> testOutput = createDefaultOutput();
129128
testOutput.put("key", finalParamName);
130129
testOutput.put("parentSuffix", suffix - 1);
131-
testOutput.put("isFirstLevel", isFirstLevel);
132-
testOutput.put("hasGeneratedKey", finalParamName.matches("(.*)_[0-9]$"));
130+
testOutput.put("useAnonymousKey", !finalParamName.matches("(.*)_[0-9]$") && suffix != 0);
133131
testOutput.put("suffix", suffix);
134132
testOutput.put("parent", parent);
135133
testOutput.put("objectName", Utils.capitalize(baseType));
@@ -161,13 +159,11 @@ private Map<String, Object> traverseParams(String paramName, Object param, Strin
161159
if (language.equals("java") && paramName.startsWith("_")) {
162160
finalParamName = paramName.substring(1);
163161
}
164-
Boolean isFirstLevel = suffix == 0;
165162

166163
Map<String, Object> testOutput = createDefaultOutput();
167164
testOutput.put("key", finalParamName);
168165
testOutput.put("parentSuffix", suffix - 1);
169-
testOutput.put("isFirstLevel", isFirstLevel);
170-
testOutput.put("hasGeneratedKey", finalParamName.matches("(.*)_[0-9]$"));
166+
testOutput.put("useAnonymousKey", !finalParamName.matches("(.*)_[0-9]$") && suffix != 0);
171167
testOutput.put("suffix", suffix);
172168
testOutput.put("parent", parent);
173169
// cannot determine objectName with inference

templates/php/tests/generateParams.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{^isFirstLevel}}{{^hasGeneratedKey}}"{{{key}}}" => {{/hasGeneratedKey}}{{/isFirstLevel}}
1+
{{#useAnonymousKey}}"{{{key}}}" => {{/useAnonymousKey}}
22
{{#isNull}}
33
null,
44
{{/isNull}}

website/docs/contributing/add-new-language.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ This parameter **must** be the last parameter of a method, and allow a user to o
144144

145145
### Requesters
146146

147-
> TODO: informations
147+
The requester needs to be configurable and capable to be overwriten to use a user defined requester.
148+
The default requester must be the classic HTTP requester, and it's possible to provide other alternatives like an [Echo Requester](https://github.com/algolia/api-clients-automation/blob/main/clients/algoliasearch-client-javascript/packages/client-common/src/createEchoRequester.ts) that just send the request back.
148149

149150
### Logger
150151

website/docs/contributing/testing/common-test-suite.md

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ title: Common Test Suite
44

55
# Common Test Suite
66

7-
The CTS aims at ensuring minimal working operation for the API clients, by comparing the request formed by sample parameters.
8-
It is automatically generated for all languages, from a JSON entry point.
7+
The CTS gather multiple types of tests that make sure our API clients generate the correct requests and throws the correct error, it does not aim at testing the engine.
8+
It is automatically generated for all languages from JSON files and ensure properties are valid by checking specs.
99

1010
:::info
1111

@@ -17,7 +17,13 @@ Common Test Suite requires all clients to be built.
1717

1818
## How to add test
1919

20-
The test generation script requires a JSON file name from the `operationId` (e.g. `search.json`), located in the `CTS/<client>/requests/` folder (e.g. `CTS/search/requests/`).
20+
There are differents type of tests in the CTS:
21+
22+
### Requests tests
23+
24+
Those tests aims at ensuring minimal working operation for the API clients, by comparing the request formed by sample parameters.
25+
26+
The test generation script requires a JSON file name from the `operationId` (e.g. `search.json`), located in the `tests/CTS/methods/requests/<client>/` folder (e.g. `tests/CTS/methods/requests/search/`).
2127

2228
> See the [browse test file for the search client](https://github.com/algolia/api-clients-automation/blob/main/tests/CTS/methods/requests/search/browse.json)
2329
@@ -66,27 +72,40 @@ The test generation script requires a JSON file name from the `operationId` (e.g
6672
And that's it! If the name of the file matches an `operationId` in the spec, a test will be generated and will be calling the method name `operationId`.
6773

6874
The list of `queryParameters` must match exactly the actual value, the CTS has to check the number of query parameters and the value of each.
75+
**It's important to ensure the number of parameters because the API clients must not set default values, they should be handled by the engine.**
76+
77+
### Clients tests
78+
79+
The clients tests are located in the folder `tests/CTS/client/<client>`, they aim at testing the constructors and common error thrown by the API, and can be use to build more complex multi-step tests.
80+
81+
> TODO
6982
7083
## How to add a new language
7184

72-
Create a template in [`tests/CTS/methods/requests/templates/<languageName>/requests.mustache`](https://github.com/algolia/api-clients-automation/tree/main/tests/CTS/methods/requests/templates) that parses an array of tests into the test framework of choice.
85+
### Requests tests
7386

74-
> See [implementation of the JavaScript tests](https://github.com/algolia/api-clients-automation/blob/main/tests/CTS/methods/requests/templates/javascript/requests.mustache)
87+
Create a template in `templates/<languageName>/tests/requests.mustache` that parses an array of tests into the test framework of choice.
88+
89+
> See [implementation of the JavaScript tests](https://github.com/algolia/api-clients-automation/blob/main/templates/javascript/tests/requests/requests.mustache)
7590
7691
When writing your template, here is a list of variables accessible from `mustache`:
7792

7893
```json
7994
{
80-
"import": "the name of the package or library to import",
8195
"client": "the name of the API Client object to instanciate and import",
96+
"clientPrefix": "the name of the client without Client at the end",
97+
"hasRegionalHost": "true if the hosts accepts region",
98+
"defaultRegion": "the region to provide by default to the constructor",
8299
"blocks": [
83100
{
84101
// The list of test to implement
85102
"operationID": "the name of the endpoint and the cts file to test",
86103
"tests": [
87104
{
88-
"testName": "the descriptive name test (default to `method`)",
89105
"method": "the method to call on the API Client",
106+
"testName": "the descriptive name test (default to `method`)",
107+
"testIndex": "the index of the test to avoid duplicate function name",
108+
"hasParameters": "true if the method has parameters, useful for `GET` requests",
90109
"parameters": {
91110
// Object of all parameters with their name, to be used for languages that require the parameter name
92111
"parameterName": "value"
@@ -109,49 +128,45 @@ When writing your template, here is a list of variables accessible from `mustach
109128
"objectName": "SearchParams",
110129
// oneOfModel empty if there is no oneOf
111130
"oneOfModel": {
131+
// the compound type
112132
"parentClassName": "SearchParams",
113-
"type": "SearchParamsObject"
133+
"type": "SearchParamsObject",
134+
"x-one-of-explicit-name": "true if the name needs to be explicit (types are compatible)"
114135
},
115136
// properties used to have unique name and link to parent
116137
"parent": "theParentObject",
117138
"suffix": 7,
118139
"parentSuffix": 6,
140+
"useAnonymousKey": "true if we are in an array at the first level",
119141
// boolean indicating if it is the last parameter
120142
"-last": false
121143
}
122144
],
123-
"requestOptionsWithDataType": [
124-
{
125-
"key": "key",
126-
"value": "value",
127-
// booleans indicating the data type
128-
"isArray": false,
129-
"isObject": true,
130-
"isFreeFormObject": false,
131-
"isString": false,
132-
"isInteger": false,
133-
"isLong": false,
134-
"isDouble": false,
135-
"isEnum": false,
136-
"isBoolean": false,
137-
"objectName": "SearchParams",
138-
// oneOfModel empty if there is no oneOf
139-
"oneOfModel": {
140-
"parentClassName": "SearchParams",
141-
"type": "SearchParamsObject"
142-
},
143-
// properties used to have unique name and link to parent
144-
"parent": "theParentObject",
145-
"suffix": 7,
146-
"parentSuffix": 6,
147-
// boolean indicating if it is the last parameter
148-
"-last": false
145+
"parametersWithDataTypeMap": {
146+
"parameterName": {
147+
// Same as the parametersWithDataType, where the "key" is really a key
149148
}
150-
],
151-
// boolean indicating if the method has parameters, useful for `GET` requests
152-
"hasParameters": true,
149+
},
153150
// boolean indicating if the method has requestOptions, useful to shape your template only if provided
154151
"hasRequestOptions": true,
152+
"requestOptions": {
153+
"queryParameters": {
154+
// the raw JSON object
155+
"parameters": {},
156+
// list of parameters with enhance types
157+
"parametersWithDataType": [],
158+
// map of enhance parameters
159+
"parametersWithDataTypeMap": {}
160+
},
161+
"headers": {
162+
// the raw JSON object
163+
"parameters": {},
164+
// list of parameters with enhance types
165+
"parametersWithDataType": [],
166+
// map of enhance parameters
167+
"parametersWithDataTypeMap": {}
168+
},
169+
},
155170
"request": {
156171
"path": "the expected path of the request",
157172
"method": "the expected method: GET, POST, PUT, DELETE or PATCH",
@@ -174,6 +189,31 @@ When writing your template, here is a list of variables accessible from `mustach
174189
}
175190
```
176191

192+
As well as lambdas to transform strings:
193+
- `escapeQuotes` - Escapes double quotes characters, replaces `"` with `\"`.
194+
- `escapeSlash` - Escapes backward slash characters, replaces `\` with `\\`.
195+
- `lowercase` - Converts all of the characters in this fragment to lower case using the rules of the ROOT locale.
196+
- `uppercase` - Converts all of the characters in this fragment to upper case using the rules of the ROOT locale.
197+
- `titlecase` - Converts text in a fragment to title case. For example once upon a time to Once Upon A Time.
198+
- `camelcase` - Converts text in a fragment to camelCase. For example Input-text to inputText.
199+
- `indented` - Prepends 4 spaces indention from second line of a fragment on. First line will be indented by Mustache.
200+
- `indented_8` - Prepends 8 spaces indention from second line of a fragment on. First line will be indented by Mustache.
201+
- `indented_12` - Prepends 12 spaces indention from second line of a fragment on. First line will be indented by Mustache.
202+
- `indented_16` - Prepends 16 spaces indention from second line of a fragment on. First line will be indented by Mustache.
203+
204+
If specific values are needed for a specific languages, or custom generated files, they can be added using a custom CTS manager:
205+
- [javascript](https://github.com/algolia/api-clients-automation/blob/main/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java)
206+
- `npmNamespace`: the npm namespace
207+
- `utilsPackageVersion`: the utils version to import
208+
- `import`: the name of the package or library to import
209+
- [java](https://github.com/algolia/api-clients-automation/blob/main/generators/src/main/java/com/algolia/codegen/cts/manager/JavaCTSManager.java)
210+
- `packageVersion`: the version of the Java client
211+
- `import`: the name of the client package to import from
212+
213+
### Clients tests
214+
215+
> TODO
216+
177217
## Add common tests to every clients
178218

179219
You might want to test how every clients behaves, without having to duplicate the same tests. We provide 4 methods on every clients, common to all languages.

0 commit comments

Comments
 (0)