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,6 +12,9 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
// tslint:disable-next-line: max-line-length
// tslint:disable: completed-docs no-implicit-dependencies prefer-function-over-method newline-per-chained-call member-ordering
import {
SCBulkAddResponse,
SCBulkAddRoute,
@@ -19,19 +22,23 @@ import {
SCBulkDoneRoute,
SCBulkResponse,
SCBulkRoute,
SCSearchResponse,
SCSearchRoute,
SCThing,
SCThings,
} from '@openstapps/core';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies';
import clone = require('fast-clone');
import {existsSync, mkdirSync, rmdirSync, unlinkSync} from 'fs';
import {createFileSync} from 'fs-extra';
import {suite, test} from 'mocha-typescript';
import {join} from 'path';
import {getItemsFromSamples, indexSamples} from '../src/e2e';
import {e2eRun, getItemsFromSamples} from '../src/e2e';
import {ApiError} from '../src/errors';
import {HttpClient, RequestOptions, Response} from '../src/http-client';
import {RecursivePartial} from './client.spec';
import {createFileSync} from 'fs-extra';
chai.should();
chai.use(chaiSpies);
@@ -43,8 +50,12 @@ const bulkRoute = new SCBulkRoute();
const bulkAddRoute = new SCBulkAddRoute();
const bulkDoneRoute = new SCBulkDoneRoute();
const searchRoute = new SCSearchRoute();
const httpClient = new HttpClient();
const storedThings: Map<string, SCThings> = new Map();
@suite
export class E2EConnectorSpec {
async after() {
@@ -52,18 +63,16 @@ export class E2EConnectorSpec {
}
@test
getCoreTestSamples() {
return getItemsFromSamples('./node_modules/@openstapps/core/test/resources')
.then(<T extends SCThing>(items: T[]) => {
// tslint:disable-next-line: no-unused-expression
chai.expect(items).to.not.be.a.instanceof(Error);
// tslint:disable-next-line: no-unused-expression
chai.expect(items).to.not.be.empty;
});
async getCoreTestSamples() {
const items = await getItemsFromSamples('./node_modules/@openstapps/core/test/resources');
// tslint:disable-next-line: no-unused-expression
chai.expect(items).to.not.be.a.instanceof(Error);
// tslint:disable-next-line: no-unused-expression
chai.expect(items).to.not.be.empty;
}
@test
getCoreTestSamplesShouldFail() {
async getCoreTestSamplesShouldFail() {
return getItemsFromSamples('./nonexistantdirectory')
.then(<T extends SCThing>(items: T[]) => {
// tslint:disable-next-line: no-unused-expression
@@ -72,11 +81,15 @@ export class E2EConnectorSpec {
}
@test
async index() {
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>;
async e2eRunSimulation() {
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse | SCSearchResponse>;
let failOnCompare = false;
let failOnLookup = false;
sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => {
if (request.url.toString() === 'http://localhost' + bulkRoute.getUrlFragment().toString()) {
if (request.url.toString() === `http://localhost${bulkRoute.getUrlFragment().toString()}`) {
return {
body: {
state: 'in progress',
@@ -84,21 +97,70 @@ export class E2EConnectorSpec {
},
statusCode: bulkRoute.statusCodeSuccess,
};
} else if (request.url.toString() === 'http://localhost' + bulkAddRoute.getUrlFragment({
UID: 'foo',
}).toString()) {
}
if (request.url.toString() === `http://localhost${bulkAddRoute.getUrlFragment({UID: 'foo'}).toString()}`) {
storedThings.set(request.body.uid, clone(request.body));
return {
body: {},
statusCode: bulkAddRoute.statusCodeSuccess,
};
}
if (request.url.toString() === `http://localhost${bulkDoneRoute.getUrlFragment({UID: 'foo'}).toString()}`) {
return {
body: {},
statusCode: bulkDoneRoute.statusCodeSuccess,
};
}
if (request.url.toString() === `http://localhost${searchRoute.getUrlFragment().toString()}`) {
const thing = storedThings.get(request.body.filter.arguments.value);
if (failOnCompare) {
thing!.origin!.modified = 'altered';
}
const returnThing = failOnLookup ? [] : [thing];
const returnBody = {
data: returnThing,
facets: [],
pagination: {
count: returnThing.length,
offset: 0,
total: returnThing.length,
},
stats: {
time: 42,
},
};
return {
body: returnBody,
statusCode: searchRoute.statusCodeSuccess,
};
}
return {
body: {},
statusCode: bulkDoneRoute.statusCodeSuccess,
statusCode: searchRoute.statusCodeSuccess,
};
});
await indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'});
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'});
failOnLookup = true;
failOnCompare = false;
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith('Search for single SCThing with uid');
failOnLookup = false;
failOnCompare = true;
// tslint:disable-next-line: max-line-length
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith('Unexpected difference');
}
@test
@@ -106,36 +168,40 @@ export class E2EConnectorSpec {
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>;
sandbox.on(httpClient, 'request', async (): Promise<RecursivePartial<responses>> => {
return {
body: {},
statusCode: Number.MAX_SAFE_INTEGER,
};
});
return indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'})
// tslint:disable-next-line: max-line-length
return e2eRun(httpClient, {to: 'http://localhost', samplesLocation: './node_modules/@openstapps/core/test/resources'})
.should.be.rejectedWith(ApiError);
}
@test
async indexShouldFailDirectoryWithoutData() {
const emptyDirPath = join(__dirname, 'emptyDir');
if(!existsSync(emptyDirPath))
if (!existsSync(emptyDirPath)) {
mkdirSync(emptyDirPath);
await indexSamples(httpClient, {to: 'http://localhost', samples: emptyDirPath})
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.');
}
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: emptyDirPath})
.should.be.rejectedWith('Could not index samples. None were retrieved from the file system.');
rmdirSync(emptyDirPath);
}
@test
async indexShouldFailDirectoryWithoutJsonData() {
const somewhatFilledDirPath = join(__dirname, 'somewhatFilledDir');
if(!existsSync(somewhatFilledDirPath))
if (!existsSync(somewhatFilledDirPath)) {
mkdirSync(somewhatFilledDirPath);
}
const nonJsonFile = join (somewhatFilledDirPath, 'nonjson.txt');
createFileSync(nonJsonFile);
await indexSamples(httpClient, {to: 'http://localhost', samples: somewhatFilledDirPath})
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.');
await e2eRun(httpClient, {to: 'http://localhost', samplesLocation: somewhatFilledDirPath})
.should.be.rejectedWith('Could not index samples. None were retrieved from the file system.');
unlinkSync(nonJsonFile);
rmdirSync(somewhatFilledDirPath);
}
}