Skip to content

Commit 9821a02

Browse files
committed
chore: add tests for WithTransformation methods
1 parent 72b03d3 commit 9821a02

File tree

7 files changed

+425
-177
lines changed

7 files changed

+425
-177
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
9595
stepOut.put("customHosts", step.parameters.get("customHosts"));
9696
}
9797

98+
boolean hasTransformationRegion = step.parameters != null && step.parameters.containsKey("transformationRegion");
99+
if (hasTransformationRegion) testOut.put("useEchoRequester", false);
100+
stepOut.put("hasTransformationRegion", hasTransformationRegion);
101+
if (hasTransformationRegion) {
102+
stepOut.put("transformationRegion", step.parameters.get("transformationRegion"));
103+
}
104+
98105
boolean gzipEncoding = step.parameters != null && step.parameters.getOrDefault("gzip", false).equals(true);
99106
// many languages don't support gzip yet
100107
if (

scripts/cts/testServer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { apiKeyServer } from './apiKey.ts';
1313
import { benchmarkServer } from './benchmark.ts';
1414
import { chunkWrapperServer } from './chunkWrapper.ts';
1515
import { gzipServer } from './gzip.ts';
16+
import { pushMockServer } from './pushMock.ts';
1617
import { replaceAllObjectsServer } from './replaceAllObjects.ts';
1718
import { replaceAllObjectsServerFailed } from './replaceAllObjectsFailed.ts';
1819
import { replaceAllObjectsScopesServer } from './replaceAllObjectsScopes.ts';
@@ -35,6 +36,7 @@ export async function startTestServer(suites: Record<CTSType, boolean>): Promise
3536
waitForApiKeyServer(),
3637
apiKeyServer(),
3738
algoliaMockServer(),
39+
pushMockServer(),
3840
);
3941
}
4042
if (suites.benchmark) {

scripts/cts/testServer/pushMock.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { Server } from 'http';
2+
3+
import { expect } from 'chai';
4+
import type { Express } from 'express';
5+
import express from 'express';
6+
7+
import { setupServer } from './index.ts';
8+
9+
const pushMockState: Record<string, any> = {};
10+
11+
export function assertPushMockValid(expectedCount: number): void {
12+
if (Object.values(pushMockState).length !== expectedCount) {
13+
throw new Error('unexpected number of call to pushMock');
14+
}
15+
for (const [lang, state] of Object.entries(pushMockState)) {
16+
let numberOfTestSuites = 1;
17+
18+
if (lang === 'python') {
19+
numberOfTestSuites = 2;
20+
}
21+
22+
expect(state).to.deep.equal({
23+
saveObjectsWithTransformation: Number(numberOfTestSuites),
24+
partialUpdateObjectsWithTransformation: Number(numberOfTestSuites),
25+
});
26+
}
27+
}
28+
29+
function addRoutes(app: Express): void {
30+
app.use(express.urlencoded({ extended: true }));
31+
app.use(
32+
express.json({
33+
type: ['application/json', 'text/plain'], // the js client sends the body as text/plain
34+
}),
35+
);
36+
37+
app.post('/1/push/:indexName', (req, res) => {
38+
const match = req.params.indexName.match(/^cts_e2e_(\w+)_(.*)$/);
39+
const helper = match?.[1] as string;
40+
const lang = match?.[2] as string;
41+
42+
if (!pushMockState[lang]) {
43+
pushMockState[lang] = {};
44+
}
45+
46+
pushMockState[lang][helper] = (pushMockState[lang][helper] ?? 0) + 1;
47+
switch (helper) {
48+
case 'saveObjectsWithTransformation':
49+
expect(req.body).to.deep.equal({
50+
requests: [
51+
{ action: 'addObject', body: { objectID: '1', name: 'Adam' } },
52+
{ action: 'addObject', body: { objectID: '2', name: 'Benoit' } },
53+
],
54+
});
55+
56+
res.json({
57+
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
58+
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
59+
message: 'OK',
60+
createdAt: '2022-05-12T06:24:30.049Z',
61+
});
62+
63+
break;
64+
case 'partialUpdateObjectsWithTransformation':
65+
expect(req.body).to.deep.equal({
66+
requests: [
67+
{ action: 'partialUpdateObject', body: { objectID: '1', name: 'Adam' } },
68+
{ action: 'partialUpdateObject', body: { objectID: '2', name: 'Benoit' } },
69+
],
70+
});
71+
72+
res.json({
73+
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
74+
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
75+
message: 'OK',
76+
createdAt: '2022-05-12T06:24:30.049Z',
77+
});
78+
break;
79+
default:
80+
throw new Error('unknown helper');
81+
}
82+
});
83+
}
84+
85+
export function pushMockServer(): Promise<Server> {
86+
return setupServer('pushMock', 6689, addRoutes);
87+
}

templates/javascript/tests/client/createClient.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
protocol: 'http'
1818
},
1919
{{/customHosts}}
20-
]
20+
],
2121
{{/hasCustomHosts}}
22+
{{#hasTransformationRegion}}
23+
transformation: { region : "{{{transformationRegion}}}" },
24+
{{/hasTransformationRegion}}
2225
}
2326
{{/isStandaloneClient}}
2427
){{^isStandaloneClient}}.{{{initMethod}}}(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[
2+
{
3+
"testName": "call partialUpdateObjectsWithTransformation with createIfNotExists=true",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "test-app-id",
10+
"apiKey": "test-api-key",
11+
"customHosts": [
12+
{
13+
"port": 6689
14+
}
15+
],
16+
"transformationRegion": "us"
17+
}
18+
},
19+
{
20+
"type": "method",
21+
"method": "partialUpdateObjectsWithTransformation",
22+
"parameters": {
23+
"indexName": "cts_e2e_partialUpdateObjectsWithTransformation_${{language}}",
24+
"objects": [
25+
{
26+
"objectID": "1",
27+
"name": "Adam"
28+
},
29+
{
30+
"objectID": "2",
31+
"name": "Benoit"
32+
}
33+
],
34+
"createIfNotExists": true
35+
},
36+
"expected": {
37+
"type": "response",
38+
"match": [
39+
{
40+
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
41+
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
42+
"message": "OK",
43+
"createdAt": "2022-05-12T06:24:30.049Z"
44+
}
45+
]
46+
}
47+
}
48+
]
49+
}
50+
]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[
2+
{
3+
"testName": "call saveObjectsWithTransformation without error",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "test-app-id",
10+
"apiKey": "test-api-key",
11+
"customHosts": [
12+
{
13+
"port": 6689
14+
}
15+
],
16+
"transformationRegion": "us"
17+
}
18+
},
19+
{
20+
"type": "method",
21+
"method": "saveObjectsWithTransformation",
22+
"parameters": {
23+
"indexName": "cts_e2e_saveObjectsWithTransformation_${{language}}",
24+
"objects": [
25+
{
26+
"objectID": "1",
27+
"name": "Adam"
28+
},
29+
{
30+
"objectID": "2",
31+
"name": "Benoit"
32+
}
33+
]
34+
},
35+
"expected": {
36+
"type": "response",
37+
"match": [
38+
{
39+
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
40+
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
41+
"message": "OK",
42+
"createdAt": "2022-05-12T06:24:30.049Z"
43+
}
44+
]
45+
}
46+
}
47+
]
48+
}
49+
]

0 commit comments

Comments
 (0)