Skip to content

Commit 484888b

Browse files
authored
chore(cts): reuse method template (#3385)
1 parent 92ad367 commit 484888b

File tree

60 files changed

+160
-317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+160
-317
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ public class ClientTestData {
1313
class Step {
1414

1515
public String type;
16-
public String object;
17-
public String path;
16+
public String method;
1817
public Map<String, Object> parameters;
19-
public Map<String, Object> requestOptions;
2018
public Expected expected;
2119
}
2220

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
6868
CodegenOperation ope = null;
6969
if (step.type.equals("createClient")) {
7070
stepOut.put("stepTemplate", "tests/client/createClient.mustache");
71-
stepOut.put("isCreateClient", true); // TODO: remove once dart and kotlin are converted
71+
stepOut.put("isCreateClient", true); // TODO: remove once kotlin is converted
7272

7373
boolean hasCustomHosts = step.parameters != null && step.parameters.containsKey("customHosts");
7474

@@ -88,30 +88,23 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
8888
}
8989
stepOut.put("gzipEncoding", gzipEncoding);
9090
} else if (step.type.equals("method")) {
91-
ope = operations.get(step.path);
91+
ope = operations.get(step.method);
9292
if (ope == null) {
93-
throw new CTSException("Cannot find operation for method: " + step.path, test.testName);
93+
throw new CTSException("Cannot find operation for method: " + step.method, test.testName);
9494
}
9595
stepOut.put("stepTemplate", "tests/client/method.mustache");
9696
stepOut.put("isMethod", true); // TODO: remove once kotlin is converted
97-
stepOut.put("hasOperationParams", ope.hasParams);
97+
stepOut.put("hasParams", ope.hasParams);
9898

9999
// set on testOut because we need to wrap everything for java.
100100
testOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
101101
testOut.put("isAsync", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true)); // default to true because most api calls are asynchronous
102102
}
103103

104-
stepOut.put("object", step.object);
105-
stepOut.put("path", step.path);
104+
stepOut.put("method", step.method);
106105

