Skip to content

Commit 9cbff0b

Browse files
committed
feat(clients): generate test for wait api key helpers
1 parent 5c412b0 commit 9cbff0b

File tree

6 files changed

+241
-2
lines changed

6 files changed

+241
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
169169
} else {
170170
stepOut.put("match", step.expected.match);
171171
}
172+
} else if (step.expected.match == null) {
173+
stepOut.put("match", Map.of());
174+
stepOut.put("matchIsJSON", false);
175+
stepOut.put("matchIsNull", true);
172176
}
173177
}
174178
steps.add(stepOut);

playground/swift/playground/playground/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Task {
4444
taskIDs.append(saveObjRes.taskID)
4545
}
4646
for taskID in taskIDs {
47-
try await client.waitForTask(with: taskID, in: indexName)
47+
try await client.waitForTask(indexName: indexName, taskID: taskID)
4848
}
4949

5050
let searchParams = SearchSearchParamsObject(query: "Jimmy")

scripts/cts/testServer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { gzipServer } from './gzip';
1010
import { replaceAllObjectsServer } from './replaceAllObjects';
1111
import { timeoutServer } from './timeout';
1212
import { timeoutServerBis } from './timeoutBis';
13+
import { waitForApiKeyServer } from './waitForApiKey';
1314

