Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
stepOut.put("customHosts", step.parameters.get("customHosts"));
}

boolean hasTransformationRegion = step.parameters != null && step.parameters.containsKey("transformationRegion");
if (hasTransformationRegion) testOut.put("useEchoRequester", false);
stepOut.put("hasTransformationRegion", hasTransformationRegion);
if (hasTransformationRegion) {
stepOut.put("transformationRegion", step.parameters.get("transformationRegion"));
}

boolean gzipEncoding = step.parameters != null && step.parameters.getOrDefault("gzip", false).equals(true);
// many languages don't support gzip yet
if (
Expand Down
2 changes: 2 additions & 0 deletions scripts/cts/runCts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { assertValidAccountCopyIndex } from './testServer/accountCopyIndex.ts';
import { printBenchmarkReport } from './testServer/benchmark.ts';
import { assertChunkWrapperValid } from './testServer/chunkWrapper.ts';
import { startTestServer } from './testServer/index.ts';
import { assertPushMockValid } from './testServer/pushMock.ts';
import { assertValidReplaceAllObjects } from './testServer/replaceAllObjects.ts';
import { assertValidReplaceAllObjectsFailed } from './testServer/replaceAllObjectsFailed.ts';
import { assertValidReplaceAllObjectsScopes } from './testServer/replaceAllObjectsScopes.ts';
Expand Down Expand Up @@ -157,6 +158,7 @@ export async function runCts(
assertValidReplaceAllObjectsFailed(languages.length - skip('dart'));
assertValidReplaceAllObjectsScopes(languages.length - skip('dart'));
assertValidWaitForApiKey(languages.length - skip('dart'));
assertPushMockValid(only('javascript') + only('go') + only('python'));
}
if (withBenchmarkServer) {
printBenchmarkReport();
Expand Down
2 changes: 2 additions & 0 deletions scripts/cts/testServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { apiKeyServer } from './apiKey.ts';
import { benchmarkServer } from './benchmark.ts';
import { chunkWrapperServer } from './chunkWrapper.ts';
import { gzipServer } from './gzip.ts';
import { pushMockServer } from './pushMock.ts';
import { replaceAllObjectsServer } from './replaceAllObjects.ts';
import { replaceAllObjectsServerFailed } from './replaceAllObjectsFailed.ts';
import { replaceAllObjectsScopesServer } from './replaceAllObjectsScopes.ts';
Expand All @@ -35,6 +36,7 @@ export async function startTestServer(suites: Record<CTSType, boolean>): Promise
waitForApiKeyServer(),
apiKeyServer(),
algoliaMockServer(),
pushMockServer(),
);
}
if (suites.benchmark) {
Expand Down
89 changes: 89 additions & 0 deletions scripts/cts/testServer/pushMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Server } from 'http';

import { expect } from 'chai';
import type { Express } from 'express';
import express from 'express';

import { setupServer } from './index.ts';

const pushMockState: Record<string, any> = {};

export function assertPushMockValid(expectedCount: number): void {
if (Object.values(pushMockState).length !== expectedCount) {
throw new Error('unexpected number of call to pushMock');
}
for (const [lang, state] of Object.entries(pushMockState)) {
let numberOfTestSuites = 1;

if (lang === 'python') {
numberOfTestSuites = 2;
}

expect(state).to.deep.equal({
saveObjectsWithTransformation: Number(numberOfTestSuites),
partialUpdateObjectsWithTransformation: Number(numberOfTestSuites),
});
}
}

function addRoutes(app: Express): void {
app.use(express.urlencoded({ extended: true }));
app.use(
express.json({
type: ['application/json', 'text/plain'], // the js client sends the body as text/plain
}),
);

app.post('/1/push/:indexName', (req, res) => {
const match = req.params.indexName.match(/^cts_e2e_(\w+)_(.*)$/);
const helper = match?.[1] as string;
const lang = match?.[2] as string;

if (!pushMockState[lang]) {
pushMockState[lang] = {};
}

pushMockState[lang][helper] = (pushMockState[lang][helper] ?? 0) + 1;
switch (helper) {
case 'saveObjectsWithTransformation':
expect(req.body).to.deep.equal({
action: 'addObject',
records: [
{ objectID: '1', name: 'Adam' },
{ objectID: '2', name: 'Benoit' },
],
});

res.json({
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
message: 'OK',
createdAt: '2022-05-12T06:24:30.049Z',
});

break;
case 'partialUpdateObjectsWithTransformation':
expect(req.body).to.deep.equal({
action: 'partialUpdateObject',
records: [
{ objectID: '1', name: 'Adam' },
{ objectID: '2', name: 'Benoit' },
],
});

res.json({
runID: 'b1b7a982-524c-40d2-bb7f-48aab075abda',
eventID: '113b2068-6337-4c85-b5c2-e7b213d82925',
message: 'OK',
createdAt: '2022-05-12T06:24:30.049Z',
});
break;
default:
throw new Error('unknown helper');
}
});
}

export function pushMockServer(): Promise<Server> {
return setupServer('pushMock', 6689, addRoutes);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
method:
post:
x-helper: true
x-available-languages:
- javascript
- go
- python
tags:
- Records
operationId: partialUpdateObjectsWithTransformation
Expand Down
4 changes: 4 additions & 0 deletions specs/search/helpers/saveObjectsWithTransformation.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
method:
get:
x-helper: true
x-available-languages:
- javascript
- go
- python
tags:
- Records
operationId: saveObjectsWithTransformation
Expand Down
24 changes: 18 additions & 6 deletions templates/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,26 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
}

