Skip to content

Commit 795035b

Browse files
Eunjae Leemillotp
Eunjae Lee
andauthored
chore: add client tests (#88)
* 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]>
1 parent 70369f5 commit 795035b

File tree

28 files changed

+614
-46
lines changed

28 files changed

+614
-46
lines changed

clients/algoliasearch-client-javascript/client-abtesting/src/abtestingApi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export class AbtestingApi {
5555
region: 'de' | 'us',
5656
options?: { requester?: Requester; hosts?: Host[] }
5757
) {
58+
if (!appId) {
59+
throw new Error('`appId` is missing.');
60+
}
61+
if (!apiKey) {
62+
throw new Error('`apiKey` is missing.');
63+
}
64+
if (!region) {
65+
throw new Error('`region` is missing.');
66+
}
67+
5868
this.setAuthentication({ appId, apiKey });
5969

6070
this.transporter = new Transporter({
@@ -72,7 +82,7 @@ export class AbtestingApi {
7282
});
7383
}
7484

75-
getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
85+
getDefaultHosts(region: 'de' | 'us'): Host[] {
7686
return [
7787
{
7888
url: `analytics.${region}.algolia.com`,

clients/algoliasearch-client-javascript/client-analytics/src/analyticsApi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export class AnalyticsApi {
7070
region: 'de' | 'us',
7171
options?: { requester?: Requester; hosts?: Host[] }
7272
) {
73+
if (!appId) {
74+
throw new Error('`appId` is missing.');
75+
}
76+
if (!apiKey) {
77+
throw new Error('`apiKey` is missing.');
78+
}
79+
7380
this.setAuthentication({ appId, apiKey });
7481

7582
this.transporter = new Transporter({

clients/algoliasearch-client-javascript/client-insights/src/insightsApi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export class InsightsApi {
5252
apiKey: string,
5353
options?: { requester?: Requester; hosts?: Host[] }
5454
) {
55+
if (!appId) {
56+
throw new Error('`appId` is missing.');
57+
}
58+
if (!apiKey) {
59+
throw new Error('`apiKey` is missing.');
60+
}
61+
5562
this.setAuthentication({ appId, apiKey });
5663

5764
this.transporter = new Transporter({

clients/algoliasearch-client-javascript/client-personalization/src/personalizationApi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export class PersonalizationApi {
5555
region: 'eu' | 'us',
5656
options?: { requester?: Requester; hosts?: Host[] }
5757
) {
58+
if (!appId) {
59+
throw new Error('`appId` is missing.');
60+
}
61+
if (!apiKey) {
62+
throw new Error('`apiKey` is missing.');
63+
}
64+
if (!region) {
65+
throw new Error('`region` is missing.');
66+
}
67+
5868
this.setAuthentication({ appId, apiKey });
5969

6070
this.transporter = new Transporter({
@@ -72,7 +82,7 @@ export class PersonalizationApi {
7282
});
7383
}
7484

75-
getDefaultHosts(region: 'eu' | 'us' = 'us'): Host[] {
85+
getDefaultHosts(region: 'eu' | 'us'): Host[] {
7686
return [
7787
{
7888
url: `personalization.${region}.algolia.com`,

clients/algoliasearch-client-javascript/client-query-suggestions/src/querySuggestionsApi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ export class QuerySuggestionsApi {
5757
region: 'eu' | 'us',
5858
options?: { requester?: Requester; hosts?: Host[] }
5959
) {
60+
if (!appId) {
61+
throw new Error('`appId` is missing.');
62+
}
63+
if (!apiKey) {
64+
throw new Error('`apiKey` is missing.');
65+
}
66+
if (!region) {
67+
throw new Error('`region` is missing.');
68+
}
69+
6070
this.setAuthentication({ appId, apiKey });
6171

6272
this.transporter = new Transporter({
@@ -74,7 +84,7 @@ export class QuerySuggestionsApi {
7484
});
7585
}
7686

77-
getDefaultHosts(region: 'eu' | 'us' = 'us'): Host[] {
87+
getDefaultHosts(region: 'eu' | 'us'): Host[] {
7888
return [
7989
{
8090
url: `query-suggestions.${region}.algolia.com`,

clients/algoliasearch-client-javascript/client-search/src/searchApi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ export class SearchApi {
106106
apiKey: string,
107107
options?: { requester?: Requester; hosts?: Host[] }
108108
) {
109+
if (!appId) {
110+
throw new Error('`appId` is missing.');
111+
}
112+
if (!apiKey) {
113+
throw new Error('`apiKey` is missing.');
114+
}
115+
109116
this.setAuthentication({ appId, apiKey });
110117

111118
this.transporter = new Transporter({

clients/algoliasearch-client-javascript/recommend/src/recommendApi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export class RecommendApi {
5252
apiKey: string,
5353
options?: { requester?: Requester; hosts?: Host[] }
5454
) {
55+
if (!appId) {
56+
throw new Error('`appId` is missing.');
57+
}
58+
if (!apiKey) {
59+
throw new Error('`apiKey` is missing.');
60+
}
61+
5562
this.setAuthentication({ appId, apiKey });
5663

5764
this.transporter = new Transporter({

openapitools.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"packageName": "@algolia/client-analytics",
8787
"hasRegionalHost": true,
8888
"isDeHost": true,
89+
"fallbackToUS": true,
8990
"host": "analytics"
9091
}
9192
},

templates/javascript/api-single.mustache

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ export class {{classname}} {
5757
{{/hasRegionalHost}}
5858
options?: {requester?: Requester, hosts?: Host[]}
5959
) {
60+
if (!appId) {
61+
throw new Error("`appId` is missing.");
62+
}
63+
if (!apiKey) {
64+
throw new Error("`apiKey` is missing.");
65+
}
66+
{{#hasRegionalHost}}
67+
{{^fallbackToUS}}
68+
if (!region) {
69+
throw new Error("`region` is missing.");
70+
}
71+
{{/fallbackToUS}}
72+
{{/hasRegionalHost}}
73+
6074
this.setAuthentication({ appId, apiKey });
6175

6276
this.transporter = new Transporter({
@@ -96,7 +110,7 @@ export class {{classname}} {
96110

97111
{{^isSearchHost}}
98112
{{#hasRegionalHost}}
99-
public getDefaultHosts(region: {{#isDeHost}}'de'{{/isDeHost}}{{#isEuHost}}'eu'{{/isEuHost}} | 'us' = 'us'): Host[] {
113+
public getDefaultHosts(region: {{#isDeHost}}'de'{{/isDeHost}}{{#isEuHost}}'eu'{{/isEuHost}} | 'us'{{#fallbackToUS}} = 'us'{{/fallbackToUS}}): Host[] {
100114
return [{ url: `{{{host}}}.${region}.algolia.com`, accept: 'readWrite', protocol: 'https' }];
101115
}
102116
{{/hasRegionalHost}}

tests/CTS/client/analytics/basic.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"testName": "does not throw when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": false
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "getAverageClickPosition throws without index",
21+
"steps": [
22+
{
23+
"type": "method",
24+
"object": "$client",
25+
"path": "getClickPositions",
26+
"parameters": [{}],
27+
"expected": {
28+
"error": "Parameter `index` is required when calling `getClickPositions`."
29+
}
30+
}
31+
]
32+
}
33+
]

tests/CTS/client/search/basic.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"testName": "client throws with invalid parameters",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"apiKey": "blah"
10+
},
11+
"expected": {
12+
"error": "`appId` is missing."
13+
}
14+
}
15+
]
16+
}
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
new {{client}}(
2+
'{{parameters.appId}}',
3+
'{{parameters.apiKey}}',
4+
{{#hasRegionalHost}}'{{parameters.region}}',{{/hasRegionalHost}}
5+
{
6+
requester: new EchoRequester()
7+
}
8+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#length}}
2+
expect(actual).toHaveLength({{length}});
3+
{{/length}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{#isCreateClient}}
2+
const $client = {{> createClient}}
3+
actual = $client;
4+
{{/isCreateClient}}
5+
6+
{{#isVariable}}
7+
actual = {{> variable}}
8+
{{/isVariable}}
9+
10+
{{#isMethod}}
11+
actual = {{> method}}
12+
{{/isMethod}}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
/* eslint-disable require-await */
3+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
4+
// @ts-nocheck
5+
import { {{client}}, EchoRequester } from '{{{import}}}';
6+
7+
const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key';
8+
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id';
9+
10+
function createClient(): {{client}} {
11+
return new {{client}}(appId, apiKey, {{#hasRegionalHost}}'us', {{/hasRegionalHost}}{ requester: new EchoRequester() });
12+
}
13+
14+
{{#blocks}}
15+
describe('{{operationId}}', () => {
16+
{{#tests}}
17+
test('{{testName}}', async () => {
18+
{{#autoCreateClient}}
19+
const $client = createClient();
20+
{{/autoCreateClient}}
21+
22+
let actual;
23+
{{#steps}}
24+
{{#expectedError}}
25+
await expect(new Promise((resolve, reject) => {
26+
{{> step}}
27+
if (actual instanceof Promise) {
28+
actual.then(resolve).catch(reject);
29+
} else {
30+
resolve();
31+
}
32+
})).rejects.toThrow("{{{expectedError}}}")
33+
{{/expectedError}}
34+
35+
{{^expectedError}}
36+
{{#expectedNoError}}
37+
await expect(new Promise((resolve, reject) => {
38+
{{> step}}
39+
if (actual instanceof Promise) {
40+
actual.then(resolve).catch(reject);
41+
} else {
42+
resolve();
43+
}
44+
})).resolves.not.toThrow();
45+
{{/expectedNoError}}
46+
47+
{{^expectedNoError}}
48+
{{> step}}
49+
50+
if (actual instanceof Promise) {
51+
actual = await actual;
52+
}
53+
54+
{{#expected}}
55+
{{> expected}}
56+
{{/expected}}
57+
{{/expectedNoError}}
58+
{{/expectedError}}
59+
{{/steps}}
60+
});
61+
62+
{{/tests}}
63+
})
64+
65+
{{/blocks}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{object}}{{#path}}.{{.}}{{/path}};
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// @ts-nocheck
2+
import { AnalyticsApi, EchoRequester } from '@algolia/client-analytics';
3+
4+
const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key';
5+
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id';
6+
7+
function createClient(): AnalyticsApi {
8+
return new AnalyticsApi(appId, apiKey, 'us', {
9+
requester: new EchoRequester(),
10+
});
11+
}
12+
13+
describe('basic', () => {
14+
test('does not throw when region is not given', async () => {
15+
let actual;
16+
17+
await expect(
18+
new Promise((resolve, reject) => {
19+
const $client = new AnalyticsApi('my-app-id', 'my-api-key', '', {
20+
requester: new EchoRequester(),
21+
});
22+
actual = $client;
23+
24+
if (actual instanceof Promise) {
25+
actual.then(resolve).catch(reject);
26+
} else {
27+
resolve();
28+
}
29+
})
30+
).resolves.not.toThrow();
31+
});
32+
33+
test('getAverageClickPosition throws without index', async () => {
34+
const $client = createClient();
35+
36+
let actual;
37+
await expect(
38+
new Promise((resolve, reject) => {
39+
actual = $client.getClickPositions({});
40+
if (actual instanceof Promise) {
41+
actual.then(resolve).catch(reject);
42+
} else {
43+
resolve();
44+
}
45+
})
46+
).rejects.toThrow(
47+
'Parameter `index` is required when calling `getClickPositions`.'
48+
);
49+
});
50+
});

0 commit comments

Comments
 (0)