Skip to content

Commit c5a178e

Browse files
authored
feat(cts): add algoliasearch-lite (#436)
1 parent f387277 commit c5a178e

File tree

8 files changed

+351
-15
lines changed

8 files changed

+351
-15
lines changed

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.IOException;
1212
import java.util.*;
1313
import java.util.Map.Entry;
14+
import java.util.TreeMap;
1415
import org.openapitools.codegen.*;
1516

1617
@SuppressWarnings("unchecked")
@@ -146,20 +147,20 @@ public Map<String, Object> postProcessSupportingFileData(
146147
language
147148
);
148149

149-
for (Entry<String, Request[]> entry : cts.entrySet()) {
150+
for (Entry<String, CodegenOperation> entry : operations.entrySet()) {
150151
String operationId = entry.getKey();
151-
if (!operations.containsKey(operationId)) {
152+
if (!cts.containsKey(operationId)) {
152153
throw new CTSException(
153154
"operationId " + operationId + " does not exist in the spec"
154155
);
155156
}
156-
CodegenOperation op = operations.get(operationId);
157+
Request[] op = cts.get(operationId);
157158

158159
List<Object> tests = new ArrayList<>();
159-
for (int i = 0; i < entry.getValue().length; i++) {
160+
for (int i = 0; i < op.length; i++) {
160161
Map<String, Object> test = paramsType.buildJSONForRequest(
161-
entry.getValue()[i],
162-
op,
162+
op[i],
163+
entry.getValue(),
163164
i
164165
);
165166
tests.add(test);
@@ -190,7 +191,18 @@ public Map<String, Object> postProcessSupportingFileData(
190191
private Map<String, Request[]> loadCTS()
191192
throws JsonParseException, JsonMappingException, IOException, CTSException {
192193
TreeMap<String, Request[]> cts = new TreeMap<>();
193-
File dir = new File("tests/CTS/methods/requests/" + client);
194+
String clientName = client;
195+
196+
// This special case allow us to read the `search` CTS to generated the
197+
// tests for the `algoliasearch-lite` client, which is only available
198+
// in JavaScript
199+
if (
200+
language.equals("javascript") && clientName.equals("algoliasearch-lite")
201+
) {
202+
clientName = "search";
203+
}
204+
205+
File dir = new File("tests/CTS/methods/requests/" + clientName);
194206
File commonTestDir = new File("tests/CTS/methods/requests/common");
195207
if (!dir.exists()) {
196208
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true);
@@ -217,28 +229,32 @@ private Map<String, Request[]> loadCTS()
217229
}
218230

219231
// operationId -> CodegenOperation
220-
private HashMap<String, CodegenOperation> buildOperations(
232+
private TreeMap<String, CodegenOperation> buildOperations(
221233
Map<String, Object> objs
222234
) {
223235
HashMap<String, CodegenOperation> result = new HashMap<>();
224236
List<Map<String, Object>> apis =
225237
((Map<String, List<Map<String, Object>>>) objs.get("apiInfo")).get(
226238
"apis"
227239
);
240+
228241
for (Map<String, Object> api : apis) {
229242
String apiName = ((String) api.get("baseName")).toLowerCase();
230243
if (!apiName.equals(client.replace("-", ""))) {
231244
continue;
232245
}
246+
233247
List<CodegenOperation> operations =
234248
((Map<String, List<CodegenOperation>>) api.get("operations")).get(
235249
"operation"
236250
);
251+
237252
for (CodegenOperation ope : operations) {
238253
result.put(ope.operationId, ope);
239254
}
240255
}
241-
return result;
256+
257+
return new TreeMap<String, CodegenOperation>(result);
242258
}
243259

244260
private String createImportName() {

tests/CTS/methods/requests/search/multipleQueries.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
[
22
{
33
"method": "multipleQueries",
4+
"testName": "multipleQueries for a single request with minimal parameters",
5+
"parameters": {
6+
"requests": [
7+
{
8+
"indexName": "theIndexName"
9+
}
10+
],
11+
"strategy": "stopIfEnoughMatches"
12+
},
13+
"request": {
14+
"path": "/1/indexes/*/queries",
15+
"method": "POST",
16+
"data": {
17+
"requests": [
18+
{
19+
"indexName": "theIndexName"
20+
}
21+
],
22+
"strategy": "stopIfEnoughMatches"
23+
}
24+
}
25+
},
26+
{
27+
"method": "multipleQueries",
28+
"testName": "multipleQueries for multiple requests with all parameters",
429
"parameters": {
530
"requests": [
631
{
@@ -9,6 +34,12 @@
934
"type": "facet",
1035
"facet": "theFacet",
1136
"params": "testParam"
37+
},
38+
{
39+
"indexName": "theIndexName",
40+
"query": "test",
41+
"type": "default",
42+
"params": "testParam"
1243
}
1344
],
1445
"strategy": "stopIfEnoughMatches"
@@ -24,6 +55,12 @@
2455
"type": "facet",
2556
"facet": "theFacet",
2657
"params": "testParam"
58+
},
59+
{
60+
"indexName": "theIndexName",
61+
"query": "test",
62+
"type": "default",
63+
"params": "testParam"
2764
}
2865
],
2966
"strategy": "stopIfEnoughMatches"

tests/output/java/src/test/java/com/algolia/methods/requests/search.test.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,8 +1408,49 @@ void multipleBatchTest0() {
14081408
}
14091409

14101410
@Test
1411-
@DisplayName("multipleQueries")
1411+
@DisplayName("multipleQueries for a single request with minimal parameters")
14121412
void multipleQueriesTest0() {
1413+
MultipleQueriesParams multipleQueriesParams0 = new MultipleQueriesParams();
1414+
{
1415+
List<MultipleQueries> requests1 = new ArrayList<>();
1416+
{
1417+
MultipleQueries requests_02 = new MultipleQueries();
1418+
{
1419+
String indexName3 = "theIndexName";
1420+
1421+
requests_02.setIndexName(indexName3);
1422+
}
1423+
requests1.add(requests_02);
1424+
}
1425+
multipleQueriesParams0.setRequests(requests1);
1426+
1427+
MultipleQueriesStrategy strategy1 = MultipleQueriesStrategy.fromValue(
1428+
"stopIfEnoughMatches"
1429+
);
1430+
1431+
multipleQueriesParams0.setStrategy(strategy1);
1432+
}
1433+
1434+
EchoResponseInterface req = (EchoResponseInterface) assertDoesNotThrow(() -> {
1435+
return client.multipleQueries(multipleQueriesParams0);
1436+
}
1437+
);
1438+
1439+
assertEquals(req.getPath(), "/1/indexes/*/queries");
1440+
assertEquals(req.getMethod(), "POST");
1441+
1442+
assertDoesNotThrow(() -> {
1443+
JSONAssert.assertEquals(
1444+
"{\"requests\":[{\"indexName\":\"theIndexName\"}],\"strategy\":\"stopIfEnoughMatches\"}",
1445+
req.getBody(),
1446+
JSONCompareMode.STRICT_ORDER
1447+
);
1448+
});
1449+
}
1450+
1451+
@Test
1452+
@DisplayName("multipleQueries for multiple requests with all parameters")
1453+
void multipleQueriesTest1() {
14131454
MultipleQueriesParams multipleQueriesParams0 = new MultipleQueriesParams();
14141455
{
14151456
List<MultipleQueries> requests1 = new ArrayList<>();
@@ -1434,6 +1475,24 @@ void multipleQueriesTest0() {
14341475
requests_02.setParams(params3);
14351476
}
14361477
requests1.add(requests_02);
1478+
1479+
MultipleQueries requests_12 = new MultipleQueries();
1480+
{
1481+
String indexName3 = "theIndexName";
1482+
1483+
requests_12.setIndexName(indexName3);
1484+
String query3 = "test";
1485+
1486+
requests_12.setQuery(query3);
1487+
1488+
MultipleQueriesType type3 = MultipleQueriesType.fromValue("default");
1489+
1490+
requests_12.setType(type3);
1491+
String params3 = "testParam";
1492+
1493+
requests_12.setParams(params3);
1494+
}
1495+
requests1.add(requests_12);
14371496
}
14381497
multipleQueriesParams0.setRequests(requests1);
14391498

@@ -1454,7 +1513,7 @@ void multipleQueriesTest0() {
14541513

14551514
assertDoesNotThrow(() -> {
14561515
JSONAssert.assertEquals(
1457-
"{\"requests\":[{\"indexName\":\"theIndexName\",\"query\":\"test\",\"type\":\"facet\",\"facet\":\"theFacet\",\"params\":\"testParam\"}],\"strategy\":\"stopIfEnoughMatches\"}",
1516+
"{\"requests\":[{\"indexName\":\"theIndexName\",\"query\":\"test\",\"type\":\"facet\",\"facet\":\"theFacet\",\"params\":\"testParam\"},{\"indexName\":\"theIndexName\",\"query\":\"test\",\"type\":\"default\",\"params\":\"testParam\"}],\"strategy\":\"stopIfEnoughMatches\"}",
14581517
req.getBody(),
14591518
JSONCompareMode.STRICT_ORDER
14601519
);

tests/output/javascript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"test": "jest"
66
},
77
"dependencies": {
8+
"@experimental-api-clients-automation/algoliasearch-lite": "0.0.5",
89
"@experimental-api-clients-automation/client-abtesting": "0.0.5",
910
"@experimental-api-clients-automation/client-analytics": "0.0.5",
1011
"@experimental-api-clients-automation/client-common": "0.0.5",

0 commit comments

Comments
 (0)