Skip to content

Commit

Permalink
chore: add client testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Jan 14, 2022
1 parent a3c6272 commit 0f2050b
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 33 deletions.
143 changes: 143 additions & 0 deletions tests/CTS/client/search/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
[
{
"testName": "client throws with invalid parameters",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"apiKey": "blah"
},
"expected": {
"error": "appId is missing!"
}
}
]
},
{
"testName": "client has instance variables for appId and apiKey",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key"
}
},
{
"type": "variable",
"object": "$client",
"path": ["appId"],
"expected": { "match": "my-app-id" }
},
{
"type": "variable",
"object": "$client",
"path": ["apiKey"],
"expected": { "match": "my-api-key" }
}
]
},
{
"testName": "sets user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": ["setUserAgent"],
"parameters": ["hello"]
},
{
"type": "method",
"object": "$client",
"path": ["getUserAgent"],
"expected": { "match": "hello" }
}
]
},
{
"testName": "save objects and perform a basic search",
"autoCreateIndex": true,
"steps": [
{
"type": "method",
"object": "$index",
"path": ["saveObjects"],
"parameters": [
[
{
"objectID": "julien-lemoine",
"company": "Algolia",
"name": "Julien Lemoine"
},
{
"objectID": "nicolas-dessaigne",
"company": "Algolia",
"name": "Nicolas Dessaigne"
},
{ "company": "Amazon", "name": "Jeff Bezos" },
{ "company": "Apple", "name": "Steve Jobs" },
{ "company": "Apple", "name": "Steve Wozniak" },
{ "company": "Arista Networks", "name": "Jayshree Ullal" },
{ "company": "Google", "name": "Larry Page" },
{ "company": "Google", "name": "Rob Pike" },
{ "company": "Google", "name": "Serguey Brin" },
{ "company": "Microsoft", "name": "Bill Gates" },
{ "company": "SpaceX", "name": "Elon Musk" },
{ "company": "Tesla", "name": "Elon Musk" },
{ "company": "Yahoo", "name": "Marissa Mayer" }
],
{
"autoGenerateObjectIDIfNotExist": true
}
]
},
{
"type": "method",
"object": "$index",
"path": ["setSettings"],
"parameters": [{ "attributesForFaceting": ["searchable(company)"] }]
},
{
"type": "method",
"object": "$index",
"path": ["search"],
"parameters": ["algolia"],
"expected": {
"match": [
{
"objectContaining": {
"name": "Julien Lemoine"
}
},
{
"objectContaining": {
"name": "Nicolas Dessaigne"
}
}
]
}
},
{
"type": "method",
"object": "$index",
"path": ["search"],
"parameters": [
"elon",
{
"facets": ["*"],
"facetFilters": [["company:tesla", "company:spacex"]]
}
],
"expected": {
"length": 2,
"match": [
{ "objectContaining": { "company": "SpaceX" } },
{ "objectContaining": { "company": "Tesla" } }
]
}
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new {{client}}('{{parameters.appId}}', '{{parameters.apiKey}}');
5 changes: 5 additions & 0 deletions tests/CTS/client/templates/javascript/expected.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#error}}
{{/error}}
{{#length}}
expect(result).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 @@
await {{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}});
16 changes: 16 additions & 0 deletions tests/CTS/client/templates/javascript/step.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#isCreateClient}}
const $client = {{> createClient}}
result = $client;
{{/isCreateClient}}

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

{{#isMethod}}
result = {{> method}}
{{/isMethod}}

{{#expected}}
{{> expected}}
{{/expected}}
42 changes: 42 additions & 0 deletions tests/CTS/client/templates/javascript/suite.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { {{client}} } from '{{{import}}}';

const appId = process.env.ALGOLIA_APPLICATION_ID || 'test_app_id';
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'test_api_key';

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

async function createIndex() {
// TODO
}

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

let result;
{{#steps}}
{{#expectedError}}
expect(() => {
{{> step}}
}).toThrowError("{{expectedError}}")
{{/expectedError}}

{{^expectedError}}
{{> step}}
{{/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.
90 changes: 90 additions & 0 deletions tests/output/javascript/tests/client/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { SearchApi } from '@algolia/client-search';

const appId = process.env.ALGOLIA_APPLICATION_ID || 'test_app_id';
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'test_api_key';

function createClient() {
return new SearchApi(appId, apiKey);
}

async function createIndex() {
// TODO
}

describe('basic', () => {
test('client throws with invalid parameters', async () => {
let result;
expect(() => {
const $client = new SearchApi('', 'blah');
result = $client;
}).toThrowError('appId is missing!');
});

test('client has instance variables for appId and apiKey', async () => {
let result;

const $client = new SearchApi('my-app-id', 'my-api-key');
result = $client;

result = $client.appId;

result = $client.apiKey;
});

test('sets user agent', async () => {
const $client = createClient();

let result;

result = await $client.setUserAgent('hello');

result = await $client.getUserAgent();
});

test('save objects and perform a basic search', async () => {
const $client = createClient();
const $index = await createIndex();

let result;

result = await $index.saveObjects(
[
{
objectID: 'julien-lemoine',
company: 'Algolia',
name: 'Julien Lemoine',
},
{
objectID: 'nicolas-dessaigne',
company: 'Algolia',
name: 'Nicolas Dessaigne',
},
{ company: 'Amazon', name: 'Jeff Bezos' },
{ company: 'Apple', name: 'Steve Jobs' },
{ company: 'Apple', name: 'Steve Wozniak' },
{ company: 'Arista Networks', name: 'Jayshree Ullal' },
{ company: 'Google', name: 'Larry Page' },
{ company: 'Google', name: 'Rob Pike' },
{ company: 'Google', name: 'Serguey Brin' },
{ company: 'Microsoft', name: 'Bill Gates' },
{ company: 'SpaceX', name: 'Elon Musk' },
{ company: 'Tesla', name: 'Elon Musk' },
{ company: 'Yahoo', name: 'Marissa Mayer' },
],
{ autoGenerateObjectIDIfNotExist: true }
);

result = await $index.setSettings({
attributesForFaceting: ['searchable(company)'],
});

result = await $index.search('algolia');

result = await $index.search('elon', {
facets: ['*'],
facetFilters: [['company:tesla', 'company:spacex']],
});

expect(result).toHaveLength(2);
});
});
3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"scripts": {
"build": "tsc",
"lint:fix": "yarn workspace javascript-tests lint:fix",
"generate": "yarn generate:methods:requets ${0:-javascript} ${1:-search}",
"generate": "yarn generate:methods:requets ${0:-javascript} ${1:-search} && yarn generate:client ${0:-javascript} ${1:-search}",
"generate:methods:requets": "node dist/tests/src/methods/requests/main.js ${0:-javascript} ${1:-search}",
"generate:client": "node dist/tests/src/client/main.js ${0:-javascript} ${1:-search}",
"start": "yarn build && yarn generate ${0:-javascript} ${1:-search} && yarn lint:fix"
},
"devDependencies": {
Expand Down
Empty file removed tests/src/client/.gitkeep
Empty file.
Loading

0 comments on commit 0f2050b

Please sign in to comment.