Skip to content

Commit 883b38a

Browse files
authored
chore: add tests for WithTransformation methods (#4936)
1 parent 14cf2fc commit 883b38a

File tree

14 files changed

+470
-188
lines changed

14 files changed

+470
-188
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/runCts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { assertValidAccountCopyIndex } from './testServer/accountCopyIndex.ts';
99
import { printBenchmarkReport } from './testServer/benchmark.ts';
1010
import { assertChunkWrapperValid } from './testServer/chunkWrapper.ts';
1111
import { startTestServer } from './testServer/index.ts';
12+
import { assertPushMockValid } from './testServer/pushMock.ts';
1213
import { assertValidReplaceAllObjects } from './testServer/replaceAllObjects.ts';
1314
import { assertValidReplaceAllObjectsFailed } from './testServer/replaceAllObjectsFailed.ts';
1415
import { assertValidReplaceAllObjectsScopes } from './testServer/replaceAllObjectsScopes.ts';
@@ -157,6 +158,7 @@ export async function runCts(
157158
assertValidReplaceAllObjectsFailed(languages.length - skip('dart'));
158159
assertValidReplaceAllObjectsScopes(languages.length - skip('dart'));
159160
assertValidWaitForApiKey(languages.length - skip('dart'));
161+
assertPushMockValid(only('javascript') + only('go') + only('python'));
160162
}
161163
if (withBenchmarkServer) {
162164
printBenchmarkReport();

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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
action: 'addObject',
51+
records: [
52+
{ objectID: '1', name: 'Adam' },
53+
{ objectID: '2', name: 'Benoit' },
54+
],
55+
});
56+
57+
res.json({
58+
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
59+
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
60+
message: 'OK',
61+
createdAt: '2022-05-12T06:24:30.049Z',
62+
});
63+
64+
break;
65+
case 'partialUpdateObjectsWithTransformation':
66+
expect(req.body).to.deep.equal({
67+
action: 'partialUpdateObject',
68+
records: [
69+
{ objectID: '1', name: 'Adam' },
70+
{ objectID: '2', name: 'Benoit' },
71+
],
72+
});
73+
74+
res.json({
75+
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
76+
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
77+
message: 'OK',
78+
createdAt: '2022-05-12T06:24:30.049Z',
79+
});
80+
break;
81+
default:
82+
throw new Error('unknown helper');
83+
}
84+
});
85+
}
86+
87+
export function pushMockServer(): Promise<Server> {
88+
return setupServer('pushMock', 6689, addRoutes);
89+
}

specs/search/helpers/partialUpdateObjectsWithTransformation.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
method:
22
post:
33
x-helper: true
4+
x-available-languages:
5+
- javascript
6+
- go
7+
- python
48
tags:
59
- Records
610
operationId: partialUpdateObjectsWithTransformation

specs/search/helpers/saveObjectsWithTransformation.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
method:
22
get:
33
x-helper: true
4+
x-available-languages:
5+
- javascript
6+
- go
7+
- python
48
tags:
59
- Records
610
operationId: saveObjectsWithTransformation

templates/go/client.mustache

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,26 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
8585
}
8686

8787
{{#isSearchClient}}
88-
if cfg.Transformation != nil && cfg.Transformation.Region != "" {
89-
ingestionClient, err := ingestion.NewClient(apiClient.appID, cfg.ApiKey, cfg.Transformation.Region)
90-
if err != nil {
91-
return nil, err //nolint:wrapcheck
88+
if cfg.Transformation != nil && cfg.Transformation.Region != "" {
89+
ingestionConfig := ingestion.IngestionConfiguration{
90+
Configuration: transport.Configuration{
91+
AppID: cfg.AppID,
92+
ApiKey: cfg.ApiKey,
93+
},
94+
Region: cfg.Transformation.Region,
9295
}
9396

94-
apiClient.ingestionTransporter = ingestionClient
95-
}
97+
if len(cfg.Hosts) > 0 {
98+
ingestionConfig.Hosts = cfg.Hosts
99+
}
100+
101+
ingestionClient, err := ingestion.NewClientWithConfig(ingestionConfig)
102+
if err != nil {
103+
return nil, err //nolint:wrapcheck
104+
}
105+
106+
apiClient.ingestionTransporter = ingestionClient
107+
}
96108
{{/isSearchClient}}
97109

98110
return &apiClient, nil

templates/go/tests/client/createClient.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ cfg = {{clientPrefix}}.{{clientName}}Configuration{
1313
{{/gzipEncoding}}
1414
},{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region.value}}
1515
Region: {{clientPrefix}}.Region("{{parametersWithDataTypeMap.region.value}}"),{{/parametersWithDataTypeMap.region.value}}{{/hasRegionalHost}}
16+
{{#hasTransformationRegion}}
17+
Transformation: &search.TransformationConfiguration{ Region: "{{{transformationRegion}}}" },
18+
{{/hasTransformationRegion}}
1619
}
1720
client, err = {{clientPrefix}}.NewClientWithConfig(cfg)

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}}}(

templates/python/api.mustache

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
5454
config = {{#lambda.pascalcase}}{{client}}Config{{/lambda.pascalcase}}(transporter.config.app_id, transporter.config.api_key{{#hasRegionalHost}}, region{{/hasRegionalHost}})
5555
elif config is None:
5656
config = {{#lambda.pascalcase}}{{client}}Config{{/lambda.pascalcase}}(app_id, api_key{{#hasRegionalHost}}, region{{/hasRegionalHost}})
57-
{{#isSearchClient}}
58-
elif config.region is not None:
59-
self._ingestion_transporter = IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(IngestionConfig(config.app_id, config.api_key, config.region))
60-
{{/isSearchClient}}
6157

6258
self._config = config
6359
self._request_options = RequestOptions(config)
@@ -84,7 +80,19 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
8480
if transporter is None:
8581
transporter = Transporter{{#isSyncClient}}Sync{{/isSyncClient}}(config)
8682

87-
return {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}(app_id=config.app_id, api_key=config.api_key, {{#hasRegionalHost}}region=config.region, {{/hasRegionalHost}}transporter=transporter, config=config)
83+
client = {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}(app_id=config.app_id, api_key=config.api_key, {{#hasRegionalHost}}region=config.region, {{/hasRegionalHost}}transporter=transporter, config=config)
84+
85+
{{#isSearchClient}}
86+
if config.region is not None:
87+
ingestion_config = IngestionConfig(config.app_id, config.api_key, config.region)
88+
89+
if config.hosts is not None:
90+
ingestion_config.hosts = config.hosts
91+
92+
client._ingestion_transporter = IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(ingestion_config)
93+
{{/isSearchClient}}
94+
95+
return client
8896

8997
{{^isSyncClient}}
9098
async def __aenter__(self) -> Self:

0 commit comments

Comments
 (0)