Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:specs": "./scripts/build/specs.sh ${0:-all} ${1:-yaml}",
"build": "yarn build:specs && yarn build:clients",
"clean": "rm -rf **/dist **/build **/node_modules",
"cts:generate": "yarn workspace tests cts:generate",
"cts:generate": "yarn workspace tests start",
"cts:test": "yarn workspace tests test",
"docker:build": "./scripts/docker/build.sh",
"docker:clean": "docker stop dev; docker rm -f dev; docker image rm -f api-clients-automation",
Expand Down
29 changes: 16 additions & 13 deletions tests/CTS/templates/javascript.mustache
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// @ts-nocheck
import { {{client}}, EchoRequester } from '{{{import}}}';

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

{{#tests}}
test('{{testName}}', async () => {
const req = await client.{{method}}({{#parameters}}{{{value}}}{{^-last}}, {{/-last}}{{/parameters}});
expect(req).toMatchObject({
path: '{{{request.path}}}',
method: '{{{request.method}}}',
{{#request.data}}data: {{{.}}},{{/request.data}}
})
});
{{#blocks}}
describe('{{operationId}}', () => {
{{#tests}}
test('{{testName}}', async () => {
const req = await client.{{method}}({{#parameters}}{{{value}}}{{^-last}}, {{/-last}}{{/parameters}});
expect(req).toMatchObject({
path: '{{{request.path}}}',
method: '{{{request.method}}}',
{{#request.data}}data: {{{.}}},{{/request.data}}
})
});

{{/tests}}
});
{{/tests}}
})

{{/blocks}}
36 changes: 23 additions & 13 deletions tests/generateCTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import fsp from 'fs/promises';
import path from 'path';

import SwaggerParser from '@apidevtools/swagger-parser';
import Mustache from 'mustache';
import type { OpenAPIV3 } from 'openapi-types';
import SwaggerParser from 'swagger-parser';

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

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

type CTSBlock = {
type Tests = {
testName?: string;
method: string;
parameters: any[];
Expand All @@ -22,6 +22,11 @@ type CTSBlock = {
};
};

type CTSBlock = {
operationId: string;
tests: Tests[];
};

// Array of test per client
type CTS = Record<string, CTSBlock[]>;

Expand Down Expand Up @@ -68,6 +73,10 @@ function capitalize(str: string): string {
async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
// load the list of operations from the spec
const spec = await SwaggerParser.validate(`../specs/${client}/spec.yml`);
if (!spec.paths) {
throw new Error(`No paths found for spec ${client}/spec.yml`);
}

const operations = Object.values(spec.paths)
.flatMap<OpenAPIV3.OperationObject>((p) => Object.values(p))
.map((obj) => obj.operationId);
Expand All @@ -78,22 +87,18 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
if (!file.name.endsWith('json')) {
continue;
}
const operationId = file.name.replace('.json', '');
const fileName = file.name.replace('.json', '');
const fileContent = (await fsp.readFile(file.path)).toString();

if (!fileContent) {
throw new Error(
`cannot read empty file for operationId ${operationId} - ${client} client`
);
throw new Error(`cannot read empty file ${fileName} - ${client} client`);
}

const tests: CTSBlock[] = JSON.parse(fileContent);
const tests: Tests[] = JSON.parse(fileContent);

// check test validity against spec
if (!operations.includes(operationId)) {
throw new Error(
`cannot find operationId ${operationId} for the ${client} client`
);
if (!operations.includes(fileName)) {
throw new Error(`cannot find ${fileName} for the ${client} client`);
}

for (const test of tests) {
Expand All @@ -116,8 +121,13 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
// stringify request.data too
test.request.data = JSON.stringify(test.request.data);
}
ctsClient.push(...tests);

ctsClient.push({
operationId: fileName,
tests,
});
}

return ctsClient;
}

Expand All @@ -142,7 +152,7 @@ async function generateCode(language: Language): Promise<void> {
const code = Mustache.render(template, {
import: packageNames[language][client],
client: `${capitalize(client)}Api`,
tests: cts[client],
blocks: cts[client],
});
await fsp.writeFile(
`output/${language}/${client}${extensionForLanguage[language]}`,
Expand Down
12 changes: 6 additions & 6 deletions tests/output/javascript/recommend.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// @ts-nocheck
import { RecommendApi, EchoRequester } from '@algolia/recommend';

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

describe('getRecommendations', () => {
test('get recommendations with minimal parameters', async () => {
const req = await client.getRecommendations({
requests: [
Expand Down
Loading