Skip to content

Commit 518055b

Browse files
authored
feat(cts): add custom request tests (#331)
1 parent 8fcdbc1 commit 518055b

File tree

24 files changed

+1248
-54
lines changed

24 files changed

+1248
-54
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.2.0
1+
7.2.1.0.1

.github/workflows/check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ jobs:
277277
hashFiles(
278278
format('{0}/lib/Api/{1}.php', matrix.client.folder, matrix.client.api),
279279
format('{0}/lib/Configuration/{1}.php', matrix.client.folder, matrix.client.config),
280+
format('{0}/lib/Model/{1}/**', matrix.client.folder, matrix.client.capitalizedName),
280281
format('specs/bundled/{0}.yml', matrix.client.name),
281282
'templates/php/**',
282283
'generators/src/**'

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ nodeLinker: node-modules
22

33
plugins:
44
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
5-
spec: "@yarnpkg/plugin-interactive-tools"
5+
spec: '@yarnpkg/plugin-interactive-tools'
66

77
yarnPath: .yarn/releases/yarn-3.1.1.cjs

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ public Map<String, Object> postProcessSupportingFileData(
170170
System.out.println(e.getMessage());
171171
System.exit(0);
172172
}
173+
174+
System.out.println(e.getMessage());
175+
System.exit(1);
173176
} catch (Exception e) {
174177
e.printStackTrace();
175178
System.exit(1);
@@ -181,15 +184,28 @@ private Map<String, Request[]> loadCTS()
181184
throws JsonParseException, JsonMappingException, IOException, CTSException {
182185
TreeMap<String, Request[]> cts = new TreeMap<>();
183186
File dir = new File("tests/CTS/methods/requests/" + client);
187+
File commonTestDir = new File("tests/CTS/methods/requests/common");
184188
if (!dir.exists()) {
185189
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true);
186190
}
191+
if (!commonTestDir.exists()) {
192+
throw new CTSException(
193+
"CTS not found at " + commonTestDir.getAbsolutePath(),
194+
true
195+
);
196+
}
187197
for (File f : dir.listFiles()) {
188198
cts.put(
189199
f.getName().replace(".json", ""),
190200
Json.mapper().readValue(f, Request[].class)
191201
);
192202
}
203+
for (File f : commonTestDir.listFiles()) {
204+
cts.put(
205+
f.getName().replace(".json", ""),
206+
Json.mapper().readValue(f, Request[].class)
207+
);
208+
}
193209
return cts;
194210
}
195211

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ private void handleModel(
200200
String parent,
201201
int suffix
202202
) throws CTSException {
203-
assert (spec.getHasVars());
204-
assert (spec.getItems() == null);
203+
if (!spec.getHasVars()) {
204+
throw new CTSException("Spec has no vars.");
205+
}
206+
207+
if (spec.getItems() != null) {
208+
throw new CTSException("Spec has items.");
209+
}
205210

206211
if (
207212
spec instanceof CodegenModel && ((CodegenModel) spec).oneOf.size() > 0
@@ -269,8 +274,13 @@ private void handleObject(
269274
IJsonSchemaValidationProperties spec,
270275
int suffix
271276
) throws CTSException {
272-
assert (!spec.getHasVars());
273-
assert (spec.getItems() == null);
277+
if (spec.getHasVars()) {
278+
throw new CTSException("Spec has vars.");
279+
}
280+
281+
if (spec.getItems() != null) {
282+
throw new CTSException("Spec has items.");
283+
}
274284

275285
Map<String, Object> vars = (Map<String, Object>) param;
276286

@@ -300,18 +310,37 @@ private void handleMap(
300310
IJsonSchemaValidationProperties spec,
301311
int suffix
302312
) throws CTSException {
303-
assert (!spec.getHasVars());
304-
assert (spec.getItems() != null);
313+
if (spec.getHasVars()) {
314+
throw new CTSException("Spec has vars.");
315+
}
305316

306317
Map<String, Object> vars = (Map<String, Object>) param;
307318

308319
List<Object> values = new ArrayList<>();
320+
321+
CodegenProperty items = spec.getItems();
322+
309323
for (Entry<String, Object> entry : vars.entrySet()) {
324+
IJsonSchemaValidationProperties itemType = items;
325+
326+
// The generator consider a free form object type as an `object`, which
327+
// is wrong in our case, so we infer it.
328+
if (
329+
items == null ||
330+
(items.openApiType.equals("object") && items.isFreeFormObject)
331+
) {
332+
CodegenParameter maybeMatch = new CodegenParameter();
333+
String paramType = inferDataType(entry.getValue(), maybeMatch, null);
334+
335+
maybeMatch.dataType = paramType;
336+
itemType = maybeMatch;
337+
}
338+
310339
values.add(
311340
traverseParams(
312341
entry.getKey(),
313342
entry.getValue(),
314-
spec.getItems(),
343+
itemType,
315344
paramName,
316345
suffix + 1
317346
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@algolia/api-client-automation",
33
"version": "0.0.0",
4+
"private": true,
45
"workspaces": [
56
"clients/algoliasearch-client-javascript/",
67
"playground/javascript/node/",

scripts/cts/client/generate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { createSpinner } from '../../oraLog';
77
import type { Generator } from '../../types';
88
import {
99
walk,
10-
createClientName,
1110
createOutputDir,
1211
getOutputPath,
1312
loadTemplates,
13+
createClientName,
1414
} from '../utils';
1515

1616
import type { TestsBlock, Test, ModifiedStepForMustache } from './types';
@@ -98,7 +98,7 @@ export async function generateClientTests(
9898
template,
9999
{
100100
import: packageName,
101-
client: createClientName(client, language),
101+
client: `${createClientName(client, language)}Api`,
102102
blocks: modifyForMustache(testsBlocks),
103103
hasRegionalHost: hasRegionalHost ? true : undefined,
104104
},

scripts/cts/utils.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@ describe('utils', () => {
2020

2121
describe('createClientName', () => {
2222
it('does not capitalize every part for JavaScript', () => {
23-
expect(createClientName('search', 'javascript')).toEqual('searchApi');
23+
expect(createClientName('search', 'javascript')).toEqual('search');
2424
expect(createClientName('search-client', 'javascript')).toEqual(
25-
'searchClientApi'
25+
'searchClient'
2626
);
2727
expect(createClientName('search-cli!nt-complex', 'javascript')).toEqual(
28-
'searchCli!ntComplexApi'
28+
'searchCli!ntComplex'
2929
);
3030
});
3131

3232
it('capitalize every part for other languages', () => {
33-
expect(createClientName('search', 'java')).toEqual('SearchApi');
34-
expect(createClientName('search-client', 'java')).toEqual(
35-
'SearchClientApi'
36-
);
33+
expect(createClientName('search', 'java')).toEqual('Search');
34+
expect(createClientName('search-client', 'java')).toEqual('SearchClient');
3735
expect(createClientName('search-cli!nt-complex', 'java')).toEqual(
38-
'SearchCli!ntComplexApi'
36+
'SearchCli!ntComplex'
3937
);
4038
});
4139
});

scripts/cts/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export function createClientName(client: string, language: string): string {
3030
})
3131
.join('');
3232

33-
return `${clientName}Api`;
33+
return clientName;
3434
}
35+
3536
export async function createOutputDir({
3637
language,
3738
testPath,

specs/common/paths/customRequest.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,4 @@ put:
2424

2525
delete:
2626
operationId: del
27-
requestBody:
28-
description: The parameters to send with the custom request.
29-
content:
30-
application/json:
31-
schema:
32-
type: object
3327
$ref: '../schemas/CustomRequest.yml#/customRequest'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"method": "del",
4+
"testName": "allow del method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "DELETE"
11+
}
12+
},
13+
{
14+
"method": "del",
15+
"testName": "allow del method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
}
21+
},
22+
"request": {
23+
"path": "/1/test/all",
24+
"method": "DELETE",
25+
"searchParams": {
26+
"query": "parameters"
27+
}
28+
}
29+
}
30+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"method": "get",
4+
"testName": "allow get method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "GET"
11+
}
12+
},
13+
{
14+
"method": "get",
15+
"testName": "allow get method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
}
21+
},
22+
"request": {
23+
"path": "/1/test/all",
24+
"method": "GET",
25+
"searchParams": {
26+
"query": "parameters"
27+
}
28+
}
29+
}
30+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"method": "post",
4+
"testName": "allow post method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "POST"
11+
}
12+
},
13+
{
14+
"method": "post",
15+
"testName": "allow post method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
},
21+
"body": {
22+
"body": "parameters"
23+
}
24+
},
25+
"request": {
26+
"path": "/1/test/all",
27+
"method": "POST",
28+
"data": {
29+
"body": "parameters"
30+
},
31+
"searchParams": {
32+
"query": "parameters"
33+
}
34+
}
35+
}
36+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"method": "put",
4+
"testName": "allow put method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "PUT"
11+
}
12+
},
13+
{
14+
"method": "put",
15+
"testName": "allow put method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
},
21+
"body": {
22+
"body": "parameters"
23+
}
24+
},
25+
"request": {
26+
"path": "/1/test/all",
27+
"method": "PUT",
28+
"data": {
29+
"body": "parameters"
30+
},
31+
"searchParams": {
32+
"query": "parameters"
33+
}
34+
}
35+
}
36+
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class {{client}}Tests {
5050

5151
{{#request.searchParams}}
5252
HashMap<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.searchParams}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
53-
List<Pair> acutalQuery = req.getQueryParams();
54-
for (Pair p : acutalQuery) {
53+
List<Pair> actualQuery = req.getQueryParams();
54+
for (Pair p : actualQuery) {
5555
assertEquals(expectedQuery.get(p.getName()), p.getValue());
5656
}
5757
{{/request.searchParams}}

0 commit comments

Comments
 (0)