refactor: update e2e methods

This commit is contained in:
Michel Jonathan Schmitz
2019-05-21 13:35:51 +02:00
parent 2c43fc09b5
commit ec6296a606

View File

@@ -13,11 +13,10 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCThings} from '@openstapps/core';
import {SCThings, SCThingType} from '@openstapps/core';
import {readdir, readFile} from 'fs';
import {join} from 'path';
import {promisify} from 'util';
import {Bulk} from './bulk';
import {ConnectorClient} from './connectorClient';
import {HttpClientInterface} from './httpClientInterface';
@@ -51,25 +50,23 @@ export async function indexSamples(client: HttpClientInterface, options: E2EOpti
throw new Error('Could not index samples. None were retrived from the file system.');
}
items.sort((a, b) => (a.type.localeCompare(b.type)));
let currentBulkType;
let currentBulk: Bulk<SCThings> | undefined;
try {
// Add items depending on their type property with one type per bulk
// sort items by type
const itemMap: Map<SCThingType, SCThings[]> = new Map();
for (const item of items) {
if (currentBulkType !== item.type) {
if (typeof currentBulk !== 'undefined') {
await currentBulk.done();
}
currentBulk = await api.bulk(item.type, 'stapps-core-sample-data');
currentBulkType = item.type;
if (!itemMap.has(item.type)) {
itemMap.set(item.type, []);
}
await currentBulk!.add(item);
const currentItems = itemMap.get(item.type) as SCThings[];
currentItems.push(item);
itemMap.set(item.type, currentItems);
}
// close the last open bulk
if (typeof currentBulk !== 'undefined') {
// 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();
}
} catch (err) {
@@ -92,10 +89,12 @@ export async function getItemsFromSamples<T extends SCThings>(samplesDirectory:
const fileNames = await readDirPromised(samplesDirectory);
for (const fileName of fileNames) {
const filePath = join(samplesDirectory, fileName);
const fileContent = await readFilePromised(filePath, {encoding: 'utf8'});
const schemaObject = JSON.parse(fileContent);
if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') {
things.push(schemaObject.instance);
if (filePath.endsWith('.json')) {
const fileContent = await readFilePromised(filePath, {encoding: 'utf8'});
const schemaObject = JSON.parse(fileContent);
if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') {
things.push(schemaObject.instance);
}
}
}
} catch (error) {