Skip to content

Commit

Permalink
chore: add client tests (#88)
Browse files Browse the repository at this point in the history
* chore: add client testing

* chore: skip when template is missing

* chore: skip when tests for client don't exist

* test region WIP

* fix tests

* remove unnecessary url from echo requester

* fix: update template to check region

* fix: add hasReigonalHost to generators

* fix: make regional optional in client testing

* fix: test asynchronous errors

* update tests

* fix: remove duplicated import statements

* chore: remove unused part

* chore: format cts output

* fix: remove createIndex

* chore: do not use post- script in package.json

* fix: type issues

* Update tests/CTS/client/templates/javascript/suite.mustache

Co-authored-by: Pierre Millot <[email protected]>

* chore: add eslint to tests

* chore: update output

* run java cts on the CI

* Revert "run java cts on the CI"

This reverts commit 7a0358d.

Co-authored-by: Pierre Millot <[email protected]>
  • Loading branch information
Eunjae Lee and millotp authored Jan 26, 2022
1 parent 70369f5 commit 795035b
Show file tree
Hide file tree
Showing 28 changed files with 614 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export class AbtestingApi {
region: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
if (!region) {
throw new Error('`region` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand All @@ -72,7 +82,7 @@ export class AbtestingApi {
});
}

getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
getDefaultHosts(region: 'de' | 'us'): Host[] {
return [
{
url: `analytics.${region}.algolia.com`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export class AnalyticsApi {
region: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class InsightsApi {
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export class PersonalizationApi {
region: 'eu' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
if (!region) {
throw new Error('`region` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand All @@ -72,7 +82,7 @@ export class PersonalizationApi {
});
}

getDefaultHosts(region: 'eu' | 'us' = 'us'): Host[] {
getDefaultHosts(region: 'eu' | 'us'): Host[] {
return [
{
url: `personalization.${region}.algolia.com`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export class QuerySuggestionsApi {
region: 'eu' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
if (!region) {
throw new Error('`region` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand All @@ -74,7 +84,7 @@ export class QuerySuggestionsApi {
});
}

getDefaultHosts(region: 'eu' | 'us' = 'us'): Host[] {
getDefaultHosts(region: 'eu' | 'us'): Host[] {
return [
{
url: `query-suggestions.${region}.algolia.com`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ export class SearchApi {
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class RecommendApi {
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}
if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand Down
1 change: 1 addition & 0 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"packageName": "@algolia/client-analytics",
"hasRegionalHost": true,
"isDeHost": true,
"fallbackToUS": true,
"host": "analytics"
}
},
Expand Down
16 changes: 15 additions & 1 deletion templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ export class {{classname}} {
{{/hasRegionalHost}}
options?: {requester?: Requester, hosts?: Host[]}
) {
if (!appId) {
throw new Error("`appId` is missing.");
}
if (!apiKey) {
throw new Error("`apiKey` is missing.");
}
{{#hasRegionalHost}}
{{^fallbackToUS}}
if (!region) {
throw new Error("`region` is missing.");
}
{{/fallbackToUS}}
{{/hasRegionalHost}}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand Down Expand Up @@ -96,7 +110,7 @@ export class {{classname}} {

{{^isSearchHost}}
{{#hasRegionalHost}}
public getDefaultHosts(region: {{#isDeHost}}'de'{{/isDeHost}}{{#isEuHost}}'eu'{{/isEuHost}} | 'us' = 'us'): Host[] {
public getDefaultHosts(region: {{#isDeHost}}'de'{{/isDeHost}}{{#isEuHost}}'eu'{{/isEuHost}} | 'us'{{#fallbackToUS}} = 'us'{{/fallbackToUS}}): Host[] {
return [{ url: `{{{host}}}.${region}.algolia.com`, accept: 'readWrite', protocol: 'https' }];
}
{{/hasRegionalHost}}
Expand Down
33 changes: 33 additions & 0 deletions tests/CTS/client/analytics/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"testName": "does not throw when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": false
}
}
]
},
{
"testName": "getAverageClickPosition throws without index",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getClickPositions",
"parameters": [{}],
"expected": {
"error": "Parameter `index` is required when calling `getClickPositions`."
}
}
]
}
]
17 changes: 17 additions & 0 deletions tests/CTS/client/search/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"testName": "client throws with invalid parameters",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"apiKey": "blah"
},
"expected": {
"error": "`appId` is missing."
}
}
]
}
]
8 changes: 8 additions & 0 deletions tests/CTS/client/templates/javascript/createClient.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
new {{client}}(
'{{parameters.appId}}',
'{{parameters.apiKey}}',
{{#hasRegionalHost}}'{{parameters.region}}',{{/hasRegionalHost}}
{
requester: new EchoRequester()
}
)
3 changes: 3 additions & 0 deletions tests/CTS/client/templates/javascript/expected.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#length}}
expect(actual).toHaveLength({{length}});
{{/length}}
1 change: 1 addition & 0 deletions tests/CTS/client/templates/javascript/method.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}});
12 changes: 12 additions & 0 deletions tests/CTS/client/templates/javascript/step.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#isCreateClient}}
const $client = {{> createClient}}
actual = $client;
{{/isCreateClient}}

{{#isVariable}}
actual = {{> variable}}
{{/isVariable}}

{{#isMethod}}
actual = {{> method}}
{{/isMethod}}
65 changes: 65 additions & 0 deletions tests/CTS/client/templates/javascript/suite.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable require-await */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
// @ts-nocheck
import { {{client}}, EchoRequester } from '{{{import}}}';

const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key';
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id';

function createClient(): {{client}} {
return new {{client}}(appId, apiKey, {{#hasRegionalHost}}'us', {{/hasRegionalHost}}{ requester: new EchoRequester() });
}

{{#blocks}}
describe('{{operationId}}', () => {
{{#tests}}
test('{{testName}}', async () => {
{{#autoCreateClient}}
const $client = createClient();
{{/autoCreateClient}}

let actual;
{{#steps}}
{{#expectedError}}
await expect(new Promise((resolve, reject) => {
{{> step}}
if (actual instanceof Promise) {
actual.then(resolve).catch(reject);
} else {
resolve();
}
})).rejects.toThrow("{{{expectedError}}}")
{{/expectedError}}

{{^expectedError}}
{{#expectedNoError}}
await expect(new Promise((resolve, reject) => {
{{> step}}
if (actual instanceof Promise) {
actual.then(resolve).catch(reject);
} else {
resolve();
}
})).resolves.not.toThrow();
{{/expectedNoError}}

{{^expectedNoError}}
{{> step}}

if (actual instanceof Promise) {
actual = await actual;
}

{{#expected}}
{{> expected}}
{{/expected}}
{{/expectedNoError}}
{{/expectedError}}
{{/steps}}
});

{{/tests}}
})

{{/blocks}}
1 change: 1 addition & 0 deletions tests/CTS/client/templates/javascript/variable.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{object}}{{#path}}.{{.}}{{/path}};
File renamed without changes.
50 changes: 50 additions & 0 deletions tests/output/javascript/tests/client/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @ts-nocheck
import { AnalyticsApi, EchoRequester } from '@algolia/client-analytics';

const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key';
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id';

function createClient(): AnalyticsApi {
return new AnalyticsApi(appId, apiKey, 'us', {
requester: new EchoRequester(),
});
}

describe('basic', () => {
test('does not throw when region is not given', async () => {
let actual;

await expect(
new Promise((resolve, reject) => {
const $client = new AnalyticsApi('my-app-id', 'my-api-key', '', {
requester: new EchoRequester(),
});
actual = $client;

if (actual instanceof Promise) {
actual.then(resolve).catch(reject);
} else {
resolve();
}
})
).resolves.not.toThrow();
});

test('getAverageClickPosition throws without index', async () => {
const $client = createClient();

let actual;
await expect(
new Promise((resolve, reject) => {
actual = $client.getClickPositions({});
if (actual instanceof Promise) {
actual.then(resolve).catch(reject);
} else {
resolve();
}
})
).rejects.toThrow(
'Parameter `index` is required when calling `getClickPositions`.'
);
});
});
Loading

0 comments on commit 795035b

Please sign in to comment.