{{#isSearchClient}}
if cfg.Transformation != nil && cfg.Transformation.Region != "" {
ingestionClient, err := ingestion.NewClient(apiClient.appID, cfg.ApiKey, cfg.Transformation.Region)
if err != nil {
return nil, err //nolint:wrapcheck
if cfg.Transformation != nil && cfg.Transformation.Region != "" {
ingestionConfig := ingestion.IngestionConfiguration{
Configuration: transport.Configuration{
AppID: cfg.AppID,
ApiKey: cfg.ApiKey,
},
Region: cfg.Transformation.Region,
}

apiClient.ingestionTransporter = ingestionClient
}
if len(cfg.Hosts) > 0 {
ingestionConfig.Hosts = cfg.Hosts
}

ingestionClient, err := ingestion.NewClientWithConfig(ingestionConfig)
if err != nil {
return nil, err //nolint:wrapcheck
}

apiClient.ingestionTransporter = ingestionClient
}
{{/isSearchClient}}

return &apiClient, nil
Expand Down
3 changes: 3 additions & 0 deletions templates/go/tests/client/createClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ cfg = {{clientPrefix}}.{{clientName}}Configuration{
{{/gzipEncoding}}
},{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region.value}}
Region: {{clientPrefix}}.Region("{{parametersWithDataTypeMap.region.value}}"),{{/parametersWithDataTypeMap.region.value}}{{/hasRegionalHost}}
{{#hasTransformationRegion}}
Transformation: &search.TransformationConfiguration{ Region: "{{{transformationRegion}}}" },
{{/hasTransformationRegion}}
}
client, err = {{clientPrefix}}.NewClientWithConfig(cfg)
5 changes: 4 additions & 1 deletion templates/javascript/tests/client/createClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
protocol: 'http'
},
{{/customHosts}}
]
],
{{/hasCustomHosts}}
{{#hasTransformationRegion}}
transformation: { region : "{{{transformationRegion}}}" },
{{/hasTransformationRegion}}
}
{{/isStandaloneClient}}
){{^isStandaloneClient}}.{{{initMethod}}}(
Expand Down
18 changes: 13 additions & 5 deletions templates/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
config = {{#lambda.pascalcase}}{{client}}Config{{/lambda.pascalcase}}(transporter.config.app_id, transporter.config.api_key{{#hasRegionalHost}}, region{{/hasRegionalHost}})
elif config is None:
config = {{#lambda.pascalcase}}{{client}}Config{{/lambda.pascalcase}}(app_id, api_key{{#hasRegionalHost}}, region{{/hasRegionalHost}})
{{#isSearchClient}}
elif config.region is not None:
self._ingestion_transporter = IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(IngestionConfig(config.app_id, config.api_key, config.region))
{{/isSearchClient}}

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

return {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}(app_id=config.app_id, api_key=config.api_key, {{#hasRegionalHost}}region=config.region, {{/hasRegionalHost}}transporter=transporter, config=config)
client = {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}(app_id=config.app_id, api_key=config.api_key, {{#hasRegionalHost}}region=config.region, {{/hasRegionalHost}}transporter=transporter, config=config)

{{#isSearchClient}}
if config.region is not None:
ingestion_config = IngestionConfig(config.app_id, config.api_key, config.region)

if config.hosts is not None:
ingestion_config.hosts = config.hosts

client._ingestion_transporter = IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(ingestion_config)
{{/isSearchClient}}

return client

{{^isSyncClient}}
async def __aenter__(self) -> Self:
Expand Down
3 changes: 3 additions & 0 deletions templates/python/tests/client/createClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ _config = {{#lambda.pascalcase}}{{clientPrefix}}Config{{/lambda.pascalcase}}("{{
{{#hasCustomHosts}}
{{#isError}} {{/isError}}_config.hosts = HostsCollection([{{#customHosts}}Host(url='localhost' if environ.get('CI') == 'true' else 'host.docker.internal', scheme='http', port={{port}}){{^-last}},{{/-last}}{{/customHosts}}])
{{/hasCustomHosts}}
{{#hasTransformationRegion}}
{{#isError}} {{/isError}}_config.with_transformation("{{{transformationRegion}}}")
{{/hasTransformationRegion}}
{{#isError}} {{/isError}}_client = {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(config=_config{{#useEchoRequester}}, transporter=EchoTransporter{{#isSyncClient}}Sync{{/isSyncClient}}(_config){{/useEchoRequester}})
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[
{
"testName": "call partialUpdateObjectsWithTransformation with createIfNotExists=true",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "test-app-id",
"apiKey": "test-api-key",
"customHosts": [
{
"port": 6689
}
],
"transformationRegion": "us"
}
},
{
"type": "method",
"method": "partialUpdateObjectsWithTransformation",
"parameters": {
"indexName": "cts_e2e_partialUpdateObjectsWithTransformation_${{language}}",
"objects": [
{
"objectID": "1",
"name": "Adam"
},
{
"objectID": "2",
"name": "Benoit"
}
],
"createIfNotExists": true
},
"expected": {
"type": "response",
"match": {
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
"message": "OK",
"createdAt": "2022-05-12T06:24:30.049Z"
}
}
}
]
}
]
47 changes: 47 additions & 0 deletions tests/CTS/client/search/saveObjectsWithTransformation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"testName": "call saveObjectsWithTransformation without error",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "test-app-id",
"apiKey": "test-api-key",
"customHosts": [
{
"port": 6689
}
],
"transformationRegion": "us"
}
},
{
"type": "method",
"method": "saveObjectsWithTransformation",
"parameters": {
"indexName": "cts_e2e_saveObjectsWithTransformation_${{language}}",
"objects": [
{
"objectID": "1",
"name": "Adam"
},
{
"objectID": "2",
"name": "Benoit"
}
]
},
"expected": {
"type": "response",
"match": {
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
"message": "OK",
"createdAt": "2022-05-12T06:24:30.049Z"
}
}
}
]
}
]
Loading