Skip to content

Commit 9f2a96d

Browse files
authored
feat(cts): group tests per method (#53)
1 parent e036bcf commit 9f2a96d

File tree

8 files changed

+131
-171
lines changed

8 files changed

+131
-171
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build:specs": "./scripts/build/specs.sh ${0:-all} ${1:-yaml}",
1212
"build": "yarn build:specs && yarn build:clients",
1313
"clean": "rm -rf **/dist **/build **/node_modules",
14-
"cts:generate": "yarn workspace tests cts:generate",
14+
"cts:generate": "yarn workspace tests start",
1515
"cts:test": "yarn workspace tests test",
1616
"docker:build": "./scripts/docker/build.sh",
1717
"docker:clean": "docker stop dev; docker rm -f dev; docker image rm -f api-clients-automation",
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// @ts-nocheck
22
import { {{client}}, EchoRequester } from '{{{import}}}';
33

4-
describe('Common Test Suite', () => {
5-
const client = new {{client}}(process.env.ALGOLIA_APPLICATION_ID, process.env.ALGOLIA_SEARCH_KEY, { requester: new EchoRequester() });
4+
const client = new {{client}}(process.env.ALGOLIA_APPLICATION_ID, process.env.ALGOLIA_SEARCH_KEY, { requester: new EchoRequester() });
65

7-
{{#tests}}
8-
test('{{testName}}', async () => {
9-
const req = await client.{{method}}({{#parameters}}{{{value}}}{{^-last}}, {{/-last}}{{/parameters}});
10-
expect(req).toMatchObject({
11-
path: '{{{request.path}}}',
12-
method: '{{{request.method}}}',
13-
{{#request.data}}data: {{{.}}},{{/request.data}}
14-
})
15-
});
6+
{{#blocks}}
7+
describe('{{operationId}}', () => {
8+
{{#tests}}
9+
test('{{testName}}', async () => {
10+
const req = await client.{{method}}({{#parameters}}{{{value}}}{{^-last}}, {{/-last}}{{/parameters}});
11+
expect(req).toMatchObject({
12+
path: '{{{request.path}}}',
13+
method: '{{{request.method}}}',
14+
{{#request.data}}data: {{{.}}},{{/request.data}}
15+
})
16+
});
1617

17-
{{/tests}}
18-
});
18+
{{/tests}}
19+
})
20+
21+
{{/blocks}}

tests/generateCTS.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import fsp from 'fs/promises';
33
import path from 'path';
44

5+
import SwaggerParser from '@apidevtools/swagger-parser';
56
import Mustache from 'mustache';
67
import type { OpenAPIV3 } from 'openapi-types';
7-
import SwaggerParser from 'swagger-parser';
88

99
import openapitools from '../openapitools.json';
1010

1111
const availableLanguages = ['javascript'] as const;
1212
type Language = typeof availableLanguages[number];
1313

14-
type CTSBlock = {
14+
type Tests = {
1515
testName?: string;
1616
method: string;
1717
parameters: any[];
@@ -22,6 +22,11 @@ type CTSBlock = {
2222
};
2323
};
2424

25+
type CTSBlock = {
26+
operationId: string;
27+
tests: Tests[];
28+
};
29+
2530
// Array of test per client
2631
type CTS = Record<string, CTSBlock[]>;
2732

@@ -68,6 +73,10 @@ function capitalize(str: string): string {
6873
async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
6974
// load the list of operations from the spec
7075
const spec = await SwaggerParser.validate(`../specs/${client}/spec.yml`);
76+
if (!spec.paths) {
77+
throw new Error(`No paths found for spec ${client}/spec.yml`);
78+
}
79+
7180
const operations = Object.values(spec.paths)
7281
.flatMap<OpenAPIV3.OperationObject>((p) => Object.values(p))
7382
.map((obj) => obj.operationId);
@@ -78,22 +87,18 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
7887
if (!file.name.endsWith('json')) {
7988
continue;
8089
}
81-
const operationId = file.name.replace('.json', '');
90+
const fileName = file.name.replace('.json', '');
8291
const fileContent = (await fsp.readFile(file.path)).toString();
8392

8493
if (!fileContent) {
85-
throw new Error(
86-
`cannot read empty file for operationId ${operationId} - ${client} client`
87-
);
94+
throw new Error(`cannot read empty file ${fileName} - ${client} client`);
8895
}
8996

90-
const tests: CTSBlock[] = JSON.parse(fileContent);
97+
const tests: Tests[] = JSON.parse(fileContent);
9198

9299
// check test validity against spec
93-
if (!operations.includes(operationId)) {
94-
throw new Error(
95-
`cannot find operationId ${operationId} for the ${client} client`
96-
);
100+
if (!operations.includes(fileName)) {
101+
throw new Error(`cannot find ${fileName} for the ${client} client`);
97102
}
98103

99104
for (const test of tests) {
@@ -116,8 +121,13 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
116121
// stringify request.data too
117122
test.request.data = JSON.stringify(test.request.data);
118123
}
119-
ctsClient.push(...tests);
124+
125+
ctsClient.push({
126+
operationId: fileName,
127+
tests,
128+
});
120129
}
130+
121131
return ctsClient;
122132
}
123133

@@ -142,7 +152,7 @@ async function generateCode(language: Language): Promise<void> {
142152
const code = Mustache.render(template, {
143153
import: packageNames[language][client],
144154
client: `${capitalize(client)}Api`,
145-
tests: cts[client],
155+
blocks: cts[client],
146156
});
147157
await fsp.writeFile(
148158
`output/${language}/${client}${extensionForLanguage[language]}`,

tests/output/javascript/recommend.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @ts-nocheck
22
import { RecommendApi, EchoRequester } from '@algolia/recommend';
33

4-
describe('Common Test Suite', () => {
5-
const client = new RecommendApi(
6-
process.env.ALGOLIA_APPLICATION_ID,
7-
process.env.ALGOLIA_SEARCH_KEY,
8-
{ requester: new EchoRequester() }
9-
);
4+
const client = new RecommendApi(
5+
process.env.ALGOLIA_APPLICATION_ID,
6+
process.env.ALGOLIA_SEARCH_KEY,
7+
{ requester: new EchoRequester() }
8+
);
109

10+
describe('getRecommendations', () => {
1111
test('get recommendations with minimal parameters', async () => {
1212
const req = await client.getRecommendations({
1313
requests: [

0 commit comments

Comments
 (0)