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/>. * 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 {readdir, readFile} from 'fs';
import {join} from 'path'; import {join} from 'path';
import {promisify} from 'util'; import {promisify} from 'util';
import {Bulk} from './bulk';
import {ConnectorClient} from './connectorClient'; import {ConnectorClient} from './connectorClient';
import {HttpClientInterface} from './httpClientInterface'; 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.'); 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 { 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) { for (const item of items) {
if (currentBulkType !== item.type) { if (!itemMap.has(item.type)) {
if (typeof currentBulk !== 'undefined') { itemMap.set(item.type, []);
await currentBulk.done();
}
currentBulk = await api.bulk(item.type, 'stapps-core-sample-data');
currentBulkType = 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 // add items depending on their type property with one type per bulk
if (typeof currentBulk !== 'undefined') { 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 currentBulk.done();
} }
} catch (err) { } catch (err) {
@@ -92,10 +89,12 @@ export async function getItemsFromSamples<T extends SCThings>(samplesDirectory:
const fileNames = await readDirPromised(samplesDirectory); const fileNames = await readDirPromised(samplesDirectory);
for (const fileName of fileNames) { for (const fileName of fileNames) {
const filePath = join(samplesDirectory, fileName); const filePath = join(samplesDirectory, fileName);
const fileContent = await readFilePromised(filePath, {encoding: 'utf8'}); if (filePath.endsWith('.json')) {
const schemaObject = JSON.parse(fileContent); const fileContent = await readFilePromised(filePath, {encoding: 'utf8'});
if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') { const schemaObject = JSON.parse(fileContent);
things.push(schemaObject.instance); if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') {
things.push(schemaObject.instance);
}
} }
} }
} catch (error) { } catch (error) {