mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 03:32:52 +00:00
refactor: update e2e methods
This commit is contained in:
41
src/e2e.ts
41
src/e2e.ts
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user