107-
if (step.requestOptions != null) {
108-
Map<String, Object> requestOptions = new HashMap<>();
109-
paramsType.enhanceParameters(step.requestOptions, requestOptions);
110-
stepOut.put("requestOptions", requestOptions);
111-
}
112-
113-
if (step.path != null && CUSTOM_METHODS.contains(step.path)) {
114-
stepOut.put("isCustom", true);
106+
if (step.method != null && CUSTOM_METHODS.contains(step.method)) {
107+
stepOut.put("isCustomRequest", true);
115108
}
116109
paramsType.enhanceParameters(step.parameters, stepOut, ope);
117110

@@ -152,9 +145,9 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
152145
if (step.expected.error != null) {
153146
stepOut.put("isError", true);
154147
stepOut.put("expectedError", step.expected.error);
155-
if (language.equals("go") && step.path != null) {
148+
if (language.equals("go") && step.method != null) {
156149
// hack for go that use PascalCase, but just in the operationID
157-
stepOut.put("expectedError", step.expected.error.replace(step.path, Helpers.toPascalCase(step.path)));
150+
stepOut.put("expectedError", step.expected.error.replace(step.method, Helpers.toPascalCase(step.method)));
158151
}
159152
} else if (step.expected.match != null) {
160153
Map<String, Object> matchMap = new HashMap<>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
182182
}
183183

184184
test.put("request", req.request);
185-
test.put("hasParameters", req.parameters.size() != 0);
186-
test.put("hasOperationParams", ope.hasParams);
185+
test.put("isAsync", true);
186+
test.put("hasParams", ope.hasParams);
187187
test.put("isHelper", isHelper);
188188

189189
if (req.requestOptions != null) {

scripts/cts/runCts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function runCtsOne(
115115
break;
116116
case 'swift':
117117
await run(
118-
`swift test -Xswiftc -suppress-warnings -v --parallel ${filter((f) => `--filter ${f}.*`)}`,
118+
`swift test -Xswiftc -suppress-warnings --parallel ${filter((f) => `--filter ${f}.*`)}`,
119119
{
120120
cwd,
121121
language,

scripts/cts/testServer/timeout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function assertValidTimeouts(expectedCount: number): void {
2929
expect(state.duration[0] * 2).to.be.closeTo(state.duration[1], 200);
3030
break;
3131
case 'Swift':
32-
expect(state.duration[0]).to.be.closeTo(state.duration[1], 500);
32+
expect(state.duration[0]).to.be.closeTo(state.duration[1], 800);
3333
break;
3434
default:
3535
// the delay should be the same, because the `retryCount` is per host instead of global

templates/csharp/snippets/method.mustache

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@ public class Snippet{{client}}
2323
var client = new {{client}}(new {{clientPrefix}}Config("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}},"YOUR_APP_ID_REGION"{{/hasRegionalHost}}));
2424

2525
// Call the API
26-
var response = await client.{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}Async{{#isGeneric}}<Object>{{/isGeneric}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, new RequestOptionBuilder()
27-
{{#requestOptions.queryParameters.parametersWithDataType}}
28-
.AddExtraQueryParameters("{{{key}}}", {{> tests/requests/requestOptionsParams}} )
29-
{{/requestOptions.queryParameters.parametersWithDataType}}
30-
{{#requestOptions.headers.parametersWithDataType}}
31-
.AddExtraHeader("{{{key}}}", "{{{value}}}")
32-
{{/requestOptions.headers.parametersWithDataType}}
33-
.Build()
34-
{{/hasRequestOptions}});
26+
var response = {{> tests/method}};
3527
// SEPARATOR<
3628
}
3729

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
{{^useEchoRequester}}var res = {{/useEchoRequester}}{{#isAsync}}await {{/isAsync}}client.{{#lambda.pascalcase}}{{#path}}.{{.}}{{/path}}{{/lambda.pascalcase}}{{#isAsync}}Async{{/isAsync}}{{#isGeneric}}<Object>{{/isGeneric}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, new RequestOptions(){
2-
{{#requestOptions.queryParameters}}
3-
QueryParameters = new Dictionary<string, object>(){ {{#parametersWithDataType}} {"{{{key}}}", {{> tests/requests/requestOptionsParams}} } {{^-last}},{{/-last}}{{/parametersWithDataType}} },
4-
{{/requestOptions.queryParameters}}
5-
{{#requestOptions.headers}}
6-
Headers = new Dictionary<string, string>(){ {{#parametersWithDataType}} {"{{{key}}}", "{{{value}}}" } {{^-last}},{{/-last}}{{/parametersWithDataType}} },
7-
{{/requestOptions.headers}}
8-
}{{/hasRequestOptions}});
1+
{{^useEchoRequester}}var res = {{/useEchoRequester}}{{> tests/method}};
92
{{#useEchoRequester}}EchoResponse result = _echo.LastResponse;{{/useEchoRequester}}

templates/csharp/tests/e2e/e2e.mustache

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Algolia.Search.e2e;
1414

1515
public class {{client}}RequestTestsE2E
1616
{
17-
private readonly {{client}} _client;
17+
private readonly {{client}} client;
1818

1919
public {{client}}RequestTestsE2E()
2020
{
@@ -32,7 +32,7 @@ private readonly {{client}} _client;
3232
throw new Exception("please provide an `{{e2eApiKey}}` env var for e2e tests");
3333
}
3434

35-
_client = new {{client}}(new {{clientPrefix}}Config(appId, apiKey{{#hasRegionalHost}},"{{defaultRegion}}"{{/hasRegionalHost}}));
35+
client = new {{client}}(new {{clientPrefix}}Config(appId, apiKey{{#hasRegionalHost}},"{{defaultRegion}}"{{/hasRegionalHost}}));
3636
}
3737

3838
[Fact]
@@ -47,14 +47,7 @@ private readonly {{client}} _client;
4747
public async Task {{#lambda.pascalcase}}{{method}}Test{{testIndex}}{{/lambda.pascalcase}}()
4848
{
4949
try {
50-
var resp = await _client.{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}Async{{#isGeneric}}<Hit>{{/isGeneric}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, new RequestOptions(){
51-
{{#requestOptions.queryParameters}}
52-
QueryParameters = new Dictionary<string, object>(){ {{#parametersWithDataType}} {"{{{key}}}", {{> tests/requests/requestOptionsParams}} } {{^-last}},{{/-last}}{{/parametersWithDataType}} },
53-
{{/requestOptions.queryParameters}}
54-
{{#requestOptions.headers}}
55-
Headers = new Dictionary<string, string>(){ {{#parametersWithDataType}} {"{{{key}}}", "{{{value}}}" } {{^-last}},{{/-last}}{{/parametersWithDataType}} },
56-
{{/requestOptions.headers}}
57-
}{{/hasRequestOptions}});
50+
var resp = {{> tests/method}};
5851
{{#response}}
5952
{{#statusCode}}
6053
// Check status code {{statusCode}}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#isAsync}}await {{/isAsync}}client.{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}{{#isAsync}}Async{{/isAsync}}{{#isGeneric}}<Hit>{{/isGeneric}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, new RequestOptionBuilder()
2+
{{#requestOptions.queryParameters.parametersWithDataType}}
3+
.AddExtraQueryParameters("{{{key}}}", {{> tests/requests/requestOptionsParams}} )
4+
{{/requestOptions.queryParameters.parametersWithDataType}}
5+
{{#requestOptions.headers.parametersWithDataType}}
6+
.AddExtraHeader("{{{key}}}", "{{{value}}}")
7+
{{/requestOptions.headers.parametersWithDataType}}
8+
.Build()
9+
{{/hasRequestOptions}})

templates/csharp/tests/requests/requests.mustache

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace Algolia.Search.requests;
1414

1515
public class {{client}}RequestTests
1616
{
17-
private readonly {{client}} _client;
17+
private readonly {{client}} client;
1818
private readonly EchoHttpRequester _echo;
1919

2020
public {{client}}RequestTests()
2121
{
2222
_echo = new EchoHttpRequester();
23-
_client = new {{client}}(new {{clientPrefix}}Config("appId", "apiKey"{{#hasRegionalHost}},"{{defaultRegion}}"{{/hasRegionalHost}}), _echo);
23+
client = new {{client}}(new {{clientPrefix}}Config("appId", "apiKey"{{#hasRegionalHost}},"{{defaultRegion}}"{{/hasRegionalHost}}), _echo);
2424
}
2525

2626
[Fact]
@@ -34,15 +34,7 @@ private readonly {{client}} _client;
3434
[Fact(DisplayName = "{{{testName}}}")]
3535
public async Task {{#lambda.pascalcase}}{{method}}Test{{testIndex}}{{/lambda.pascalcase}}()
3636
{
37-
await _client.{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}Async{{#isGeneric}}<Hit>{{/isGeneric}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, new RequestOptionBuilder()
38-
{{#requestOptions.queryParameters.parametersWithDataType}}
39-
.AddExtraQueryParameters("{{{key}}}", {{> tests/requests/requestOptionsParams}} )
40-
{{/requestOptions.queryParameters.parametersWithDataType}}
41-
{{#requestOptions.headers.parametersWithDataType}}
42-
.AddExtraHeader("{{{key}}}", "{{{value}}}")
43-
{{/requestOptions.headers.parametersWithDataType}}
44-
.Build()
45-
{{/hasRequestOptions}});
37+
{{> tests/method}};
4638

4739
var req = _echo.LastResponse;
4840
{{#request}}

0 commit comments

Comments
 (0)