Skip to content

feat(cts): add custom request tests #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 14, 2022
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 .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.2.0
7.2.1.0.1
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ jobs:
hashFiles(
format('{0}/lib/Api/{1}.php', matrix.client.folder, matrix.client.api),
format('{0}/lib/Configuration/{1}.php', matrix.client.folder, matrix.client.config),
format('{0}/lib/Model/{1}/**', matrix.client.folder, matrix.client.capitalizedName),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that making a difference ? Those files aren't pushed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet they are not, but required by the cache, I wonder if it will work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An other limitation of the cache :( We will need to add it afterward

format('specs/bundled/{0}.yml', matrix.client.name),
'templates/php/**',
'generators/src/**'
Expand Down
2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
spec: '@yarnpkg/plugin-interactive-tools'

yarnPath: .yarn/releases/yarn-3.1.1.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public Map<String, Object> postProcessSupportingFileData(
System.out.println(e.getMessage());
System.exit(0);
}

System.out.println(e.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huge fix !

System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
Expand All @@ -181,15 +184,28 @@ private Map<String, Request[]> loadCTS()
throws JsonParseException, JsonMappingException, IOException, CTSException {
TreeMap<String, Request[]> cts = new TreeMap<>();
File dir = new File("tests/CTS/methods/requests/" + client);
File commonTestDir = new File("tests/CTS/methods/requests/common");
if (!dir.exists()) {
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true);
}
if (!commonTestDir.exists()) {
throw new CTSException(
"CTS not found at " + commonTestDir.getAbsolutePath(),
true
);
}
for (File f : dir.listFiles()) {
cts.put(
f.getName().replace(".json", ""),
Json.mapper().readValue(f, Request[].class)
);
}
for (File f : commonTestDir.listFiles()) {
cts.put(
f.getName().replace(".json", ""),
Json.mapper().readValue(f, Request[].class)
);
}
return cts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ private void handleModel(
String parent,
int suffix
) throws CTSException {
assert (spec.getHasVars());
assert (spec.getItems() == null);
if (!spec.getHasVars()) {
throw new CTSException("Spec has no vars.");
}

if (spec.getItems() != null) {
throw new CTSException("Spec has items.");
}

if (
spec instanceof CodegenModel && ((CodegenModel) spec).oneOf.size() > 0
Expand Down Expand Up @@ -269,8 +274,13 @@ private void handleObject(
IJsonSchemaValidationProperties spec,
int suffix
) throws CTSException {
assert (!spec.getHasVars());
assert (spec.getItems() == null);
if (spec.getHasVars()) {
throw new CTSException("Spec has vars.");
}

if (spec.getItems() != null) {
throw new CTSException("Spec has items.");
}

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

Expand Down Expand Up @@ -300,18 +310,37 @@ private void handleMap(
IJsonSchemaValidationProperties spec,
int suffix
) throws CTSException {
assert (!spec.getHasVars());
assert (spec.getItems() != null);
if (spec.getHasVars()) {
throw new CTSException("Spec has vars.");
}

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

List<Object> values = new ArrayList<>();

CodegenProperty items = spec.getItems();

for (Entry<String, Object> entry : vars.entrySet()) {
IJsonSchemaValidationProperties itemType = items;

// The generator consider a free form object type as an `object`, which
// is wrong in our case, so we infer it.
if (
items == null ||
(items.openApiType.equals("object") && items.isFreeFormObject)
) {
CodegenParameter maybeMatch = new CodegenParameter();
String paramType = inferDataType(entry.getValue(), maybeMatch, null);

maybeMatch.dataType = paramType;
itemType = maybeMatch;
}

values.add(
traverseParams(
entry.getKey(),
entry.getValue(),
spec.getItems(),
itemType,
paramName,
suffix + 1
)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@algolia/api-client-automation",
"version": "0.0.0",
"private": true,
"workspaces": [
"clients/algoliasearch-client-javascript/",
"playground/javascript/node/",
Expand Down
4 changes: 2 additions & 2 deletions scripts/cts/client/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { createSpinner } from '../../oraLog';
import type { Generator } from '../../types';
import {
walk,
createClientName,
createOutputDir,
getOutputPath,
loadTemplates,
createClientName,
} from '../utils';

import type { TestsBlock, Test, ModifiedStepForMustache } from './types';
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function generateClientTests(
template,
{
import: packageName,
client: createClientName(client, language),
client: `${createClientName(client, language)}Api`,
blocks: modifyForMustache(testsBlocks),
hasRegionalHost: hasRegionalHost ? true : undefined,
},
Expand Down
14 changes: 6 additions & 8 deletions scripts/cts/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@ describe('utils', () => {

describe('createClientName', () => {
it('does not capitalize every part for JavaScript', () => {
expect(createClientName('search', 'javascript')).toEqual('searchApi');
expect(createClientName('search', 'javascript')).toEqual('search');
expect(createClientName('search-client', 'javascript')).toEqual(
'searchClientApi'
'searchClient'
);
expect(createClientName('search-cli!nt-complex', 'javascript')).toEqual(
'searchCli!ntComplexApi'
'searchCli!ntComplex'
);
});

it('capitalize every part for other languages', () => {
expect(createClientName('search', 'java')).toEqual('SearchApi');
expect(createClientName('search-client', 'java')).toEqual(
'SearchClientApi'
);
expect(createClientName('search', 'java')).toEqual('Search');
expect(createClientName('search-client', 'java')).toEqual('SearchClient');
expect(createClientName('search-cli!nt-complex', 'java')).toEqual(
'SearchCli!ntComplexApi'
'SearchCli!ntComplex'
);
});
});
Expand Down
3 changes: 2 additions & 1 deletion scripts/cts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export function createClientName(client: string, language: string): string {
})
.join('');

return `${clientName}Api`;
return clientName;
}

export async function createOutputDir({
language,
testPath,
Expand Down
6 changes: 0 additions & 6 deletions specs/common/paths/customRequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ put:

delete:
operationId: del
requestBody:
description: The parameters to send with the custom request.
content:
application/json:
schema:
type: object
$ref: '../schemas/CustomRequest.yml#/customRequest'
30 changes: 30 additions & 0 deletions tests/CTS/methods/requests/common/del.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"method": "del",
"testName": "allow del method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "DELETE"
}
},
{
"method": "del",
"testName": "allow del method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": {
"query": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "DELETE",
"searchParams": {
"query": "parameters"
}
}
}
]
30 changes: 30 additions & 0 deletions tests/CTS/methods/requests/common/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"method": "get",
"testName": "allow get method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "GET"
}
},
{
"method": "get",
"testName": "allow get method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": {
"query": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "GET",
"searchParams": {
"query": "parameters"
}
}
}
]
36 changes: 36 additions & 0 deletions tests/CTS/methods/requests/common/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"method": "post",
"testName": "allow post method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "POST"
}
},
{
"method": "post",
"testName": "allow post method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": {
"query": "parameters"
},
"body": {
"body": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "POST",
"data": {
"body": "parameters"
},
"searchParams": {
"query": "parameters"
}
}
}
]
36 changes: 36 additions & 0 deletions tests/CTS/methods/requests/common/put.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"method": "put",
"testName": "allow put method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "PUT"
}
},
{
"method": "put",
"testName": "allow put method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": {
"query": "parameters"
},
"body": {
"body": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "PUT",
"data": {
"body": "parameters"
},
"searchParams": {
"query": "parameters"
}
}
}
]
4 changes: 2 additions & 2 deletions tests/CTS/methods/requests/templates/java/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class {{client}}Tests {

{{#request.searchParams}}
HashMap<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.searchParams}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
List<Pair> acutalQuery = req.getQueryParams();
for (Pair p : acutalQuery) {
List<Pair> actualQuery = req.getQueryParams();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops I have the same code here, you will merge first unfortunately :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D:

for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
}
{{/request.searchParams}}
Expand Down
Loading