Skip to content

fix(cts): add e2e tests for composition api [skip-bc] #4111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -81,7 +81,11 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
List<Object> blocksE2E = new ArrayList<>();
ParametersWithDataType paramsType = new ParametersWithDataType(models, language, client, false);

bundle.put("e2eApiKey", client.equals("monitoring") ? "MONITORING_API_KEY" : "ALGOLIA_ADMIN_KEY");
bundle.put("e2eAppID", client.equals("composition") ? "METIS_APPLICATION_ID" : "ALGOLIA_APPLICATION_ID");
bundle.put(
"e2eApiKey",
client.equals("composition") ? "METIS_API_KEY" : (client.equals("monitoring") ? "MONITORING_API_KEY" : "ALGOLIA_ADMIN_KEY")
);
bundle.put("useEchoRequester", true);

for (Map.Entry<String, CodegenOperation> entry : operations.entrySet()) {
Expand Down
3 changes: 3 additions & 0 deletions playground/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ SEARCH_INDEX=""
SEARCH_QUERY=""

ANALYTICS_INDEX=""

METIS_APPLICATION_ID=
METIS_API_KEY=
4 changes: 2 additions & 2 deletions playground/javascript/node/algoliasearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function testAlgoliasearch() {
}

try {
const analyticsClient = client.initAnalytics({region: 'de'});
const analyticsClient = client.initAnalytics({ region: 'de' });

const res = await analyticsClient.getTopFilterForAttribute({
attribute: 'myAttribute1,myAttribute2',
Expand All @@ -97,7 +97,7 @@ async function testAlgoliasearch() {
}

try {
const abtestingClient = client.initAbtesting({region: 'us'});
const abtestingClient = client.initAbtesting({ region: 'us' });

const res = await abtestingClient.getABTest({
id: 42,
Expand Down
35 changes: 31 additions & 4 deletions playground/javascript/node/composition.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { ApiError } from '@algolia/client-common';
import { compositionClient } from '@algolia/client-composition';

const appId = process.env.ALGOLIA_APPLICATION_ID || '**** APP_ID *****';
const apiKey = process.env.ALGOLIA_ADMIN_KEY || '**** ADMIN_KEY *****';
const appId = process.env.METIS_APPLICATION_ID || '**** APP_ID *****';
const apiKey = process.env.METIS_API_KEY || '**** ADMIN_KEY *****';

// Init client with appId and apiKey
const client = compositionClient(appId, apiKey);

async function testComposition() {
try {
const res = await client.listCompositions();

// create a new composition
/*
const res = await client.multipleBatch({
requests: [
{
action: 'upsert',
body: {
objectID: 'id1',
description: 'the first ever composition from the client',
behavior: {
injection: {
main: {
source: {
search: {
index: 'cts_e2e_small',
},
},
},
},
},
},
},
],
});
console.log(`[OK]`, res);
*/

const compos = await client.listCompositions();

console.log(`[OK]`, compos);
} catch (e) {
if (e instanceof ApiError) {
return console.log(`[${e.status}] ${e.message}`, e.stackTrace, e);
Expand Down
3 changes: 1 addition & 2 deletions playground/javascript/node/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApiError } from '@algolia/client-common';
import { apiClientVersion, searchClient } from '@algolia/client-search';
import { SearchQuery } from '@algolia/client-search';
import { apiClientVersion, searchClient, SearchQuery } from '@algolia/client-search';

const appId = process.env.ALGOLIA_APPLICATION_ID || '**** APP_ID *****';
const apiKey = process.env.ALGOLIA_ADMIN_KEY || '**** SEARCH_API_KEY *****';
Expand Down
4 changes: 2 additions & 2 deletions templates/csharp/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class {{client}}RequestTestsE2E
{
DotEnv.Load(options: new DotEnvOptions(ignoreExceptions: true, probeForEnv: true, probeLevelsToSearch: 8, envFilePaths: new[] { ".env" }));

var appId = Environment.GetEnvironmentVariable("ALGOLIA_APPLICATION_ID");
var appId = Environment.GetEnvironmentVariable("{{e2eAppID}}");
if (appId == null)
{
throw new Exception("please provide an `ALGOLIA_APPLICATION_ID` env var for e2e tests");
throw new Exception("please provide an `{{e2eAppID}}` env var for e2e tests");
}

var apiKey = Environment.GetEnvironmentVariable("{{e2eApiKey}}");
Expand Down
4 changes: 2 additions & 2 deletions templates/go/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
func createE2E{{#lambda.titlecase}}{{clientPrefix}}{{/lambda.titlecase}}Client(t *testing.T) *{{clientPrefix}}.APIClient {
t.Helper()
appID := os.Getenv("ALGOLIA_APPLICATION_ID")
appID := os.Getenv("{{e2eAppID}}")
if appID == "" && os.Getenv("CI") != "true" {
err := godotenv.Load("../../../../.env")
require.NoError(t, err)
appID = os.Getenv("ALGOLIA_APPLICATION_ID")
appID = os.Getenv("{{e2eAppID}}")
}
apiKey := os.Getenv("{{e2eApiKey}}")
client, err := {{clientPrefix}}.NewClient(appID, apiKey, {{#hasRegionalHost}}{{clientPrefix}}.{{#lambda.uppercase}}{{defaultRegion}}{{/lambda.uppercase}},{{/hasRegionalHost}})
Expand Down
4 changes: 2 additions & 2 deletions templates/java/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class {{client}}RequestsTestsE2E {
this.json = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build();
if ("true".equals(System.getenv("CI"))) {
this.client = new {{client}}(System.getenv("ALGOLIA_APPLICATION_ID"), System.getenv("{{e2eApiKey}}"){{#hasRegionalHost}}, "{{defaultRegion}}"{{/hasRegionalHost}});
this.client = new {{client}}(System.getenv("{{e2eAppID}}"), System.getenv("{{e2eApiKey}}"){{#hasRegionalHost}}, "{{defaultRegion}}"{{/hasRegionalHost}});
} else {
Dotenv dotenv = Dotenv.configure().directory("../../").load();
this.client = new {{client}}(dotenv.get("ALGOLIA_APPLICATION_ID"), dotenv.get("{{e2eApiKey}}"){{#hasRegionalHost}}, "{{defaultRegion}}"{{/hasRegionalHost}});
this.client = new {{client}}(dotenv.get("{{e2eAppID}}"), dotenv.get("{{e2eApiKey}}"){{#hasRegionalHost}}, "{{defaultRegion}}"{{/hasRegionalHost}});
}
}

Expand Down
6 changes: 3 additions & 3 deletions templates/javascript/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { compositionClient } from '@algolia/client-composition';
{{/isCompositionClient}}
import type { RequestOptions } from '@algolia/client-common';

if (!process.env.ALGOLIA_APPLICATION_ID) {
throw new Error("please provide an `ALGOLIA_APPLICATION_ID` env var for e2e tests");
if (!process.env.{{e2eAppID}}) {
throw new Error("please provide an `{{e2eAppID}}` env var for e2e tests");
}

if (!process.env.{{e2eApiKey}}) {
throw new Error("please provide an `{{e2eApiKey}}` env var for e2e tests");
}

const client = {{^isCompositionClient}}{{{clientName}}}{{/isCompositionClient}}{{#isCompositionClient}}compositionClient{{/isCompositionClient}}(process.env.ALGOLIA_APPLICATION_ID, process.env.{{e2eApiKey}}){{^isStandaloneClient}}.{{{initMethod}}}({{#hasRegionalHost}} {region:'{{{defaultRegion}}}'} {{/hasRegionalHost}}){{/isStandaloneClient}};
const client = {{^isCompositionClient}}{{{clientName}}}{{/isCompositionClient}}{{#isCompositionClient}}compositionClient{{/isCompositionClient}}(process.env.{{e2eAppID}}, process.env.{{e2eApiKey}}){{^isStandaloneClient}}.{{{initMethod}}}({{#hasRegionalHost}} {region:'{{{defaultRegion}}}'} {{/hasRegionalHost}}){{/isStandaloneClient}};

{{#blocksE2E}}
describe('{{operationId}}', () => {
Expand Down
4 changes: 2 additions & 2 deletions templates/kotlin/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class {{clientPrefix}}Test {

init {
if (System.getenv("CI") == "true") {
this.client = {{client}}(appId = System.getenv("ALGOLIA_APPLICATION_ID"), apiKey = System.getenv("{{e2eApiKey}}"){{#hasRegionalHost}}, region = "{{defaultRegion}}", {{/hasRegionalHost}});
this.client = {{client}}(appId = System.getenv("{{e2eAppID}}"), apiKey = System.getenv("{{e2eApiKey}}"){{#hasRegionalHost}}, region = "{{defaultRegion}}", {{/hasRegionalHost}});
} else {
val dotenv = Dotenv.configure().directory("../../").load()
this.client = {{client}}(appId = dotenv["ALGOLIA_APPLICATION_ID"], apiKey = dotenv["{{e2eApiKey}}"]{{#hasRegionalHost}}, region = "{{defaultRegion}}", {{/hasRegionalHost}});
this.client = {{client}}(appId = dotenv["{{e2eAppID}}"], apiKey = dotenv["{{e2eApiKey}}"]{{#hasRegionalHost}}, region = "{{defaultRegion}}", {{/hasRegionalHost}});
}
}

Expand Down
4 changes: 2 additions & 2 deletions templates/php/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use Psr\Http\Message\RequestInterface;
use Dotenv\Dotenv;

// we only read .env file if we run locally
if (getenv('ALGOLIA_APPLICATION_ID')) {
if (getenv('{{e2eAppID}}')) {
$_ENV = getenv();
} else {
$dotenv = Dotenv::createImmutable('tests');
Expand All @@ -44,7 +44,7 @@ class {{clientPrefix}}Test extends TestCase

protected function getClient(): {{client}}
{
return {{client}}::create($_ENV['ALGOLIA_APPLICATION_ID'], $_ENV['{{e2eApiKey}}']{{#hasRegionalHost}},'{{defaultRegion}}' {{/hasRegionalHost}});
return {{client}}::create($_ENV['{{e2eAppID}}'], $_ENV['{{e2eApiKey}}']{{#hasRegionalHost}},'{{defaultRegion}}' {{/hasRegionalHost}});
}

{{#blocksE2E}}
Expand Down
4 changes: 2 additions & 2 deletions templates/python/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ load_dotenv("../../.env")
{{#modes}}
class Test{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}{{#isSyncClient}}Sync{{/isSyncClient}}E2E:
_helpers = Helpers()
_e2e_app_id = environ.get("ALGOLIA_APPLICATION_ID")
_e2e_app_id = environ.get("{{e2eAppID}}")
if _e2e_app_id is None:
raise Exception("please provide an `ALGOLIA_APPLICATION_ID` env var for e2e tests")
raise Exception("please provide an `{{e2eAppID}}` env var for e2e tests")

_e2e_api_key = environ.get("{{e2eApiKey}}")
if _e2e_api_key is None:
Expand Down
2 changes: 1 addition & 1 deletion templates/ruby/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Dotenv.load('../../.env')
class Test{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}E2E < Test::Unit::TestCase
def setup
@client = Algolia::{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}.create(
ENV.fetch('ALGOLIA_APPLICATION_ID', nil),
ENV.fetch('{{e2eAppID}}', nil),
ENV.fetch('{{e2eApiKey}}', nil){{#hasRegionalHost}},
'{{{defaultRegion}}}'{{/hasRegionalHost}}
)
Expand Down
4 changes: 2 additions & 2 deletions templates/scala/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class {{clientPrefix}}TestE2E extends AnyFunSuite {
{{/hasRegionalHost}}
if (System.getenv("CI") == "true") {
{{client}}(
appId = System.getenv("ALGOLIA_APPLICATION_ID"),
appId = System.getenv("{{e2eAppID}}"),
apiKey = System.getenv("{{e2eApiKey}}"){{#hasRegionalHost}},
region = region{{/hasRegionalHost}}
)
} else {
val dotenv = Dotenv.configure.directory("../../").load
{{client}}(
appId = dotenv.get("ALGOLIA_APPLICATION_ID"),
appId = dotenv.get("{{e2eAppID}}"),
apiKey = dotenv.get("{{e2eApiKey}}"){{#hasRegionalHost}},
region = region{{/hasRegionalHost}}
)
Expand Down
4 changes: 2 additions & 2 deletions templates/swift/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ final class {{client}}RequestsTestsE2E: XCTestCase {
}

do {
APPLICATION_ID = try XCTUnwrap(ProcessInfo.processInfo.environment["ALGOLIA_APPLICATION_ID"])
APPLICATION_ID = try XCTUnwrap(ProcessInfo.processInfo.environment["{{e2eAppID}}"])
} catch {
XCTFail("Please provide an `ALGOLIA_APPLICATION_ID` env var for e2e tests")
XCTFail("Please provide an `{{e2eAppID}}` env var for e2e tests")
}

do {
Expand Down
4 changes: 4 additions & 0 deletions tests/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ALGOLIA_APPLICATION_ID=
ALGOLIA_ADMIN_KEY=

MONITORING_API_KEY=

METIS_APPLICATION_ID=
METIS_API_KEY=
23 changes: 23 additions & 0 deletions tests/CTS/requests/composition/listCompositions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
"request": {
"path": "/1/compositions",
"method": "GET"
},
"response": {
"statusCode": 200,
"body": {
"items": [
{
"objectID": "id1",
"description": "the first ever composition from the client",
"behavior": {
"injection": {
"main": {
"source": {
"search": {
"index": "cts_e2e_small"
}
}
}
}
}
}
],
"nbPages": 1
}
}
}
]
Loading