feat: extend e2e procedure

This commit is contained in:
Rainer Killinger
2019-11-25 16:47:54 +01:00
parent 91de58b5ae
commit dc79dc8feb
3 changed files with 173 additions and 49 deletions

View File

@@ -12,13 +12,14 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThingType} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import * as commander from 'commander';
import {readFileSync} from 'fs';
import {join} from 'path';
import {URL} from 'url';
import {copy} from './copy';
import {indexSamples} from './e2e';
import {e2eRun} from './e2e';
import {HttpClient} from './http-client';
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
@@ -34,7 +35,7 @@ process.on('unhandledRejection', async (error) => {
commander
.command('e2e <to>')
.description('Run in end to end test mode. Indexing all test files from @openstapp/core to the backend')
.description('Run in end to end test mode. Indexing and afterwards retrieving all test files from @openstapp/core to the backend')
.option('-s --samples [path]', 'Path to @openstapp/core test files', './node_modules/@openstapps/core/test/resources')
.action(async (to, e2eCommand) => {
let toURL = '';
@@ -49,7 +50,7 @@ commander
actionDone = true;
indexSamples(client, {to: toURL, samples: e2eCommand.samples})
e2eRun(client, {to: toURL, samplesLocation: e2eCommand.samples})
.then(() => {
Logger.ok('Done');
})
@@ -108,7 +109,7 @@ commander
from: fromURL,
source: copyCommand.bulkSource,
to: toURL,
type: type,
type: type as SCThingType,
version: copyCommand.appVersion,
})
.then(() => {

View File

@@ -13,13 +13,18 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThings, SCThingType} from '@openstapps/core';
import {SCSearchRequest, SCThings, SCThingType} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import {deepStrictEqual} from 'assert';
import {readdir, readFile} from 'fs';
import {join} from 'path';
import {promisify} from 'util';
import {ConnectorClient} from './connector-client';
import {HttpClientInterface} from './http-client-interface';
const localItemMap: Map<string, SCThings> = new Map();
const remoteItemMap: Map<string, SCThings> = new Map();
/**
* Options to set up indexing core test files to backend
*/
@@ -27,7 +32,7 @@ export interface E2EOptions {
/**
* File path of the directory containing core test files
*/
samples: string;
samplesLocation: string;
/**
* URL of the backend to index to
@@ -36,18 +41,73 @@ export interface E2EOptions {
}
/**
* Function to add all the SCThings that getItemsFromSamples() returns to the backend
*
* @param client HTTP client
* @param options Map of options
* Function that can be used for integration tests.
* Adds all the SCThings that getItemsFromSamples() returns to the backend.
* Afterwards retrieves the items from backend and checks for differences with original ones.
*/
export async function indexSamples(client: HttpClientInterface, options: E2EOptions): Promise<void> {
const api = new ConnectorClient(client, options.to);
export async function e2eRun(client: HttpClientInterface, options: E2EOptions): Promise<void> {
localItemMap.clear();
remoteItemMap.clear();
const items = await getItemsFromSamples(options.samples);
const api = new ConnectorClient(client, options.to);
try {
await indexSamples(api, options);
Logger.info(`All samples have been indexed via the backend`);
await retrieveItems(api);
Logger.info(`All samples have been retrieved from the backend`);
compareItems();
} catch (error) {
throw error;
}
}
/**
* Retieves all samples previously index using the api
*/
async function retrieveItems(api: ConnectorClient): Promise<void> {
const singleItemSearchRequest: SCSearchRequest = {
filter: {
arguments: {
field: 'uid',
value: 'replace-me',
},
type: 'value',
},
};
for (const uid of localItemMap.keys()) {
singleItemSearchRequest.filter!.arguments.value = uid;
const searchResonse = await api.search(singleItemSearchRequest);
if (searchResonse.data.length !== 1) {
throw Error(`Search for single SCThing with uid: ${uid} returned ${searchResonse.data.length} results`);
}
remoteItemMap.set(uid, searchResonse.data[0]);
}
}
/**
* Compares all samples (local and remote) with the same uid and throws if they're not deep equal
*/
function compareItems() {
for (const localThing of localItemMap.values()) {
/* istanbul ignore next retrieveItems will throw before*/
if (!remoteItemMap.has(localThing.uid)) {
throw Error(`Did not retrieve expected SCThing with uid: ${localThing.uid}`);
}
const remoteThing = remoteItemMap.get(localThing.uid);
deepStrictEqual(remoteThing, localThing, `Unexpected difference between original and retrieved sample`);
}
Logger.info(`All samples retrieved from the backend are the same (deep equal) as the original ones submitted`);
}
/**
* Function to add all the SCThings that getItemsFromSamples() returns to the backend
*/
async function indexSamples(api: ConnectorClient, options: E2EOptions): Promise<void> {
const items = await getItemsFromSamples(options.samplesLocation);
if (items.length === 0) {
throw new Error('Could not index samples. None were retrived from the file system.');
throw new Error('Could not index samples. None were retrieved from the file system.');
}
try {
@@ -57,17 +117,14 @@ export async function indexSamples(client: HttpClientInterface, options: E2EOpti
if (!itemMap.has(item.type)) {
itemMap.set(item.type, []);
}
const currentItems = itemMap.get(item.type) as SCThings[];
currentItems.push(item);
itemMap.set(item.type, currentItems);
const itemsOfSameType = itemMap.get(item.type) as SCThings[];
itemsOfSameType.push(item);
itemMap.set(item.type, itemsOfSameType);
localItemMap.set(item.uid, item);
}
// add items depending on their type property with one type per bulk
for (const type of itemMap.keys()) {
const currentBulk = await api.bulk(type, 'stapps-core-sample-data');
for (const item of (itemMap.get(type) as SCThings[])) {
await currentBulk!.add(item);
}
await currentBulk.done();
await api.index(itemMap.get(type) as SCThings[], 'stapps-core-sample-data');
}
} catch (err) {
throw err;