1415
export async function startTestServer(): Promise<() => Promise<void>> {
1516
const servers = await Promise.all([
@@ -18,6 +19,7 @@ export async function startTestServer(): Promise<() => Promise<void>> {
1819
timeoutServerBis(),
1920
replaceAllObjectsServer(),
2021
chunkWrapperServer(),
22+
waitForApiKeyServer(),
2123
]);
2224

2325
return async () => {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import type { Server } from 'http';
2+
3+
import type { Express } from 'express';
4+
5+
import { setupServer } from '.';
6+
7+
const retryCount = {
8+
add: 0,
9+
update: 0,
10+
delete: 0,
11+
};
12+
13+
function addRoutes(app: Express): void {
14+
app.get('/1/keys/:key', (req, res) => {
15+
if (req.params.key === 'api-key-add-operation-test') {
16+
if (retryCount.add < 3) {
17+
res.status(404).json({ message: `API key doesn't exist` });
18+
} else if (retryCount.add === 3) {
19+
res.status(200).json({
20+
value: req.params.key,
21+
description: 'my new api key',
22+
acl: ['search', 'addObject'],
23+
validity: 300,
24+
maxQueriesPerIPPerHour: 100,
25+
maxHitsPerQuery: 20,
26+
createdAt: 1720094400,
27+
});
28+
} else {
29+
res.status(500).json({ message: `Internal Server Error` });
30+
}
31+
32+
retryCount.add += 1;
33+
} else if (req.params.key === 'api-key-update-operation-test') {
34+
if (retryCount.update < 3) {
35+
res.status(200).json({
36+
value: req.params.key,
37+
description: 'my new api key',
38+
acl: ['search', 'addObject'],
39+
validity: 300,
40+
maxQueriesPerIPPerHour: 100,
41+
maxHitsPerQuery: 20,
42+
createdAt: 1720094400,
43+
});
44+
} else if (retryCount.update === 3) {
45+
res.status(200).json({
46+
value: req.params.key,
47+
description: 'my updated api key',
48+
acl: ['search', 'addObject', 'deleteObject'],
49+
validity: 305,
50+
maxQueriesPerIPPerHour: 95,
51+
maxHitsPerQuery: 20,
52+
createdAt: 1720094400,
53+
});
54+
} else {
55+
res.status(500).json({ message: `Internal Server Error` });
56+
}
57+
58+
retryCount.update += 1;
59+
} else if (req.params.key === 'api-key-delete-operation-test') {
60+
if (retryCount.delete < 3) {
61+
res.status(200).json({
62+
value: req.params.key,
63+
description: 'my updated api key',
64+
acl: ['search', 'addObject', 'deleteObject'],
65+
validity: 305,
66+
maxQueriesPerIPPerHour: 95,
67+
maxHitsPerQuery: 20,
68+
createdAt: 1720094400,
69+
});
70+
} else if (retryCount.delete === 3) {
71+
res.status(404).json({ message: `API key doesn't exist` });
72+
} else {
73+
res.status(500).json({ message: `Internal Server Error` });
74+
}
75+
76+
retryCount.delete += 1;
77+
} else {
78+
res.status(500).json({ message: `Internal Server Error` });
79+
}
80+
});
81+
82+
// fallback route
83+
app.use((req, res) => {
84+
// eslint-disable-next-line no-console
85+
console.log('fallback route', req.method, req.url);
86+
res.status(500).json({ message: `Internal Server Error (fallback)` });
87+
});
88+
89+
app.use((err, req, res, _) => {
90+
// eslint-disable-next-line no-console
91+
console.error(err.message);
92+
res.status(500).send({ message: err.message });
93+
});
94+
}
95+
96+
export function waitForApiKeyServer(): Promise<Server> {
97+
return setupServer('waitForApiKey', 6680, addRoutes);
98+
}

templates/swift/tests/client/suite.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ final class {{client}}ClientTests: XCTestCase {
6363
try XCTLenientAssertEqual(received: CodableHelper.jsonEncoder.encode(response), expected: comparableData)
6464
{{/matchIsJSON}}
6565
{{^matchIsJSON}}
66-
XCTAssertEqual("{{#lambda.escapeQuotes}}{{{match}}}{{/lambda.escapeQuotes}}", response)
66+
{{#matchIsNull}}
67+
XCTAssertNil(response)
68+
{{/matchIsNull}}
69+
{{^matchIsNull}}
70+
XCTAssertEqual("{{#lambda.escapeQuotes}}{{{match}}}{{/lambda.escapeQuotes}}", response)
71+
{{/matchIsNull}}
6772
{{/matchIsJSON}}
6873
{{/isHelper}}
6974
{{^isHelper}}

tests/CTS/client/search/helpers.json

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,135 @@
366366
}
367367
}
368368
]
369+
},
370+
{
371+
"testName": "wait for api key helper (add)",
372+
"autoCreateClient": false,
373+
"steps": [
374+
{
375+
"type": "createClient",
376+
"parameters": {
377+
"appId": "test-app-id",
378+
"apiKey": "test-api-key",
379+
"customHosts": [
380+
{
381+
"host": "${{localhost}}",
382+
"port": 6680
383+
}
384+
]
385+
}
386+
},
387+
{
388+
"type": "method",
389+
"object": "$client",
390+
"path": "waitForApiKey",
391+
"parameters": {
392+
"key": "api-key-add-operation-test",
393+
"operation": "add"
394+
},
395+
"expected": {
396+
"type": "response",
397+
"match": {
398+
"value": "api-key-add-operation-test",
399+
"description": "my new api key",
400+
"acl": [
401+
"search",
402+
"addObject"
403+
],
404+
"validity": 300,
405+
"maxQueriesPerIPPerHour": 100,
406+
"maxHitsPerQuery": 20,
407+
"createdAt": 1720094400
408+
}
409+
}
410+
}
411+
]
412+
},
413+
{
414+
"testName": "wait for api key (update)",
415+
"autoCreateClient": false,
416+
"steps": [
417+
{
418+
"type": "createClient",
419+
"parameters": {
420+
"appId": "test-app-id",
421+
"apiKey": "test-api-key",
422+
"customHosts": [
423+
{
424+
"host": "${{localhost}}",
425+
"port": 6680
426+
}
427+
]
428+
}
429+
},
430+
{
431+
"type": "method",
432+
"object": "$client",
433+
"path": "waitForApiKey",
434+
"parameters": {
435+
"key": "api-key-update-operation-test",
436+
"operation": "update",
437+
"apiKey": {
438+
"description": "my updated api key",
439+
"acl": [
440+
"search",
441+
"addObject",
442+
"deleteObject"
443+
],
444+
"validity": 305,
445+
"maxQueriesPerIPPerHour": 95,
446+
"maxHitsPerQuery": 20
447+
}
448+
},
449+
"expected": {
450+
"type": "response",
451+
"match": {
452+
"value": "api-key-update-operation-test",
453+
"description": "my updated api key",
454+
"acl": [
455+
"search",
456+
"addObject",
457+
"deleteObject"
458+
],
459+
"validity": 305,
460+
"maxQueriesPerIPPerHour": 95,
461+
"maxHitsPerQuery": 20,
462+
"createdAt": 1720094400
463+
}
464+
}
465+
}
466+
]
467+
},
468+
{
469+
"testName": "wait for api key (delete)",
470+
"autoCreateClient": false,
471+
"steps": [
472+
{
473+
"type": "createClient",
474+
"parameters": {
475+
"appId": "test-app-id",
476+
"apiKey": "test-api-key",
477+
"customHosts": [
478+
{
479+
"host": "${{localhost}}",
480+
"port": 6680
481+
}
482+
]
483+
}
484+
},
485+
{
486+
"type": "method",
487+
"object": "$client",
488+
"path": "waitForApiKey",
489+
"parameters": {
490+
"key": "api-key-delete-operation-test",
491+
"operation": "delete"
492+
},
493+
"expected": {
494+
"type": "response",
495+
"match": null
496+
}
497+
}
498+
]
369499
}
370500
]

0 commit comments

Comments
 (0)